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

avatar

Borislav Hadzhiev

Tue Mar 08 20222 min read

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

To solve the error "cross-env is not recognized as an internal or external command, operable program or batch file", open your terminal in your project's root directory and install the cross-env package by running npm install --save-dev cross-env.

Open your terminal in your project's root directory (where your package.json file is located) and run the following command:

shell
# 👇️ installs cross-env locally npm install --save-dev cross-env # 👇️ installs cross-env globally (can run from any directory) npm install -g cross-env # ---------------------------------------------- # 👇️ installs cross-env locally yarn add cross-env --dev # 👇️ installs cross-env globally (can run from any directory) yarn add cross-env --global

The command will add the cross-env package to the development dependencies of your project.

The benefit of installing cross-env as a development dependency is that you can control the version of the package in your `package.json` file.

You can also create commands in the scripts object of your package.json file.

package.json
{ "scripts": { "build": "cross-env NODE_ENV=production webpack --config build/webpack.config.js" } }

This works because npm will resolve cross-env from your node_modules directory because you ran npm install --save-dev cross-env.

Now you would run the command as npm run build, and not use cross-env directly.

If the error is not resolved, try to delete your node_modules and package-lock.json (not package.json) files, re-run npm install and restart your IDE.

shell
# 👇️ delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json # 👇️ clean npm cache npm cache clean --force npm install
Make sure to restart your IDE and dev server if the error still persists. VSCode often glitches and a reboot solves things sometimes.

Alternatively, you solve the error by prefixing the command with npx.

shell
# 👇️ prefix with npx npx cross-env NODE_ENV=production webpack --config build/webpack.config.js

The npx prefix will look for the cross-env package in your local dependencies and if it's not found, it will install the package before running the command.

Use the search field on my Home Page to filter through my more than 1,000 articles.