Borislav Hadzhiev
Tue Mar 08 2022·2 min read
Photo by Karina Carvalho
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:
# 👇️ 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.
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.
{ "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.
# 👇️ 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
Alternatively, you solve the error by prefixing the command with npx
.
# 👇️ 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.