Folder and file structure:
Here is package.json
{ "name": "node-server-using-es6", "version": "1.0.0", "description": "Node Server compiles code to ES6 using babel", "main": "lib/index.js", "scripts": { "build": "rimraf build && babel src -d dist", "start": "nodemon --exec babel-node src/index", "prod": "npm run build && node dist/index.js" }, "license": "ISC", "devDependencies": { "@babel/cli": "^7.2.3", "@babel/core": "^7.4.0", "@babel/node": "^7.18.10", "@babel/preset-env": "^7.4.2", "@babel/register": "^7.4.0", "nodemon": "^1.17.5" "rimraf": "^3.0.2" }, "dependencies": { "express": "^4.18.1" } }
Here is .babelrc
{ "presets": ["@babel/preset-env"] }
Here is src/index.js in ES6
import express from 'express'; const app = express(); const PORT = 4000; app.get('/', (req, res) => { res.send('Hello World!'); }); app.listen(4000, () => { console.log(`app is running on ${PORT}`); });
for production npm run build this will populate build folder
for development npm start