← ./articles

npm install Skips devDependencies? Check NODE_ENV=production

npm install --save-dev appears to succeed, but the package is not available in node_modules.

For example:

npm install --save-dev cross-env
npm run dev

Then the script fails:

'cross-env' is not recognized as an internal or external command

Before clearing npm cache or deleting the whole project setup, check whether NODE_ENV=production is being inherited by npm. When NODE_ENV is production, npm can omit devDependencies.

Symptom: --save-dev succeeds but the command is missing

The pattern usually looks like this:

  • npm install --save-dev <package> exits without an obvious error
  • package.json may list the package under devDependencies
  • node_modules/.bin/<command> is missing
  • npm run dev or npm run build cannot find the command
  • the same steps work in another terminal, machine, or CI job

This does not necessarily mean the package is broken. npm may have skipped the dev dependency set.

Cause: npm inherited NODE_ENV=production

Node.js tools often use NODE_ENV to select behavior.

If the shell or parent process has NODE_ENV=production, npm inherits it:

NODE_ENV=production
  -> npm install
  -> devDependencies omitted

This can happen even if you did not pass a --production flag. The value may come from a previous command, an IDE terminal, a CI environment, or an AI coding tool process.

Check NODE_ENV first

In PowerShell:

$env:NODE_ENV

If it prints production, child processes from that terminal will inherit production mode.

Also check persistent user and machine values:

[Environment]::GetEnvironmentVariable("NODE_ENV", "User")
[Environment]::GetEnvironmentVariable("NODE_ENV", "Machine")

If either value is production, reopening the terminal may not fix the issue.

In Bash or Git Bash:

echo "$NODE_ENV"

Recover with --include=dev

To force npm to install devDependencies:

npm install --include=dev

If you need to install a new dev dependency, set the environment for the session first:

$env:NODE_ENV = "development"
npm install --save-dev cross-env

If cross-env is already available, you can also force the command environment:

npx cross-env NODE_ENV=development npm install --include=dev

Do not rely on this if cross-env itself is the missing package. In that case, fix the shell environment first.

Make dev scripts explicit

Development scripts should not depend on whatever the parent shell happens to contain.

For Next.js:

{
  "scripts": {
    "dev": "cross-env NODE_ENV=development next dev",
    "build": "next build"
  }
}

This prevents next dev from starting with production assumptions inherited from the terminal.

Do not start with cache deletion

Avoid starting with destructive or broad cleanup:

npm cache clean --force
Remove-Item -Recurse node_modules
Remove-Item package-lock.json

If NODE_ENV=production is still set, reinstalling will reproduce the same problem.

Use this order instead:

  1. Check $env:NODE_ENV or echo "$NODE_ENV".
  2. Check User and Machine environment variables.
  3. Run npm install --include=dev.
  4. Confirm node_modules/.bin/<command> exists.
  5. Run the failing script again.

It can share a root cause with Next.js dev CSS errors

NODE_ENV=production pollution can affect more than npm install.

In Next.js, next build may pass while next dev fails with a CSS parse error because the dev server inherited production mode.

Different symptoms can have the same root:

NODE_ENV=production
  -> npm install skips devDependencies
  -> next dev starts with wrong assumptions

If npm install and next dev both behave strangely, inspect the environment variable before changing code.

Related: Why only next dev breaks CSS: it was NODE_ENV=production

If the node_modules directory itself is being replaced or corrupted under OneDrive, that is a different failure mode. See Node.js on OneDrive: Move node_modules with a Windows Junction.

Checklist

Check these in order:

  • $env:NODE_ENV or echo "$NODE_ENV" is not production
  • User or Machine environment variables do not pin NODE_ENV=production
  • npm install --include=dev was run after fixing the environment
  • node_modules/.bin/<command> exists
  • the dev script explicitly sets development mode when needed
  • npm and next dev failures are not both caused by the same inherited variable

Summary

If npm install --save-dev appears to succeed but devDependencies are missing, check NODE_ENV=production before changing packages or clearing caches.

The fix is usually to correct the environment and reinstall with npm install --include=dev. Once you identify the environment layer, the repair is small and repeatable.