Where does npm install node modules
If you are new to npm (node package manager) then you must have been wondering that where does npm install node modules. npm install module locally and globally.
Installing a node module locally means that this module will be needed by the current project only. when we install a module locally then npm downloads files from npm server and puts them inside the node_modules folder of your current project.
Creating project directory
mkdir todo-app cd todo-app
Now you will try to install a node module called express from npm
npm install express --save
You will see that when the installation gets completed then a folder with name node_modules gets created inside todo-app folder. Now if you look inside node_modules you would find so many folders but if you look carefully then you can find a folder named express. You must have noticed that you installed only one express module but when you look inside node_modules folders then you found many other modules also got downloaded. The reason behind this is
- express itself depend upon other modules
- so when you install a module locally then npm downloads all the dependencies for that particular module and puts them inside same node modules folder
Installing a module globally means that all your node projects can use this module. when we install a module globally then npm downloads the file from npm server and puts then inside /usr/lib/node_modules. since /usr/lib lies in the system path variable so any node project can find the modules installed globally.
when you install a module globally then you need to pass a -g flag to npm command line tool so that npm can understand that the current module under installation needs to be installed globally
Now you will try to install the express module globally.
npm -g install express
Now go to the path where node modules are installed globally
You will see the express folder. But this time you noticed that only express module is visible and not the dependencies of express. But if you see inside the node_modules folder of express then you can see all the dependencies of express module
cd express cd node_modules ls