In this beginner guide to NPM, we will cover what exactly it is, how it works & a couple of commonly used NPM commands which you can start using right away in your projects.
What is NPM?
NPM is basically a Javascript package manager which allows you to quickly add & manage any available package /module/dependency in your project with just a couple of commands.
With NPM, you don't have to go manually downloading, installing & setting up the package. NPM take cares of everything so that you can focus on your project.
NPM official website
NPM has an official website which you should definitely bookmark. On the website, you can find all the npm packages available for you to use. The packages also have great documentation which you can follow on how to install and use in your project.
Not just that but if you wish, you can also publish and share your own packages on this platform which then will be available for anyone to use.
You can visit the official website here - www.npmjs.com
How to access NPM?
Working with npm is super easy, all you need is to make sure that Node JS is installed in your system. NPM comes pre-installed with Node JS so you don't have to worry about installing it manually. Once you've successfully installed Node JS, you can simply use any command-line interface like a terminal to run npm commands, install npm packages from the npm registry and start using npm in your projects.
Important NPM files in your project
If you are using npm in your project then there are two very important files associated with npm that you should know about.
They are:
-
node_modules
- It's a directory automatically created in the root folder of your project which will contain all the npm packages you install locally. One more thing you should note here that this directory will be automatically ignored by git and will not appear in your project's repo. -
package.json
- It is a file that is again automatically created in the root directory of your project which contains metadata about the packages you have installed.
Both of these files are created automatically & are managed by npm so you don't have to worry about them until you're experienced enough to manually work on them.
Important NPM Commands
npm init
This command initializes npm in your project and will automatically create the package.json file.
npm init -y
This command is simply an alteration of npm init
which just skips a few unnecessary steps and creates the package.json file quickly.
npm install xyz
This command will install the specified package locally and add it into the package.json as a dependency.
npm install -g xyz
This command is again an alteration of the npm install
command which will install the specified package but on a system-wide or global scale.
npm update xyz
This command is used to update a specified package to its most recent release available.
npm update
This command is used to update all the packages present in the package.json file at once to their most recent versions available.
npm list
This command lists all the packages installed in your system.
npm uninstall xyz
This command is used to uninstall the specified package which you no longer need.
npm uninstall xyz -g
This command will uninstall the specified package globally.
Support
Thank you so much for reading! I hope you learnt something new today. Please leave a like, a lovely comment or feedback & also consider following me as I regularly post useful & informative articles to help you become a better programmer.
If you like my work please consider Buying me a Coffee so that I can bring more projects, more articles for you.
Connect with me
If you have any questions or doubts feel free to contact me on Twitter, LinkedIn & GitHub. Or you can also post a comment/discussion here & I will try my best to help you :D