Get started with NPM, the Node.js package manager.
Whenever you do
npm install <module> you are installing a module locally. If you add the
npm install -g <module>, you are installing it globally (sudo required).
$ npm install <module> // locally to the project
If you give it a path, then it search it on the path given. So if you write something like this:
const foo = require('./my-modules/foo');
You are telling Node.js to look for the file
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.
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.
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:
$ git clone https://github.com/youruser/yourrepo
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.
$ npm install passport --save
$ npm uninstall passport --save
You can write some common commands inside scripts section. In the example:
You can run any script of the scripts section with
npm run <script_name>. There is a exception, the common names
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:
$ npm start // Runs "node app.js"