NodeJS Application built with Babel
Do you fancy to write a node.js app (that uses the latest and greatest Javascript syntax) and would like to build?
The application we are going to build is no React / Angular application. Hence, the use of webpack could be an overkill.
Instead let’s do it all using package.json
and .babelrc
Building an application
Step 1: Create a node application
$ mkdir myserver_app
$ cd my_server_app
$ npm init -y
Wrote to ./myserver_app/package.json:{
"name": "myserver_app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
$ mkdir src
Step 2: Add the following source code to src/server.js, package.json and .babelrc
Step 3: Once added, run npm install
at the project root directory ./myserver_app
.
Step 4: Now run npm start
to run the project in dev mode.
Step 5: Now that our app can run in dev
mode, let’s emulate a package on dist
by running npm build
and npm dist
How did it all work?
There are couple of pieces to have this work.
- .babel file’s
@babel/plugin-transform-runtime
is responsible for polyfill since Babel 7.4
Note: Babel 7.4 started to deprecate@babel/polyfill
plugin. - .babel file’s preset is responsible for supporting modern JS functionality.
- runtime dependency of
@babel/runtime
. This is also required for being able to build the node application usingbabel
.
All other things are routine NodeJS nuts and bolts.
- Express — NodeJS web application framework
- Morgan — Used for logging express
- nodemon — watches source code for rerunning.
Hopefully this article has helped with building a NodeJS app.