1 votes

Using @babel/node in Babel version 7

I have a very big doubt, when using Babel with an express server, including node, I perform the following steps:

npm init -y para generar el package.json

I install nodemon and express:

npm i nodemon express

after that I install the packages:

npm i -D @babel/core @babel/cli @babel/preset-env

in .babelrc I write the following:

{
    "presets":[
        "@babel/preset-env"
    ]
}

In the package.json I write a new script:

"start": "nodemon --exec babel-node index.js "

I create an index.js file and in it:

import express from 'express';

const app = express();

app.get('/', (req, res) => {
    res.send('Todo Listo');
});

app.listen(8000, () => console.log('El servidor esta funcionando'));

when I run through

 npm start

I get an error:

introducir la descripción de la imagen aquí

then when installing babel-node in the course of the installation I get these messages:

introducir la descripción de la imagen aquí

In reviewing Babel's documentation I note the following:

introducir la descripción de la imagen aquí

@babel/node is not recommended, as it is unnecessarily heavy, and the transpilation would not be correct.

Please help. With the previous version of Babel this problem did not occur. Thank you.

0 votes

install it globally npm install -g babel-cli

0voto

codemcu Points 141

the only problem is in the npm scripts you have to put it like this

  "scripts": {
    "start": "node index.js",
    "server": "nodemon index.js"
  },

and then launch npm run server so that nodemon will stay listening for changes and if you go to http://localhost:8000/ you will see that everything works correctly

Greetings

edit: and in the index.js you have to change the first line to:

const express = require('express');

0 votes

You have a problem with this answer. The OP clearly wants to use babel to be able to run your application which is written with syntax of EN6 . In your answer you recommend to change 1 line of his code, but if the whole application is in ES6, then you are suggesting to rewrite the whole application. Check your answer. Regards

0 votes

I'm not suggesting anything, the application won't start because it has a problem in the npm start . Everything you have set up works perfectly with ES6 and maybe you will ask yourself, how am I so sure? because I have the same configuration in a project, that's why I decided to answer. Best regards

0voto

Mauricio Contreras Points 2571

Your doubt is quite valid, however, I think you are confusing a bit the message from the official website of @babel/node .

The translation of the message would be approximately as follows:

Not intended for production use

Should not be used babel-node in production. It is unnecessarily heavy, with high memory usage due to loading modules into the memory cache. In addition, you will always experience a startup performance penalty, because the entire application must be compiled on the fly.

This message does not say that do not use babel-node . The message only indicates that it has not been designed for production environment. Therefore, in a development environment it is totally valid to use it, and I even recommend it.

As for the way of doing things with Babel 7, you can find a example in the official repository.

On the other hand, the message of the deprecated package @babel/polyfill is simply a warning message stating that this package has been deprecated in favor of using core-js . Then the installation continues smoothly with the installation of the new units.

I will give you a basic example of how to perform the tasks you require in your question with the packages you have already installed.

We will create some scripts in the appropriate section of our archive package.json

In addition, we are going to create a folder called src inside our project root folder and all our application will go inside the folder src (except for files package.json , package-lock.json , .babelrc y .gitignore together with the folders node_modules y .git ).

package.json

{
    //...
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "dev": "nodemon --exec babel-node ./src/index.js",
        "clean": "rm -rf dist && mkdir dist",
        "build-babel": "babel -d ./dist ./src -s",
        "build": "npm run clean && npm run build-babel",
        "start": "npm run build && node ./dist/index.js"
    },
    //...
}

In this example you can notice different scripts to be used depending on the task you want to perform.

  • dev : East script is the one we will use during the development stage, as you can see it makes the call to nodemon and run the babel-node which is in charge of compiling the whole application written in ES6 and then executes the Node-Express server. To run this script you only write by console: npm run dev .
  • clean : East script is responsible for cleaning the directory dist where your ES6 code will be transpiled to Node-compatible JS code. This example uses Linux command ( rm y mkdir ), you can adapt them to your platform according to what you use.
  • build-babel : East script calls for babel and transpiles the code from the src and place it in the folder dist . You can read the documentation for more information on the use of the tool cli of babel.
  • build : East script is used to call the 2 previous ones, in this way in a single call the directory cleanup is executed and then the new code is transpiled.
  • start : This script is in charge of calling build and then run your server using Node from the folder dist . It is the script to be used in production in case the distribution is done from the same system.

This is a proper and basic way to use Babel 7 in your Node applications. There are many other ways to do the same, each one designed according to a different requirement.

It should be noted that the following should also not be used for production nodemon Remember that this library is also intended for the development environment and not for the production environment.

I hope this clears up your doubts.

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X