21
How to install npm packages?
Installing dependencies is a core part of working with any Node.js project.
Before an application can run, you need to install all existing dependencies from the package.json
file. You can also add new packages as dependencies or devDependencies.
The most common way to interact with the npm CLI is through installing packages with npm install <package>
command from npmjs.com. When you check out a new project repository and try to start the application with npm start
, you will likely get this error - Error: Cannot find module
. This means, you forgot to install the project's dependencies.
To install a project's dependencies, navigate in the root folder of the application, and run:
npm install
You'll see a progress bar as npm begins downloading the dependencies, and a node_modules/
folder will be created in the project's root directory. A package-lock.json
file will also be created, if none existed in the project already. Check out the article - What is package-lock.json, if you are not familiar with this file.
Basically running npm install
(with no arguments) reads the list of dependencies from the package.json
with the applicable package versions and version locks(package-lock.json
), downloads the dependencies, and puts them into the node_modules/
folder. Once the dependencies are added into the node_modules
folder, they are found by the application.
Most npm commands have an alias, so you don't have to type the entire command. Just type npm i
and npm install
will be executed.
The command npm install
can also be used to add new dependencies. You can find packages on npmjs.com. Just run npm install <package-name>
to add the new dependency. For example, to install the popular Express server framework as a dependency:
npm install express
After running the command npm install express
, you will see a progress bar as npm fetches the package. The package will be downloaded from NPM, installed to your node_modules/
folder, and added to the dependencies in package.json
. The package-lock.json
file also updated, which will happen whenever you add or update dependencies. If express
is added to a fresh package.json, after npm init
, the package.json
might look like this:
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
}
}
When installing a package we can choose to record it in the package.json
as an entry to dependencies or devDependencies. You can learn more about the difference between dependencies
and devDependencies
in the article - what is package.json. To sum up, dependencies
are used during production, and devDependencies
are used only in development or during a build step.
By default, npm install
adds the package as an entry to dependencies
, when the flag --save-dev
, or it's alias -D
, is added, the package will be installed as a devDependency
.ESLint
would be a typical devDependency.
npm install --save-dev eslint
After running this command, an entry should be added to devDependencies
in the package.json:
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
},
"devDependencies": {
"eslint": "^7.28.0"
}
}
You can also install packages globally. This will be covered in a separate article, since there can be errors, like EACCES
, and npx
is a great addition to run an arbitrary command from an NPM package. Maybe you don't need to install a package globally, since it can only be used with one installed Node.js version (another Node.js version, would require a new global install).
- Installing dependencies is a core part of working with any Node.js project.
- Install the project dependencies listed in the
package.json
withnpm install
. - Some NPM cli commands have an alias (
npm i
fornpm install
). - Install a new dependency with
npm install <package>
. - Install a new devDependency by passing the flag
--save-dev
, likenpm install <package> --save-dev
.
Thanks for reading and if you have any questions , use the comment function or send me a message @mariokandut.
If you want to know more about Node, have a look at these Node Tutorials.
21