26
loading...
This website collects cookies to deliver better user experience
package.json
file. You can also add new packages as dependencies or devDependencies.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.npm install
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.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.npm i
and npm install
will be executed.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
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"
}
}
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.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
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"
}
}
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).package.json
with npm install
.npm i
for npm install
).npm install <package>
.--save-dev
, like npm install <package> --save-dev
.