Local package installation
To install a package locally, navigate to your site's application directory (not the /public directory). The command to install a package is:
[server]$ npm install <package_name>
For example, this installs a packages called 'lodash'.:
[server]$ npm install lodash /home/username/example.com └── lodash@4.17.4 npm WARN enoent ENOENT: no such file or directory, open '/home/username/example.com/package.json' npm WARN example.com No description npm WARN example.com No repository field. npm WARN example.com No README data npm WARN example.com No license field.
This creates a directory in your site's application directory called /node_modules.
You should then check to confirm it was correctly installed. You must be in your site's application directory where the local /node_modules directory resides:
[server]$ ls node_modules lodash
Testing your locally installed package
This example is taken from docs.npmjs.com.
Create a file named index.js, with the following code:
// index.js
var lodash = require('lodash');
var output = lodash.without([1, 2, 3], 1);
console.log(output);
Run the file using node index.js. It should output [2, 3].
[server]$ node index.js [ 2, 3 ]
If the module was not correctly installed, an error would be thrown.
Creating a packages.json file for locally installed packages
It's a good idea to create a local packages.json file in your site's application directory where the /node_modules directory resides. This package.json file helps to manage your locally installed packages.
To be clear, this package.json file is different than the package.json file within any locally installed package. For example:
Local site package.json file location: /example.com/package.json
Locally installed package package.json file: /example.com/node_modules/<module_name>/package.json
Global package installation
Use the -g flag to install a package globally:
[server]$ npm install -g <package_name>
For example, this installs a packages called 'jshint':
[server]$ npm install -g jshint /home/username/.nvm/versions/node/v6.10.0/bin/jshint -> /home/username/.nvm/versions/node/v6.10.0/lib/node_modules/jshint/bin/jshint /home/username/.nvm/versions/node/v6.10.0/lib └─┬ jshint@2.9.4 ├─┬ cli@1.0.1 │ └─┬ glob@7.1.1 │ ├── fs.realpath@1.0.0 │ ├─┬ inflight@1.0.6 │ │ └── wrappy@1.0.2 │ ├── inherits@2.0.3 │ ├── once@1.4.0 │ └── path-is-absolute@1.0.1 ├─┬ console-browserify@1.1.0 │ └── date-now@0.1.4 ├── exit@0.1.2 ├─┬ htmlparser2@3.8.3 │ ├── domelementtype@1.3.0 │ ├── domhandler@2.3.0 │ ├─┬ domutils@1.5.1 │ │ └─┬ dom-serializer@0.1.0 │ │ ├── domelementtype@1.1.3 │ │ └── entities@1.1.1 │ ├── entities@1.0.0 │ └─┬ readable-stream@1.1.14 │ ├── core-util-is@1.0.2 │ ├── isarray@0.0.1 │ └── string_decoder@0.10.31 ├── lodash@3.7.0 ├─┬ minimatch@3.0.3 │ └─┬ brace-expansion@1.1.6 │ ├── balanced-match@0.4.2 │ └── concat-map@0.0.1 ├── shelljs@0.3.0 └── strip-json-comments@1.0.4
As you can see from the output, global modules are installed under your user in the following directory:
/home/username/.nvm/versions/node/v6.10.0/lib/node_modules
You can confirm which packages are installed globally by running the following:
[server]$ npm list -g --depth=0 /home/username/.nvm/versions/node/v6.10.0/lib ├── jshint@2.9.4 └── npm@4.4.1