How To Enable ES6 (And Beyond) Syntax With Node and Express
How To Enable ES6 (And Beyond) Syntax With Node and Express
Forum Donate
syntax, but then when you decided to learn back-end Forum Donate
development with
Learn Node.js
to code — freeand Express,
3,000-hour you realized
curriculum
that you can’t use stuff like import from and export de
fault ? If so, you came to the right place! This is step by
step guide on how to con gure your dev and prod
environments, setup scripts, and as a bonus we’ll learn
how to add tests!
Prerequisites
Installing express
Setting up scripts
Bonus
TL;DR
https://www.freecodecamp.org/news/how-to-enable-es6-and-beyond-syntax-with-node-and-express-68d3e11fe1ab/ 2/20
2/8/2021 How to enable ES6 (and beyond) syntax with Node and Express
So that’s the high-level view of how it works under the hood. With
that, let’s start on how should we setup or project.
Prerequisites
Before we begin, we need some things setup rst.
https://www.freecodecamp.org/news/how-to-enable-es6-and-beyond-syntax-with-node-and-express-68d3e11fe1ab/ 3/20
2/8/2021 How to enable ES6 (and beyond) syntax with Node and Express
3. Make sure you have your terminal open and your favorite text
editor installed.
Installing Express
Using the Express generator, we will create a new project with
generated code, move some les, and convert some code to ES6
syntax. We need to convert it at this early stage because we need a
way to verify if our ES6 code works.
Project Setup
Run this command in your terminal. You can name your-project-nam
e with the name you like. --no-view ag means that we won’t be
using any templating engine such as handlebars, ejs, or pug, for our
skeleton Express app.
After creating your app, you need to go to your app directory. For
Windows Powershell and Linux terminals, use:
cd your-project-name
Next, open the text editor you like. For me, I just use VSCode so I just
have my terminal and text editor open at the same time. But you can
https://www.freecodecamp.org/news/how-to-enable-es6-and-beyond-syntax-with-node-and-express-68d3e11fe1ab/ 4/20
2/8/2021 How to enable ES6 (and beyond) syntax with Node and Express
Installing Learn
Packages and
to code — free Moving
3,000-hour and
curriculum
Deleting Files
After we have the generated project ready, we need to install the
dependencies and move some folders. Run this command to install
Express and other packages.
npm install
https://www.freecodecamp.org/news/how-to-enable-es6-and-beyond-syntax-with-node-and-express-68d3e11fe1ab/ 5/20
2/8/2021 How to enable ES6 (and beyond) syntax with Node and Express
Forum Donate
// package.json
{
"name": "your-project-name",
// ....other details
"scripts": {
"server": "node ./server/bin/www"
}
}
https://www.freecodecamp.org/news/how-to-enable-es6-and-beyond-syntax-with-node-and-express-68d3e11fe1ab/ 6/20
2/8/2021 How to enable ES6 (and beyond) syntax with Node and Express
Forum Donate
You’ll see that we changed the le path from ./bin/www to ./server/b
in/www becauseLearn to code —
we moved lesfree
to 3,000-hour curriculum
server/ . We’ll use start script later
on.
Try it! Try running the server by typing npm run server on your
terminal, and go to localhost:3000 on your browser.
// bin/www.js
/**
* Module dependencies.
*/
import app from '../app';
import debugLib from 'debug';
import http from 'http';
const debug = debugLib('your-project-name:server');
// ..generated code below.
Almost all of our modi cations are only at the top and bottom of the
les. We are leaving other generated code as is.
// app.js
import express from 'express';
import path from 'path';
import cookieParser from 'cookie-parser';
import logger from 'morgan';
import indexRouter from './routes/index';
import usersRouter from './routes/users';
var app = express();
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, '../public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
export default app;
app.use(express.static(path.join(__dirname, '../public')));
Okay we’re done with converting the code! Let’s setup our scripts
now.
Setting up Scripts
https://www.freecodecamp.org/news/how-to-enable-es6-and-beyond-syntax-with-node-and-express-68d3e11fe1ab/ 8/20
2/8/2021 How to enable ES6 (and beyond) syntax with Node and Express
Forum
In setting up scripts, each script performs a different role. And we Donate
reuse each npmLearn
script.
to And
codefor our3,000-hour
— free development and production
curriculum
environments, they have a different con guration. (Almost identical,
you’ll see later) That’s why we need to compose our scripts so we can
use them without repeatedly typing the same stuff all over again.
Install `npm-run-all`
Since some terminal commands won’t work on windows cmd, we need
to install a package called npm-run-all so this script will work for any
environment. Run this command in your terminal project root.
Inside your package.json le, create a "babel" object and put this
setting.
// package.json
{
// .. contents above
"babel": {
"presets": ["@babel/preset-env"]
},
}
After this setup we are now ready to test if babel really converts code.
Add a script named transpile in your package.json :
// package.json
"scripts": {
"start": "node ./server/bin/www",
"transpile": "babel ./server --out-dir dist-server",
}
Now what happened here? First we need to run the cli command bab
el , specify the les to convert, in this case, the les in server/ and
put the transpiled contents in a different folder called dist-server in
our project root.
Forum Donate
npm run transpile
Learn to code — free 3,000-hour curriculum
Yay it worked! ✅ As you can see, there’s a folder that has the same
folder structure as our server folder but with converted code inside.
Pretty cool right? Next step is to run try if our server is running!
Clean script
To have a fresh copy every-time we transpile code into new les, we
need a script that removes old les. Add this script to your
package.json
https://www.freecodecamp.org/news/how-to-enable-es6-and-beyond-syntax-with-node-and-express-68d3e11fe1ab/ 11/20
2/8/2021 How to enable ES6 (and beyond) syntax with Node and Express
This npm script that we made means it removes the folder dist-serve
r/
Now to combine transpile and clean, add a script called build , which
combines the two processes.
// scripts
"build": "npm-run-all clean transpile"
"scripts": {
"build": "npm-run-all clean transpile"
"server": "node ./dist-server/bin/www",
"dev": "NODE_ENV=development npm-run-all build server",
"transpile": "babel ./server --out-dir dist-server",
"clean": "rimraf dist-server"
}
https://www.freecodecamp.org/news/how-to-enable-es6-and-beyond-syntax-with-node-and-express-68d3e11fe1ab/ 12/20
2/8/2021 How to enable ES6 (and beyond) syntax with Node and Express
Forum
Note here that we’ve changed again the le we are running on our Donate
server script. We’re
Learnrunning the
to code — freele-path withcurriculum
3,000-hour the transpiled code,
found in dist-server/ .
"scripts": {
"start": "npm run prod"
"build": "npm-run-all clean transpile"
"server": "node ./dist-server/bin/www",
"dev": "NODE_ENV=development npm-run-all build server",
"prod": "NODE_ENV=production npm-run-all build server",
"transpile": "babel ./server --out-dir dist-server",
"clean": "rimraf dist-server"
}
We set start script default to prod because start script is being used
always by deployment platforms like AWS or Heroku to start a server.
// package.json
...
"nodemonConfig": {
"exec": "npm run dev",
"watch": ["server/*", "public/*"],
"ignore": ["**/__tests__/**", "*.test.js", "*.spec.js"]
https://www.freecodecamp.org/news/how-to-enable-es6-and-beyond-syntax-with-node-and-express-68d3e11fe1ab/ 13/20
2/8/2021 How to enable ES6 (and beyond) syntax with Node and Express
}, Forum Donate
"scripts": {
// ... other scripts
Learn to code — free 3,000-hour curriculum
"watch:dev": "nodemon"
}
// package.json
...
"nodemonConfig": {
"exec": "npm run dev",
"watch": ["server/*", "public/*"],
"ignore": ["**/__tests__/**", "*.test.js", "*.spec.js"]
},
"scripts": {
// ... other scripts
"watch:dev": "nodemon"
}
https://www.freecodecamp.org/news/how-to-enable-es6-and-beyond-syntax-with-node-and-express-68d3e11fe1ab/ 14/20
2/8/2021 How to enable ES6 (and beyond) syntax with Node and Express
Now that we have our le watcher, you can now just run npm run watc
h:dev , code, and save your le. and whenever you go to localhost:3
000 , you’ll see the changes. Try it out!
npm i -D jest
// package.json
...
"jest": {
"testEnvironment": "node"
},
"scripts": {
// ..other scripts
"test": "jest"
}
Try it out, make a le sample.test.js, write any tests, and run the script!
https://www.freecodecamp.org/news/how-to-enable-es6-and-beyond-syntax-with-node-and-express-68d3e11fe1ab/ 15/20
2/8/2021 How to enable ES6 (and beyond) syntax with Node and Express
Forum Donate
TL;DR
Here are the simpli ed steps for how to enable ES6 in Node.js. I’ll also
include the repo so you can copy and inspect the whole code.
Move the bin/ , routes/ and app into a new folder called
src/ , and convert the code into ES6. Also don’t forget to
rename bin/www to www.js
https://www.freecodecamp.org/news/how-to-enable-es6-and-beyond-syntax-with-node-and-express-68d3e11fe1ab/ 16/20
2/8/2021 How to enable ES6 (and beyond) syntax with Node and Express
Forum Donate
Add these scripts tocode
Learn to your—package.json
free 3,000-hour curriculum
"scripts": {
"start": "npm run prod",
"build": "npm-run-all clean transpile",
"server": "node ./dist-server/bin/www",
"dev": "NODE_ENV=development npm-run-all build server",
"prod": "NODE_ENV=production npm-run-all build server",
"transpile": "babel ./server --out-dir dist-server",
"clean": "rimraf dist-server",
"watch:dev": "nodemon",
"test": "jest"
}
"nodemonConfig": {
"exec": "npm run dev",
"watch": [ "server/*", "public/*" ],
"ignore": [ "**/__tests__/**", "*.test.js", "*.spec.js" ]
},
"babel": {
"presets": [ "@babel/preset-env" ]
},
"jest": {
"testEnvironment": "node"
},
Forum Donate
Notes andLearn
disclaimers
to code — free 3,000-hour curriculum
Note that this setup may not be proved ideal for all situations,
specially for big projects. (like 1k les of code). Transpiling step and
deleting might slow down your development environment. Plus, ES
Modules, is almost coming to node. But, nevertheless, this is a good
eductational material to understand how transipiling runs under the
hood like when we are developing front-end apps :)
Conclusion
All right! I hope you learned a lot. Thank you for reading this far.
Happy Coding!
Jonathan Cunanan
Javascript Engineer / React Dev with AWS Experience. Passionate in
#100DaysOfCode
If you read this far, tweet to the author to show them you care.
Tweet a thanks
https://www.freecodecamp.org/news/how-to-enable-es6-and-beyond-syntax-with-node-and-express-68d3e11fe1ab/ 18/20
2/8/2021 How to enable ES6 (and beyond) syntax with Node and Express
Our mission: to help people learn to code for free. We accomplish this by creating thousands of
videos, articles, and interactive coding lessons - all freely available to the public. We also have
thousands of freeCodeCamp study groups around the world.
Donations to freeCodeCamp go toward our education initiatives, and help pay for servers,
services, and staff.
Trending Guides
Our Nonpro t
About Alumni Network Open Source Shop Support Sponsors Academic Honesty
https://www.freecodecamp.org/news/how-to-enable-es6-and-beyond-syntax-with-node-and-express-68d3e11fe1ab/ 20/20