Laravel Mix "sh: 1: cross-env: not found error"
Asked Answered
J

10

118

I have been trying to set up Laravel Mix in my project and followed the install guide on the Laravel website however keep getting errors.

My package.json

{
  "private": true,
  "scripts": {
    "dev": "cross-env NODE_ENV=development webpack --progress --hide-modules",
    "watch": "cross-env NODE_ENV=development webpack --watch --progress --hide-modules",
    "hot": "cross-env NODE_ENV=development webpack-dev-server --inline --hot",
    "production": "cross-env NODE_ENV=production webpack --progress --hide-modules"
  },
  "devDependencies": {
    "axios": "^0.15.2",
    "bootstrap-sass": "^3.3.7",
    "jquery": "^3.1.0",
    "laravel-mix": "^0.4.0",
    "lodash": "^4.16.2",
    "vue": "^2.0.1"
  },
  "name": "Code",
  "version": "1.0.0",
  "main": "webpack.mix.js",
  "directories": {
    "test": "tests"
  },
  "dependencies": {
    "ansi-regex": "^2.1.1",
    "ansi-styles": "^2.2.1",
    "axios": "^0.15.3",
    "babel-core": "^6.24.1",
    "babel-code-frame": "^6.22.0",
    "babel-generator": "^6.24.1",
    "babel-messages": "^6.23.0",
    "babel-helpers": "^6.24.1",
    "babel-register": "^6.24.1",
    "babel-template": "^6.24.1",
    "babylon": "^6.17.0",
    "balanced-match": "^0.4.2",
    "babel-runtime": "^6.23.0",
    "babel-types": "^6.24.1",
    "babel-traverse": "^6.24.1",
    "brace-expansion": "^1.1.7",
    "bootstrap-sass": "^3.3.7",
    "chalk": "^1.1.3",
    "convert-source-map": "^1.5.0",
    "concat-map": "^0.0.1",
    "core-js": "^2.4.1",
    "cross-env": "^3.2.4",
    "detect-indent": "^4.0.0",
    "esutils": "^2.0.2",
    "escape-string-regexp": "^1.0.5",
    "follow-redirects": "^1.0.0",
    "globals": "^9.17.0",
    "has-ansi": "^2.0.0",
    "home-or-tmp": "^2.0.0",
    "is-finite": "^1.0.2",
    "invariant": "^2.2.2",
    "json5": "^0.5.1",
    "js-tokens": "^3.0.1",
    "jquery": "^3.2.1",
    "jsesc": "^1.3.0",
    "laravel-mix": "^0.4.0",
    "lodash": "^4.17.4",
    "loose-envify": "^1.3.1",
    "mkdirp": "^0.5.1",
    "minimatch": "^3.0.3",
    "minimist": "^0.0.8",
    "number-is-nan": "^1.0.1",
    "os-homedir": "^1.0.2",
    "os-tmpdir": "^1.0.2",
    "path-is-absolute": "^1.0.1",
    "private": "^0.1.7",
    "regenerator-runtime": "^0.10.3",
    "repeating": "^2.0.1",
    "slash": "^1.0.0",
    "source-map": "^0.5.6",
    "source-map-support": "^0.4.14",
    "strip-ansi": "^3.0.1",
    "trim-right": "^1.0.1",
    "to-fast-properties": "^1.0.2",
    "vue": "^2.3.0"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": ""
}

The error I am getting when I run any npm run command.

> [email protected] dev /home/vagrant/Code
> cross-env NODE_ENV=development webpack --progress --hide-modules

sh: 1: cross-env: not found

npm ERR! Linux 4.4.0-51-generic
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "dev"
npm ERR! node v7.8.0
npm ERR! npm  v4.2.0
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! [email protected] dev: `cross-env NODE_ENV=development webpack --progress --hide-modules`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the [email protected] dev script 'cross-env NODE_ENV=development webpack --progress --hide-modules'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the Code package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     cross-env NODE_ENV=development webpack --progress --hide-modules
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs Code
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls Code
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /home/vagrant/.npm/_logs/2017-04-28T17_24_11_458Z-debug.log

I am running my project on a Vagrant box, and I am running Laravel version 5.4.

Jespersen answered 28/4, 2017 at 17:29 Comment(2)
I can confirm that following Laravel 5.4 installing instructions giving you these errors. See my answer for workaround.Stoltzfus
Using Laravel 5.7 with Vagrant on an bento/ubuntu-16.04 box. Since I am using linux and this will carry over into production I just bypassed cross-env and changed the cross-env call with set. Since I can't figure out the bug that seems to creep up with vagrant on my many readings, i figure I don't need this cross platform solution to set environment variables. Therefore just a plain old "set" command does the trick for me. Hopes this helps others in similar situations. i.e: "development": "set NODE_ENV=development node_modules/webpack/bin/webpack.js ... in package.jsonAllegorize
S
191

You need to make cross-env working globally instead of having it in the project.

run

$ sudo npm install --global cross-env

--- update ---

But, my advice is to avoid executing npm run ... on the guest homestead, because it is very slow and because there is no benefit in it.

No matter where you build the assets they are going to be executed in the browser. So you better install npm on your host computer and build the assets there.

Stoltzfus answered 12/8, 2017 at 2:38 Comment(4)
@leo for global packages you always need sudoInsomnolence
@Insomnolence I doubt that, I searched both on Google and SO and I don't see that mentionedSwirsky
This does not solve the problem at all. It is just a workaround. NPM scripts are expected to see the tools installed locally without adding them to global PATH.Penstock
@fracz, you are right. If you'd find a way to fix it, please, post it as an answer here. I'm actually not even trying to run npm run on guest homestead, instead I'm runing it on my host Windows. And it is working fine, because, who cares where is it was built, if it is going to be executed on client side any way.Stoltzfus
S
64

First check if cross-env module is installed. If not, run:

npm install cross-env

After that you need to go to the node_modules folder. Then find cross-env folder. Go inside and find cross-env.js.

In my case it was node_modules/cross-env/dist/bin/cross-env.js

You need to change path to cross-env.js in scripts section in your package.json file.

{
  "private": true,
  "scripts": {
    "dev": "node_modules/cross-env/dist/bin/cross-env.js NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
    "watch": "node_modules/cross-env/dist/bin/cross-env.js NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
    "hot": "node_modules/cross-env/dist/bin/cross-env.js NODE_ENV=development webpack-dev-server --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
    "production": "node_modules/cross-env/dist/bin/cross-env.js NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
  },

........

}
Sanhedrin answered 4/8, 2017 at 22:5 Comment(4)
It did not help me, but voted up, because my problem was in the cross-env, and this answer gave me a hintStoltzfus
You have a small typo (node_modules folder naming): "In my case it was node_modules/cross-env/dist/bin/cross-env.js"Atbara
You don't need to change the path to cross-env if you install it globally (see @YevgeniyAfanasyev answer). You're going to need it for all of your Laravel applications so might as well, and you don't have to update your package.json file.Excursion
Don't need it in prod, also : npm install --save-dev cross-envPfennig
A
38

Run:

npm install

Then try again.

Alba answered 20/6, 2017 at 19:8 Comment(2)
When running a VM in a Windows-based environment, this only worked for me once I had shut down Vagrant, closed Git Cmd, reopened Git Cmd as an Administrator and started Vagrant back up, and finally, ran sudo npm install from inside the project directory. If you do not shut down Vagrant prior to opening Git Cmd with Admin Priv, it has been my experience that you will encounter issues.Polio
This solved the cross-env error for me. I noticed previously that my package manager got stuck while completing npm install. So I cancelled the process, deleted package-lock.json, then ran npm install again.Gennagennaro
S
22

If the solutions above didn't work, refresh node_modules using

rm -rf node_modules && npm install

this worked for me

Sepulchre answered 3/8, 2019 at 2:48 Comment(1)
This worked for me. I don't know if this is the only step that matters. I did a few of the mentioned steps in order: - I installed cross-env globally - I updated the path to cross-env (I'm thinking now those two steps are probably mutually exclusive) - I halted vagrant and restarted it while running cmd as admin (symlinks issue, might be related, might not) - Then I did the above step of refreshing node_modules.Hakon
B
19

