How to configure a node.js application with es6 support and server reboot when a file changes .
I. Configuration steps (creating a project from scratch):
1.Go in the terminal to the main directory of your project
npm init // create package.json for the project
2. Install dependencies
npm install
1 - it can be stage 1 or 2, it depends on what functions we want to use
3. We should have something like this in the package.json file (the version of the package will probably be different, but this is normal):
"devDependencies": { "babel": "^6.5.2", "babel-cli": "^6.16.0", "babel-preset-es2015": "^6.16.0", "babel-preset-stage-0": "^6.16.0", "nodemon": "^1.11.0" }
4. Create a .babelrc file in the project root directory (there is a package.json file)
{ "presets": ["es2015", "stage-0"] }
5. Create two directories:
src is the working directory with files written to es6
dist - here the files will be compiled for es5 using babel
The root directory of the project should look like this:
- Project
- CSI
- index.js // main project file
- distance
- package.json
- .babelrc
7. Add the necessary commands to package.json :
"scripts": { "watch": "babel -w src/ -d dist/", "build": "babel src/ -d dist/", "serve": "babel -w src/ -d dist/ | nodemon --watch dist", "test": "echo \"Error: no test specified\" && exit 1" }
8. Available commands:
npm run watch // starts watch viewing in src directory and compiles to dist
npm run build // compiles files from src directory to dist
npm run serve // it starts watch + start node server, every time the file changes, it restarts the node server using nodemon, which tracks changes to the dist directory
9. Final notes
- The server will run the dist / index.js file as the main file.
- The dist / index.js file will be compiled from src / index.js, so there should be a main project file.
- the dist directory must be added to ignore using git (but not ignore it for npm if it is node).
10. Start the server and run the application in the src directory.
npm run serve
II. Easy way (ready to use template)
If you have too many points, then the full complete woking scheme is available on github - https://github.com/maciejsikora/node-express-babel-boilerplate .