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:
Install the latest version of Node.js on your machine if you haven't already done so.
Open a terminal or command prompt and navigate to the directory where you want to create the new project.
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.
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..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.
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.
index.js
file in the project root with the following contents:console.log('Hello, world!');
npm start
This should start the server and output "Hello, world!" to the console.