How to fix cross-env is not recognized as an internal or external command

In this Laravel tutorial, we will discuss on how to solve the error ‘cross-env is not recognized as an internal or external command, operable program or batch file’.

Firstly, you should open your terminal or command prompt in your project’s root directory and run the following command.

npm install --save-dev cross-env.

The command above will download cross-env package in your Laravel application.

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

Secondly, in your terminal or command prompt which points to the root directory of Laravel application ( package.json file location). You should pass the following commands to install cross-env which we download through above 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 commands above will add the cross-env package to the development dependencies of your project.

As a result, installing a cross-env package as development dependency in you project makes you control your package’s version inside the package.json file.

Alternatively, We can also define commands in the script object of our package.json file.

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

Here npm will resolve cross-env from your node_modules directory as you run the npm install --save-dev cross-env command.

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

If the error is still 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

Note: Restart your IDE and development server is error is still there. VS Code editor often glitches and needs a robot.

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

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

Therefore, 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.

Conclusion

To conclude, to solve the error “cross-env is not recognized as an internal or external command, operable program or batch file”, you should your terminal in your project’s root directory and install the cross-env package by running npm install --save-dev cross-env.

Leave a Comment