The node package manager (npm) utilize a section called "scripts" which can be used as an automation tool. With npm scripts, we can automate pretty much everything from running the application to testing and debugging.

Scripts in package.json

When we run nmp init, npm will walk us through a series of questions to create a package.json file. Alongside those questions, we have there is a question about the test command, which if left empty, will end up with echo \"Error: no test specified\" && exit 1 in the package.json under scripts. The following is an example of package.json.

{
    "name": "node_dev",
    "version": "1.0.0",
    "description": "",
    "main": "app.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "author": "",
    "license": "MIT"
}

That scripts section is where we define those automation commands.

scripts accepts nearly everything that we can run in the terminal, including Unix and dos commands.

Running Scripts

npm supports some key scripts by default such as install, start, stop, publish, test...etc. Those can be run as simple as appending them to the npm command.

npm install

However, arbitrary scripts can be executed by running npm run <script_name>. Those scripts could have any desired name.

npm run dev

Some examples will help to demonstrate this. Let's define a start script that will start a Node.js application which is in app.js for instance.

"scripts": {
    "start": "node app"
}

Now when we run npm start in the terminal, we will have our application running. Because the scripts can run anything, we can chain another command there that will serve our app and open a new window in the browser.

"scripts": {
    "start": "node app & open http://localhost:3000"
}

When we are developing an application, we usually use some tool or package to reload our application such as node-dev. We can add another command which we can run for lunching Node.js in development mode using that package and the dev script key.

"scripts": {
    "start": "node app & open http://localhost:3000",
    "dev": "node-dev app & open http://localhost:3000"
}

Pre and Post Hooks

Every script has a pre and postscript. For example, the start script can have prestart that will run before start, and poststart that will run after. This could be helpful for instance when we need to transpile our JS files, compile LESS or SASS to CSS, bundle the app with webpack or browserify, or even running gulp.

"scripts": {
    "build": "browserify index.js > index.min.js",
    "test": "mocha test/",
    "prestart": "gulp",
    "start": "node app & open http://localhost:3000",
    "predev": "gulp",
    "dev": "node-dev app & open http://localhost:3000"
}

One good example of running post scripts is when we are using node-inspector for debugging our Node.js app for instance.

"scripts": {
    "build": "browserify index.js > index.min.js",
    "test": "mocha test/",
    "prestart": "gulp",
    "start": "node app & open http://localhost:3000",
    "predev": "gulp",
    "dev": "node-dev app & open http://localhost:3000",
    "predebug": "gulp",
    "debug": "node-inspector & node --debug app",
    "postdebug": "open http://localhost:3000 & open http://localhost:8080/debug?port=5858",
}