npm install doesn't install devDependencies issue [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 25, 2023
6 min

banner

# Table of Contents

  1. npm install doesn't install devDependencies issue
  2. Verify your package.json file is syntactically correct
  3. Make sure you haven't set the production key in your NPM config
  4. Try running the installation command with the --save-dev flag
  5. Make sure your NODE_ENV environment variable is not set to production
  6. Try to delete your node_modules and package-lock.json files
  7. Only installing your dependencies with the --omit flag

# npm install doesn't install devDependencies issue [Solved]

Set the --production flag to false when issuing the npm install command to resolve the issue where npm install doesn't install your devDependencies.

When the --production flag is set to false, npm will install all modules listed in both dependencies and devDependencies.

shell
npm install --production=false

npm install production false

You should also try to run the npm install command with the --include flag set to dev.

shell
npm install --include=dev

npm install include dev

Setting the --also flag to dev might also help.

shell
# for older NPM versions npm install --also=dev

npm install also dev

The most common reasons for npm not installing your devDependencies are:

  1. When the --production flag is set to production, npm won't install modules listed in your devDependencies object.
  2. When the NODE_ENV environment variable is set to production, npm won't install modules listed in your devDependencies.

In order versions of npm, you were able to only install devDependencies by setting the --only flag to dev

shell
npm install --only=dev

npm install only dev

You can try running the command, however, in more recent versions, the available options are null, prod and production.

# Verify your package.json file is syntactically correct

If you have syntactical errors in your package.json file, then the npm install command might not work as expected.

Make sure you have run the npm init -y command and have initialized a package.json file in the root directory of your project.

shell
npm init -y

The npm init -y command won't override your existing package.json file if you already have one.

You can also change to another directory and initialize a new package.json file by running the npm init -y command to check if your existing file has syntactical errors.

You can install a couple of modules manually and update your original package.json file.

shell
# install multiple modules as dev dependencies npm install MODULE_A MODULE_B --save-dev

If you need to install a specific version of a module, use the @ symbol.

shell
npm install MODULE_A@1.2.3 --save-dev

# Make sure you haven't set the production key in your NPM config

Another thing to check is that you haven't set the production key in your NPM config.

shell
npm config get production

check if production key set

If the production key is set to true, then npm won't install the modules listed in your devDependencies.

You can disable the option by setting the key to false.

shell
npm config set production false

Make sure to set the key to false in your global NPM config as well

shell
npm config set --global production false

You can verify that the flag has been set to false by issuing the following commands.

shell
# for user-specific NPM config npm config get production # for global NPM config npm config --global get production

You should either get a value of false or null when issuing the commands above.

You can also use the npm config list command to print your NPM configuration keys and values.

shell
npm config list

# Try running the installation command with the --save-dev flag

If the issue persists, try to run the npm install command with the --save-dev flag.

shell
npm install --save-dev

npm install save dev

You can also use the alias -D which achieves the same result.

shell
npm install -D

npm install d

If you also have yarn installed, you can run the yan install command with the --dev flag.

shell
yarn install --dev

yarn install dev

If you need to install a specific module as a dev dependency, specify the module when issuing the command.

shell
npm install axios --save-dev # same as above npm install axios -D

Make sure to replace axios with the name of the package you're trying to install as a development dependency.

The same can be achieved when using yarn.

shell
yarn add axios --dev

When the --save-dev (or -D) NPM flag or the --dev flag of yarn is specified, the package is added to the devDependencies object of your package.json file.

# Make sure your NODE_ENV environment variable is not set to production

If your NODE_ENV environment variable is set to production, then NPM won't install the modules listed in your devDependencies.

You can check the value of your NODE_ENV environment variable by issuing the following command on macOS or Linux.

shell
# ๐Ÿ‘‡๏ธ Linux and macOS echo $NODE_ENV

If you are on Windows and use CMD, issue the following command instead.

cmd
# ๐Ÿ‘‡๏ธ Windows with CMD echo %NODE_ENV%

If you are on Windows and use PowerShell, run the following command.

PowerShell
# ๐Ÿ‘‡๏ธ on Windows with PowerShell echo $Env:NODE_ENV

If your NODE_ENV variable is set to production or prod, you can unset it by running the following command on macOS or Linux.

shell
# on macOS and Linux unset NODE_ENV

If you are on Windows and use CMD, run the following command instead.

shell
# on Windows with CMD set NODE_ENV=

If you are on Windows and use PowerShell, run the following commands.

PowerShell
[Environment]::SetEnvironmentVariable('NODE_ENV', '', 'User') [Environment]::SetEnvironmentVariable('NODE_ENV', '', 'Machine')

Try to rerun the npm install command after clearing your NODE_ENV environment variable.

If you don't want to change the value of your NODE_ENV environment variable, run the npm intsall command with the --production flag set to false.

shell
npm install --production=false

npm install production false

# Try to delete your node_modules and package-lock.json files

The issue is sometimes caused when your node_modules directory and package-lock.json files are created using an older version of npm.

One thing you can try is to delete the node_modules folder and the package-lock.json file and rerun the npm install command.

If you are on Windows, open your terminal in your project's root directory (where your package.json file is) and run the following commands in CMD.

shell
# for Windows rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐Ÿ‘‡๏ธ clean npm cache npm cache clean --force # ๐Ÿ‘‡๏ธ install packages npm install # ๐Ÿ‘‡๏ธ install packages npm install --save-dev

If you are on macOS or Linux, run the following commands in bash or zsh.

shell
# for macOS and Linux rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ๐Ÿ‘‡๏ธ clean npm cache npm cache clean --force # ๐Ÿ‘‡๏ธ install packages npm install # ๐Ÿ‘‡๏ธ install packages npm install --save-dev

Check whether your development dependencies have been installed successfully.

You can also try to restart your IDE and make sure you haven't hidden your node_modules folder from the left sidebar.

# Only installing your dependencies with the --omit flag

If you only want to install your dependencies (and not your devDependencies), set the --omit flag to dev when running npm install.

shell
# only install dependencies and not devDependencies npm install --omit=dev

The command ignores the value of your NODE_ENV environment variable and will only install your dependencies and not your devDependencies.

If you use an older version of NPM (older than 6.0.0), you have to set the --only option to prod to only install your dependencies.

shell
# for older NPM versions npm install --only=prod

When you run the npm install command without specifying any flags, it installs your dependencies and devDependencies, assuming the NODE_ENV environment variable is not set to production.

shell
# installs both dependencies and devDependencies npm install

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev