configure scripts to run in Nodejs


By default Nodejs package.json contains a single scripts, but you can add and use as many you want.

For example I want to setup a development script with nodemon and and want to run a particular javascript file.

First find the flowing section in pacakge.json file

"scripts": {
        
        "test": "echo \"Error: no test specified\" && exit 1"
    },

and add one more line to it as follows

"scripts": {
        "dev": "nodemon app.js localhost:3000",
        "test": "echo \"Error: no test specified\" && exit 1"
    },

Run the script

We can now use npm command to run the script we configured as

npm run dev

Remember add the nodemon to your project. It is is developer dependency. Don’t know how to add dependency ?, here is the guide.

  • Add packages to dependency in Nodejs

    Do you know that, we can add package names to packages.json file while install using npm ( Node Package Manager)

    Wanna learn how to add a package file to Node project. Here it is

     

    For this we have to use npm install <package-name> –save

    npm install express  --save
    

    In your package file it will show as

     "dependencies": {
            "express": "^4.17.1",
            "socket.io": "^3.0.1"
        }
    

    Uninstalling will also remove it from the dependency list.

    Developer dependecies

    Packages which are used only for at the time of development are called developer dependecies. We can add developer dependencies by adding an option with Install save option as -dev. nodemon is an ideal example for development dependency.

    </p>
    npm install nodemon --save-dev
    <p>

Published by

Manoj

A developer,teacher and a blogger obsessed with Python,Dart and open source world from India

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.