NPM get started

Get started with NPM, the Node.js package manager.

Local vs Global module installation

Whenever you do npm install <module> you are installing a module locally. If you add the -g flag, npm install -g <module>, you are installing it globally (sudo required).

1
2
$ npm install <module>    // locally to the project
# npm install -g <module> // globally to the system

How works the require('module') statement?

If you give it a path, then it search it on the path given. So if you write something like this:

1
const foo = require('./my-modules/foo');

You are telling Node.js to look for the file ./my-modules/foo.js.

If you give it a module name, then it search it on the ./node_modules/ directory of the project, if it is not there, it will be searched on the global system modules.

1
const passport = require('passport');

Node.js will look into ./node_modules/passport/ first, if not found it, it will look into the global system modules.

The file package.json and dependencies

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"name" : "foo-project",
"version" : "0.3.2",
"description" : "Foo Project",
"dependencies": {
"module-a": "1.x.x",
"module-b": "1.x.x",
"module-c": "1.x.x",
"module-d": "1.x.x",
"module-e": "1.x.x"
},
"scripts": {
"start": "node app.js",
"test" : "mocha --reporter spec --require test/*.js "
}
}

It’s common to not upload the whole dependencies along your GitHub project, instead you should put them on the package.json. If you make it you are letting know other users which dependencies are being used by your project. The easy and usual way to install them together is execution npm install where your package.json file is.

So if an user clones your repository, he/she only needs to do this:

1
2
3
$ git clone https://github.com/youruser/yourrepo
$ cd yourrepo
$ npm install

Saving dependencies on package.json file

If you add the flag --save when you install a module as local to your project, npm will add a line in your package.json dependencies section with the new module.

Before install passport

1
2
3
4
5
6
7
8
9
10
11
12
{
"name" : "foo-project",
"version" : "0.3.2",
"description" : "Foo Project",
"dependencies": {

},
"scripts": {
"start": "node app.js",
"test" : "mocha --reporter spec --require test/*.js "
}
}

After install passport

1
$ npm install passport --save
1
2
3
4
5
6
7
8
9
10
11
12
{
"name" : "foo-project",
"version" : "0.3.2",
"description" : "Foo Project",
"dependencies": {
"passport": "^1.2.4"
},
"scripts": {
"start": "node app.js",
"test" : "mocha --reporter spec --require test/*.js "
}
}

Removing passport

1
$ npm uninstall passport --save
1
2
3
4
5
6
7
8
9
10
11
12
{
"name" : "foo-project",
"version" : "0.3.2",
"description" : "Foo Project",
"dependencies": {

},
"scripts": {
"start": "node app.js",
"test" : "mocha --reporter spec --require test/*.js "
}
}

Scripts section into package.json

You can write some common commands inside scripts section. In the example:

1
2
3
4
5
6
7
8
9
10
11
12
13
{
"name" : "foo-project",
"version" : "0.3.2",
"description" : "Foo Project",
"dependencies": {

},
"scripts": {
"start": "node app.js",
"test" : "mocha --reporter spec --require test/*.js ",
"foo" : "echo 'hello'"
}
}

You can run any script of the scripts section with npm run <script_name>. There is a exception, the common names start and test no need to add run. So you can do npm start and npm test as well.

In the above package.json file you may do the following commands:

1
2
3
$ npm start   // Runs "node app.js"
$ npm test // Runs "mocha --reporter spec --require test/*.js "
$ npm run foo // Runs "echo 'hello'"

References