Be the first user to complete this post
|
Add to List |
Installing, Listing and Uninstalling packages using npm
npm install
and npm update
commands. However, you ought to know a little bit more about the different options available to you during package install.
Installing packages using npm
When I first began, i just installed packages using the command
npm install packagename
This way, the next time you clone your package, or delete your node_modules folder and then issue the {
dependencies: {
"express": "4.0.1"
}
}
npm install express --save
npm install
command, npm will install exact same versions of your packages that were specified in your package json therefore preventing your code from breaking due to updates to packages.
The next interesting happens when you realize that you actually have two kinds of packages - ones that are required for your applications to run and others that you use as tools that help you during development - such as jshint, grunt, grunt tasks, minification, sass compilers etc etc.
Since these packages are only required by the developer, they dont need to be installed by someone else that might use your package in the future. The reason why it is important to make this distinction is because when someone else who mentions your package as dependency in their project issues an npm install
, npm will also go ahead and install all the packages specified in your 'dependencies'. npm cannot distinguish between mandatory packages and development-only packages. Well at least not unless you tell it to.
The correct way to specify development only packages is by listing them in your package.json under the key 'devDependencies'. And just like before, you don't need to remember the package version numbers of these packages. You can install them by simply running the npm install command with the --save-dev option as shown
npm install grunt --save-dev
Listing installed packages using npm
If at any point you want to see the list all the packages that are installed and their dependencies, you can do that by the command
npm list
npm list --depth=0
npm list -g --depth=0
Uninstalling packages using npm
If at any time, you want uninstall a module, you can do that by the command
npm uninstall packagename
Removing unused packages from node_modules
Sometimes after installing packages, you realize that you dont really need some of them and you delete those entries from your package.json. Although those packages will not be installed again on running an npm install, you still need to remove them from your node_modules folder at least once. To remove all such unused packages from your node_modules, you can run the command
npm prune
Removing all devDependencies from node_modules
If you just want to delete all of your installed devDependencies while retaining their entry in the package.json file, you can do that using the command
npm prune --production
Also Read:
- Debugging nodejs applications using node-inspector and Chrome Dev Tools
- set the default node version using nvm
- How to publish a package on npm
- Setup nginx with multi domain websites running on nodejs
- Unit test your Nodejs RESTful API using mocha