Did you know there is more you can do with NPM(Node Package Manager) than just installing your project’s dependencies? Well in this article we’re going to explore some of npm’s commands which you may not have heard of. So grab a cup of coffee or whatever you drink and let’s dive right in.
Installing packages
npm install
or
npm i
This command is used to install all the packages that are listed in your package.json file. If you just downloaded a new javascript-based project, this is most likely the command you will use.
npm i lodash
This will install the latest version of a dependency called ‘lodash’ into your project.
npm i lodash@1.3.1
You can specify what version of a dependency you wish to install by adding the version number after the ‘@’ symbol.
npm i <package_name> --save-dev
or
npm i -D <package_name>
When you need a package just for development, this is the command you use so that in your package.json file it will be saved under the ‘devDependencies’ section.
This is mostly useful for packages used in linting, testing, creating a live server, etc because your project doesn’t really need these packages to function but they help in the development process.
npm i -g <package_name>
When installing packages, the “-g” flag is used to specify that it is a global package. This is mostly used to install CLI tools that can be accessed from any directory on your computer.
Uninstalling packages
npm uninstall <package_name>
or
npm un <package_name>
Use this to uninstall a particular package.
npm un -g <package_name>
For uninstalling global packages.
Listing your project’s dependencies
npm list
You can view all the dependencies in a project by using this command.
npm list --depth 1
To view the dependencies your packages are dependent on.
npm list -g
To view all your global packages.
Updating NPM packages
npm outdated
This shows a list of all your outdated packages.
npm update
This will update your outdated packages.
N/B: This only updates your package to the latest minor or patch versions but not to the latest major version e.g 1.3.1 cannot be updated to 2.0.0. By the way, I will be releasing an article on how ‘semantic’ versioning works in NPM so you may want to check out my blog for that.
BONUS!
npm view <package_name>
With this command, you will be able to view all the information about a particular package that is on the NPM registry.
npm view <package_name> versions
If you only want to view all the versions of a package.
npm view <package_name> dependencies
For viewing just the dependencies of a package.
This is all I have for you today, I hope you learned something new and I’m looking forward to seeing you in the next post
CIAO!!