Understanding the npm scripts in node.js

Node.jsServer Side ProgrammingProgramming

So far we are running our App.js using following command −

Node App.js

We can use npm scripts to run or debug our app.

How to start a node project

Command is − npm init

Above command will initiate a project, it will ask few questions about project name and starting file name etc.

As we have App.js file already give App.js file as starting entry file name. npm init command will create a package.json file from where dependencies for project can be added/updated/removed.

Package.json file looks like this, Its in json file format as per file extension suggests −

    "name": "dev",
   "version": "1.0.0",
   "description": "",
   "main": "App.js",
   "scripts": {
      "test": "echo \"Error: no test specified\" && exit 1"
   "author": "",
   "license": "ISC"

Main is entry file name , which is App.js file here.

We can see there are scripts for test as well added by default. We can customize the scripts as per our requirement.

Adding a start script

Lets add a start script for App.js to run. In scripts section add below entry in it −

"scripts": {
   "test": "echo \"Error: no test specified\" && exit 1",
   "start": "node App.js"

Now, we don’t need to manually use node App.js to run application instead we can npm script −

npm start

start and test are a reserved keyword in npm scripts. If we want to create a script name other than start like −

“dev”: “node App.js”

Then to run it, we will have to execute npm run dev , notice the use of run keyword after npm .

Package.json file holds the dependencies and dev-dependencies for a project. The other people will have to just execute npm install to get those dependencies in their project.

The dependencies which are required only during development of application are installed using

npm install –save-dev library_name

if any library needs to installed globally in local system,

npm install –g library-name

-g flag denotes the global install of the library, now we can use that library in other projects without need to install it again.

Webpack library helps in building a project into a defined structure , we can customize as per project requirement.

Published on 13-May-2020 12:14:28