Commonly Used npm Flags
-g (global)
-
Cmd:
npm install -g <package-name>
- Usage: Installs the package globally on your system, making it available from any directory. This is useful for tools and utilities you want to use across different projects.
-
Example:
npm install -g create-react-app
allows you to use the create-react-app command from anywhere on your machine.
-p (prefix)
-
Cmd:
npm install -p <directory> <package-name>
-
Usage: Installs the package to a specific directory rather than the default
node_modules
in the current project. This can be used to manage packages for different environments or users. -
Example:
npm install -p /usr/local <package-name>
installs the package to /usr/local.
-d (save-dev)
-
Cmd:
npm install -D <package-name>
ornpm install --save-dev <package-name>
- Usage: Adds the package as a development dependency. Development dependencies are only needed during development and testing, not in production.
-
Example:
npm install -D jest
adds jest to your devDependencies inpackage.json
{
"dependencies": {
"express": "^4.17.1"
},
"devDependencies": {
"jest": "^27.0.6"
}
}
Other Common npm Flags
--save(-S)
-
Cmd:
npm install -S <package-name>
ornpm install <package-name>
-
Usage: Adds the package as a dependency in your
package.json
. This is the default behavior when you runnpm install <package-name>
-
Example:
npm install express
will add express to the dependencies inpackage.json
--save-optional (-O)
-
Cmd:
npm install -O <package-name>
- Usage: Adds the package as an optional dependency. These dependencies are not critical for the project to function but can add optional features.
-
Example:
npm install -O fsevents
adds fsevents to optionalDependencies.
--save-exact (-E)
-
Cmd:
npm install -E <package-name>
- Usage: Installs the exact version specified without updating to newer versions.
-
Example:
npm install -E lodash
installs the exact version listed, e.g.,lodash@4.17.21
--production
-
Cmd:
npm install --production
- Usage: Installs only the dependencies listed under dependencies and skips devDependencies. Useful for deployment.
-
Example:
npm install --production
ensures only necessary packages for running the app are installed.
Summary
- -g: Global installation
- -p: Specify installation directory
- -d: Development dependencies
- --save: Default dependency installation (often implicit)
- -O: Optional dependencies
- -E: Exact version installation
- --production: Production-only installation
These flags help configure your environment and dependencies according to your project needs.
Thank You!!!
Top comments (0)