NODE, EXPRESS,Nodemon, Babel(ES6)

Izabayo Blaise
4 min readSep 22, 2020

NODE.JS WITH NODEMON/EXPRESS

Project initializer

first, we will need to start our project by running “npm init”

npm init <initializer> can be used to set up a new or existing npm package.

initializer in this case is an npm package named create-<initializer>, which will be installed by npx, and then have its main bin executed – presumably creating or updating package.json and running any other initialization-related operations.

If the initializer is omitted (by just calling npm init), init will fall back to legacy init behavior. It will ask you a bunch of questions, and then write a package.json for you. It will attempt to make reasonable guesses based on existing fields, dependencies, and options selected. It is strictly additive, so it will keep any fields and values that were already set. You can also use -y/--yes to skip the questionnaire altogether. If you pass --scope, it will create a scoped package.

after all of that a package.json file will be created for us:

{
...
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"},
"keywords": [],
...
}

Express

Express.js is the most popular choice when it comes to building web applications with Node.js. However, when saying web applications with Node.js, it’s often not for anything visible in the browser (excluding server-side rendering of a frontend application). Instead, Express.js, a web application framework for Node.js, enables you to build server applications in Node.js.

The Node.js application from before comes with a watcher script to restart your application once your source code has changed, Babel to enable JavaScript features that are not supported in Node.js yet, and environment variables for your application's sensitive information. That's a great foundation to get you started with Express.js in Node.js. Let's continue by installing Express.js in your Node.js application from before on the command line:

npm install express

Now, in your src/index.js JavaScript file, use the following code to import Express.js, to create an instance of an Express application, and to start it as an Express server:

const express = reqiure('express');
const app = express();
app.listen(3000, function(){
console.log('Example app listening on port 3000!'),
});

Now, all we need to do is to add a command for starting our server in our package.json file under scripts

"scripts": {
"start": node src/index.js,
"test": "echo \"Error: no test specified\" && exit 1"},
...

Once you start your application on the command line with npm start, you should be able to see the output in the command line:

Example app listening on port 3000!

Nodemon

So far, you are able to start your application by running the npm start script. The only remaining concern is that you have to start the script every time you want to try your source code. You can change this behavior with an always-running node process. To remedy this, install the commonly used nodemon library on the command line as a development dependency to your project.

npm install nodemon --save-dev

Next, exchange node with nodemon in your npm start script:

{
...
"scripts": {
"start": "nodemon src/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
...

When you run your application with npm start from the command line, it should keep running. The best part is that the script will execute again once you change the source code. Try adjusting your source code in the src/index.js file and see what happens in your command line.

Babel

You should be able to develop a Node.js application by now, but there is more to setting up a sophisticated Node.js project that is capable of using recent JavaScript language features (ECMAScript) that are not included in the recent Node.js versions. That’s where Babel becomes useful. You can install it from the command line for your project’s development dependencies.

npm install @babel/core @babel/node --save-dev

Next, add it to your npm start script:

{...
"main": "index.js",
"scripts": {
"start": "nodemon --exec babel-node src/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
...
}

Nothing should change when you run the application again, though that’s just the surface. Under the hood, Babel transpiler your code to vanilla JavaScript. When you use an upcoming JavaScript language feature, which hasn’t been introduced in Node.js, you can still use the feature in your source code. Babel makes sure that Node.js understands it. However, there is still one crucial step to include upcoming language features with Babel. You can activate different upcoming JavaScript features by adding them as presets to Babel. Let’s add the most common used Babel preset to your application:

npm install @babel/preset-env --save-dev

Now, in the project’s root folder, create a .babelrc file in the command line:

touch .babelrc

In this configuration file for Babel, you can include the recently installed dependency for unlocking the upcoming JavaScript language features.

{
"presets": [
"@babel/preset-env"
]
}

Now you can include upcoming JavaScript features in your src/index.js file. If you run into problems because your desired feature is not working, check whether there exists a dedicated Babel preset for it.

now that we have babel up and running we can now update our src/indexjs file with Es6 code

import express from "express";
const app = express();
app.listen(3000, () => {
console.log('Example app listening on port 3000!'),
});

That will be all for this article hope it was helpful
please also ready about HOW TO SETUP SEQUELIZE ON NODEJS

THANK YOU!

--

--