NPM install flags explained (with examples)

avatar
Borislav Hadzhiev

Last updated: Feb 27, 2023
3 min

banner

# NPM install flags explained (with examples)

This article goes over some of the most commonly used NPM install flags and their shorthands.

The --save flag is set to true by default, so it doesn't have to be specified in newer NPM versions.

shell
npm install express --save

using save flag

When the --save flag is used, the package you are installing appears in the dependencies object of your package.json file.

The flag can also be specified using the shorthand of -S.

shell
npm install express -S

# The --save-dev flag

When the --save-dev flag is used, the installed package appears in the devDependencies section of your package.json file.

shell
npm install nodemon --save-dev

using save dev flag

The flag is used when installing development dependencies, e.g. a server that is only used during development or test frameworks.

The flag can also be specified using the shorthand of -D.

shell
npm install nodemon -D

# The --global flag

The global flag is used to install a package globally.

shell
npm install --global create-react-app

using global flag

When you install a package globally, it can be accessed directly from the command line.

shell
create-react-app --version

The flag can also be specified using the shorthand of -g.

shell
npm install -g create-react-app

# The --silent flag

The --silent flag can be used to suppress the output from the npm install command.

shell
npm install --silent express

using silent flag

When the --silent flag is used, the log level of npm is set to silent and no output is produced.

The flag can also be specified using the shorthand of -s.

shell
npm install -s express

# The --save-exact flag

When the --save-exact flag is used, the installed dependencies are saved to package.json with an exact version rather than using npm's default semver range operator.

shell
npm install --save-exact express

using save exact flag

when you use the flag, you are guaranteed to install the same major, minor and patch version every time you run npm install.

The flag can also be specified using the shorthand of -E.

shell
npm install -E express

# The --dry-run flag

When the --dry-run flag is used, npm doesn't make any changes to your package.json file.

shell
npm install react --dry-run

using dry run flag

Instead, it only reports what it would have done had the command been issued without the flag.

# The --force flag

When the --force flag is used, npm fetches remote resources even if a local copy exists on disk.

shell
npm install react --force

using force flag

The flag can also be specified using the shorthand of -f.

shell
npm install react -f

# The --legacy-peer-deps flag

The --legacy-peer-deps flag ignores all peer dependencies when installing (in the style on npm version 4 through 6).

shell
npm install react --legacy-peer-deps

using legacy peer deps flag

The flag is often used when you get errors when installing a package due to it clashing with other packages.

The issue mainly occurs when multiple packages depend on different versions of another package.

# The --strict-peer-deps flag

When --strict-peer-deps is set to true and --legacy-peer-deps is not set, any conflicting peerDependencies are treated as an install failure.

shell
npm install react --strict-peer-deps

using strict peer deps flag

By default, npm tries to resolve conflicting peerDependencies.

# The --package-lock flag

When the --package-lock flag is set to true, the package-lock.json file is ignored when installing.

shell
npm install react --package-lock

using strict peer deps flag

The command also prevents npm install from writing to package-lock.json.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.