npm and Node.js Management

npm packages

install and save

Install a package and also write it to package.json under "dependencies":{}

npm install <PACKAGE> --save  

Alternatively, for developer dependencies:

npm install <PACKAGE> --save-dev or npm i -D <PACKAGE>

npm install, update, run (outside of current directory)

npm install --prefix <PATH to directory>

npm update --prefix <directory>

npm run --prefix <script defined in package.json>

npm scripts

Custom scripts

  1. Open package.json
  2. Add the key scripts and a dictionary. Each dictionary has a key for the script name (to be run as npm run custom-script, etc.) and a value that represents the actual command string.

Note: For the dictionary values, if you are trying to run binary files from your local node_modules, you do not need to refer to it explicitly. (i.e., "babel-node index.js" instead of "node_modules/.bin/babel-node index.js")

Here is an example in package.json:

  "scripts": {
    "start": "babel-node index.js",
    "build": "babel ./ -d build/",
    "production": "node build/index.js"
  }

node_modules

Running local node_modules

Sometimes you just want to run Grunt or Webpack in the local project directory, rather than from the global node_modules.

./node_modules/.bin/<module> ...

npm for production

Running the node server in production

NODE_ENV=production node index.js  

n

Use specific Node/npm versions installed with n

/usr/local/n/versions/node/<VERSION>/bin/node or /usr/local/n/versions/node/<VERSION>/bin/npm

Activate a specific version with n (without interactive GUI)

n <VERSION>

Forever

Run specific Node binary with Forever

forever start -c "<PATH to Node binary> index.js"