Setup an ES6 NodeJS Project

Thursday, March 16th 2023

There are several ways to set up an ES6 Node.js project, but one of the fastest and simplest ways is to use the npm package manager to initialize a new project with a pre-configured package.json file that includes the necessary dependencies and scripts.

Here are the steps to set up a new ES6 Node.js project using npm:

  1. Install the latest version of Node.js on your machine if you haven't already done so.

  2. Open a terminal or command prompt and navigate to the directory where you want to create the new project.

  3. Run the following command to initialize a new Node.js project with npm:

npm init -y

This will create a new package.json file in the current directory with default values for all fields.

  1. Install the necessary dependencies for an ES6 Node.js project:
npm install --save-dev @babel/core @babel/node @babel/preset-env nodemon
  • @babel/core and @babel/preset-env are used for transpiling ES6 code to ES5 so it can be run in Node.js.
  • @babel/node is used for running Node.js scripts with Babel.
  • nodemon is a utility that automatically restarts the Node.js server when changes are made to the code.
  1. Add a .babelrc file to the project root with the following contents:
{ "presets": ["@babel/preset-env"] }

This tells Babel to use the @babel/preset-env preset to transpile the code.

  1. Add a start script to the package.json file:
{ "scripts": { "start": "nodemon --exec babel-node index.js" } }

This tells nodemon to watch for changes to the code and restart the server automatically, and also tells it to run the code with Babel using the --exec babel-node option.

  1. Create a new index.js file in the project root with the following contents:
console.log('Hello, world!');
  1. Run the project with the following command:
npm start

This should start the server and output "Hello, world!" to the console.