NPM - Script

About

npm scripts are command line that are specified in the script element of package.json.

Management

Run

Command line

To run a script, you would execute the following npm command

yarn scriptName
# or
npm run scriptName

All

To run the script sequentially on all platform. https://www.npmjs.com/package/npm-run-all

Example with the release script:

"scripts": {
    "build": "cross-env NODE_ENV=production webpack --mode 'production'",
    "test": "jest",
    "publish": "npm publish",
    "release": "npm-run-all test build publish",
}

VsCode

From the VsCode IDE in a VsCode - Task (tasks.json)

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "npm",
            "script": "test",
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

Shortcut (Alias)

  • npm test is a shortcut for npm run test
  • npm start is a shortcut for npm run start

Search path

Within scripts you can reference locally installed npm packages by name instead of writing out the entire path. This convention is the standard in most npm-based projects and allows for instance to directly call a package. Example:

  • myApp,
  • instead of ./node_modules/.bin/myApp

Documentation / Reference


Powered by ComboStrap