The fast solution is to run:

npm clean-install

that sould fix the laravel mix error.

Banting answered 24/6, 2020 at 13:58 Comment(2)
Amazing! I've been banging my head on this for along time and this just works~Bergin
Best solution and must be the accepted answerConklin
D
15

There are couple of ways to solve this error.

  • Delete node_modules folder then do npm install again. if it doesn't solve then:

  • You can install as dev dependency by firing

npm install --save-dev cross-env

if it doesn't solve then:

  • Install cross-env globally as npm install --g cross-env It will install it in \Users\Roaming\npm modules. Then I suggest you can close your shell and open it again & fire the command.

if it doesn't solve then: - in Package.json you can use like:

node node_modules/dist/bin/cross-env cross-env ...

Detent answered 16/3, 2018 at 7:19 Comment(0)
B
4

Sometimes deleting the /node_modules and running

npm install 

will solve issues like this one.

Blowtorch answered 11/10, 2019 at 19:20 Comment(0)
P
1

You do need to check if cross-env module is installed. If not, run:

npm install cross-env

But you need it to be recognized as a "command". Patching the path as proposed by the accepted answer (or is it?) led me further, but still gave me an error.

The solution for me was to reload vagrant "vagrant reload --provision", and reset my ssh / putty session.

Palla answered 28/8, 2017 at 9:50 Comment(0)
M
0

If you are just desperate of fixing this error like my case, just remove cross-env from you package.json and run laravel mix without it ..

Mullion answered 27/10, 2019 at 14:28 Comment(0)
T
-1

I had to clone repository in a new folder. Wasn't able to identify which local file/setting caused the error.

Thaumaturgy answered 21/10, 2020 at 7:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.