w3resource

Npm rebuild, repo, restart, root and run-script commands


The tutorial you are about to read, will introduce you to the rebuild, repo, restart, root and run-script commands of npm.

Npm-rebuild

This command will rebuild a package.

Synopsis

npm rebuild [[<@scope>/<name>]]
alias: npm rb

Description

The npm-rebuild command will run npm build command on the matched folders. This command is particularly useful when you install a new version of node, and you have to recompile all your C++ addons with the new binary.

Npm-repo

This command opens a package repository page in the browser.

Synopsis

npm repo [<pkg>]

Description

The command will try to guess the likeliest location of a package's repository URL, and then it will try to open it using the -browser config param. If you don't provide a package name, it searches for a package.json in the current folder and it will use the name property.

Configuration

Browser

This defines the browser that is called by the npm repo command to open websites

  • Default: 'Windows': "start", 'OS X': "open", Others: "xdg-open"
  • Type: String

Npm-restart

Restarts a package

Synopsis

npm restart [-- <args>]

Description

This command restarts a package.

This will run the "stop", "restart", and "start" scripts of a package as well as associated pre- and post- scripts, in the order given below:

  1. prerestart
  2. prestop
  3. stop
  4. poststop
  5. restart
  6. prestart
  7. start
  8. poststart
  9. postrestart

NOTE

It should be noted that the "restart" script is run along with the "stop" and "start" scripts, and not instead of them.

Npm-root

Displays the npm root.

Synopsis

npm root [-g]

Description

Prints the effective node_modules folder to standard to standard out.

Npm-run-script

Runs arbitrary package scripts.

Synopsis

npm run-script <command> [--silent] [-- <args>...]
alias: npm run

Description

This will run an arbitrary command from a package's "scripts" object. If you do not provide a "command", it lists the available scripts. The run[-script] command is used by the test, start, restart, and stop commands, but can be called directly, as well. Whenever the scripts in the package are printed out, they are separated into lifecycle (test, start, restart) and directly-run scripts.

As of npm version 2.0.0, you can use custom arguments when you are executing scripts. The special option ? will be used by getopt to delimit the end of the options. npm passes all the arguments after the -- directly to your script:

npm run test -- --grep="pattern"

The arguments are only passed to the script specified after npm run and not to any post or pre script.

The env script is a special built-in command that you can use to list environment variables that will be available to the script at runtime. If you define an "env" command in your package, it takes precedence over the built-in.

In addition to the pre-existing shell's PATH, npm run will add node_modules/.bin to the PATH provided to scripts. The binaries that are provided by locally-installed dependencies can be used without the node_modules/.bin prefix. For instance, in the case where there is a devDependency on tap in your package, you should write:

"scripts": {"test": "tap test/\*.js"}

instead of

"scripts": {"test": "node_modules/.bin/tap test/\*.js"} 

to run your tests.

The actual shell that your script is run within is platform dependent. By default, for Unix-like systems it is the /bin/sh command, while for Windows it is the cmd.exe. The actual shell that is referred to by /bin/sh also depends on the system. As of [email protected] you can customize the shell using the script-shell configuration.

Scripts will be run from the root of the module, irrespective of what your current working directory is when you call npm run.

You can use the -silent flag to prevent npm from showing npm ERR! output on error.

The -if-present flag can be used to avoid exiting with a non-zero exit code when the script is undefined. This enables you run potentially undefined scripts without breaking the execution chain.

Previous: Npm-prune and npm-publish cli commands
Next: Npm-search and npm-shrinkwrap cli commands