Skip to content

Instantly share code, notes, and snippets.

@arkon108
Last active February 3, 2022 09:59
Show Gist options
  • Save arkon108/466ee53eaba3e1cb6e8a356b64d4e239 to your computer and use it in GitHub Desktop.
Save arkon108/466ee53eaba3e1cb6e8a356b64d4e239 to your computer and use it in GitHub Desktop.

The minimal Node.js with Babel Setup

Nodemon

Once the project dir is created and initialized, install nodemon for development only

npm install nodemon --save-dev

Then place the nodemon in the npm start script

{
  ...
  "main": "index.js",
  "scripts": {
    "start": "nodemon src/index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  ...
}

Now the application will keep running once started with npm start from the command line.

Babel

To use modern JS/ECMAScript install Babel

npm install @babel/core @babel/node --save-dev

Next, add it to the npm start script

{
  ...
  "main": "index.js",
  "scripts": {
    "start": "nodemon --exec babel-node src/index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  ...
}

Different JS features are activated by adding them as presets to Babel. Add the most common Babel preset

npm install @babel/preset-env --save-dev

In the project root folder create Babel config. .babelrc is used for specific files or parts of the project, and babel.config.js for global settings.

touch babel.config.js

Place the following inside

module.exports = {
  presets: [
    '@babel/preset-env',
  ],
  targets: {
    node: 'current',
  },
};

Adapted from The minimal Node.js with Babel Setup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment