What is NPM
npm is a package manager for Node.js with hundreds of thousands of packages. Although it does create some of your directory structure/organization, this is not the main purpose.
The main goal, as you touched upon, is automated dependency and package management. This means that you can specify all of your project's dependencies inside your package.json file, then any time you (or anyone else) needs to get started with your project they can just run npm install and immediately have all of the dependencies installed. On top of this, it is also possible to specify what versions your project depends upon to prevent updates from breaking your project.
It is definitely possible to manually download your libraries, copy them into the correct directories, and use them that way. However, as your project (and list of dependencies) grows, this will quickly become time-consuming and messy. It also makes collaborating and sharing your project that much more difficult.
Do those stuff is repetitive and someone who needs to focus on the business cannot spend a lot of time building infrastructure pieces of code. And when I said pieces doesn’t mean that those are small tasks. A Database driver, for example, is a huge project per se. As the same for many available packages to create a JS application.
Why use NPM?
Can you imagine looking on many websites, from different projects for parts to compose your project? NPM centralizes the search of those parts, takes care of versions and provides the correct download of dependencies. YES, a package could depend on another, that depends on another…
When you are ordering a meal, do you order each ingredient? And do you know the exact way to prepare it? NPM is the JS Waiter.
NPM has two important things to mention here:
2- it is a CLI to interact with that repository
How to use it?
NPM is part of Node.js, it means that npm Comand Line Interface will be available at the terminal/console when the Node.js is installed.
To install Node.js, follow the instructions on
The basic commands that every Node developer needs to know are to initialize, install packages, uninstall packages.
In the project-folder is necessary to create the link between the project in the local computer and the global repository.
For this the command is:
> npm init
This command will create the package.json file with some information:
The project’s name The project’s initial version The project’s description The project’s entry point (meaning the project’s main file) The project’s test command The project’s git repository The project’s keywords The project’s license
Initially, the file looks like this:
It’s possible to edit the file, but it’s not necessary. The CLI npm was developed to manage this file.
After the initialization, it’s time to install the packages/modules that we want to aggregate to the project. For example, if it’s planned for that Node project to be a web application, the most famous package to install is “express”. For that the command is:
> npm install express
This command will install the express module in the project, including as a dependency in the package.json file and downloading the required files.
Then, at this point, the module is ready to be used in the JS files in the project.
Each module has its way to be used but in most the cases is included using the command ‘require’: