106

I'm trying to run npm run dev for Laravel Mix and I get this error:

> @ dev D:\projects\ptcs
> cross-env NODE_ENV=development webpack --progress --hide-modules --
config=node_modules/laravel-mix/setup/webpack.config.js

'cross-env' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! @ dev: `cross-env NODE_ENV=development webpack --progress --hide-
modules --config=node_modules/laravel-mix/setup/webpack.config.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the @ dev script.

I updated node.js to 6.11.0 and npm to 5.2.0, but it didn't help. I'm running Homestead on Windows 7.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Arthur Tarasov
  • 3,517
  • 9
  • 45
  • 57
  • I think it depends if you run then install from Homestead or Windows 7, because it install OS specific libraries. – thefallen Jul 11 '17 at 12:31
  • For me re-installing latest node.js (8.9) fixed this issue. – Rav Nov 01 '17 at 12:15
  • after Yevgeniy Afanasyev advice if you encounter that error: > The CLI moved into a separate package: webpack-cli. you should run that command: $ npm install webpack-cli -D – Hayreddin Tüzel Mar 24 '18 at 12:07

18 Answers18

250

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

1) remove node_modules folder

2) run

npm install --global cross-env

3) remove "cross-env": "^5.0.1", from package.json file devDependencies section. Actually, you can skip this step and keep package.json intact. If you prefer.

4) run

npm install --no-bin-links

5) run

npm run dev

and see it working

P.S Tested on Windows 10 with Laravel-5.4

P.P.S Windows 10 with Laravel-5.6 does not have this problem, thus updating is an alternative solution.

Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191
  • It is tested and confirmed working on Ubuntu set as a Virtual Box on Vagrant wint Laravel 5.4 (Homestead). With win10 as a base operation system. – Yevgeniy Afanasyev Aug 12 '17 at 02:12
  • 1
    Because I didn't know for what the command `npm install --no-bin-links` is used for (even researching, I didn't understand) I avoided to use it, and worked. Why is that necessary? – Felipe Augusto Jul 06 '18 at 13:37
  • "binary links" is similar to "shortcut" for windows, but binary links work on Linux. As you have your files on windows host you would not need bin-links in Linux format seating there. – Yevgeniy Afanasyev Jul 08 '18 at 22:54
  • 1
    great! it works like a charm. I used `npm install` instead `npm install --no-bin-links` what is the difference ? thanks in advanced. – Joel Meza Baca Jan 10 '19 at 22:40
  • `--no-bin-links` is for windows, it does not create binary links. – Yevgeniy Afanasyev Apr 03 '19 at 12:27
  • 1
    Thank you. It solved my problem this noon. But I only did step 2 and 3, and deleted the corresponding cross-env package folder in the node_modules folder and package-lock.json. It worked. – Lex Soft Jun 13 '19 at 12:10
  • deleting the `node_modules` and running `npm install` again works for me – Cj Oyales Nov 30 '20 at 04:38
  • tested with laravel 8 also and it run – Pietro La Grotta Dec 07 '20 at 11:22
  • We must favor having a repository/source-code self-sufficient. Meaning that by just downloading and running it, the application works. I suggest adding "cross-env" to the "devDependencies" section so any new onboarding developer would get the repository and run the code without having to go through, sometimes extensive, pre-environment configuration. – Alexz S. Feb 19 '21 at 09:14
39

First run:

rm -rf node_modules
rm package-lock.json yarn.lock
npm cache clear --force

Then run the command

npm install cross-env

npm install 

and then you can also run

npm run dev
flik
  • 3,433
  • 2
  • 20
  • 30
  • without removing any files and without clearing cache i followed rest of the command ...its worked perfectly ..Thanks flick – Pankaj Apr 29 '20 at 15:46
  • 1
    Unfortunately these steps didn't solve the problem for me. – Ryan Jun 25 '20 at 14:51
28

Following these steps solved my problem.

  1. Delete node_modules directory
  2. Delete package-lock.json file
  3. Start command prompt as Administrator <- important
  4. Run npm install
  5. Run npm run dev
Felipe Augusto
  • 7,733
  • 10
  • 39
  • 73
collin
  • 289
  • 2
  • 3
16

You are getting the error because you might not have run the command npm install first.

i.e. First, run npm install and then npm run dev

Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
Naveed Ali
  • 361
  • 3
  • 12
9

There is the same problem in Linux OS. The issue is related on Windows OS, but Homestead is a Ubuntu VM, and the solution posted works strongly good in others SO. I applied the commands sugested by flik, and the problems was solved. I only used the following commands

I only used the following commands

rm -rf node_modules
npm cache clear --force

After

npm install cross-env
npm install 
npm run watch

It's working fine on linux Fedora 25.

Felipe Augusto
  • 7,733
  • 10
  • 39
  • 73
8

According to this issue comment, editing cross-env path will fix the problem. Change cross-env to node node_modules/cross-env/dist/bin/cross-env.js in package.json like this:

    "dev": "npm run development",
    "development": "node 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 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",
    "watch-poll": "npm run watch -- --watch-poll",
    "hot": "node node_modules/cross-env/dist/bin/cross-env.js NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
    "prod": "npm run production",
    "production": "node 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"
Hafez Divandari
  • 8,381
  • 4
  • 46
  • 63
  • 1
    For those who tried this and it did not work, as a next step of this instruction 1) delete node_modules folder from the product and 2) reinstall it with "npm install --no-bin-links". 3) try "npm run dev" and see it working. BESIDES this approach works on WIN10, but not working on Ubuntu-Laravel-5.4-Homestead. I posted my own answer here that works for windows and Ubuntu. – Yevgeniy Afanasyev Aug 12 '17 at 02:17
6

For me simply run:

npm install cross-env

was enough

Luca C.
  • 11,714
  • 1
  • 86
  • 77
5

Your error states that cross-env is not installed.

'cross-env' is not recognized as an internal or external command, operable program or batch file.

You just need to run

npm install cross-env
Mike Stratton
  • 463
  • 1
  • 7
  • 20
4

The following worked for Laravel 7.x (and should probably work for any other version as well given the nature of the issue).

npm uninstall --save-dev cross-env
npm install -g cross-env

Just moving cross-env from being a local devDependency to a globally available package.

  • +1 because this worked, and I couldn't find any other answer that worked. But this feels like a hack that doesn't address the root cause of why the project-specific installation of `cross-env` wasn't working. And in my case, I'm reasonably sure that it *used to work*, so I'm confused about why it stopped working. – Ryan Jun 25 '20 at 14:59
4

Just npm install --save-dev cross-env in the root directory of your project.

Martin Brisiak
  • 3,872
  • 12
  • 37
  • 51
urvi sharma
  • 371
  • 2
  • 6
3

Before try running npm run dev please run npm install --no-bin-links in the project directory, this will install all required packages. Also check this link for compiling instruction. https://laravel.com/docs/5.4/mix

Also double check in your conf file, wherever you find something like this

(something)/cross-env/bin/(something)

change it to

(something)/cross-env/dist/bin/(something)

If you are using homestead, in package.json paste this

{
  "private": true,
  "scripts": {
    "dev": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
    "watch": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
    "watch-poll": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --watch-poll --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
    "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
    "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
  },
  "devDependencies": {
    "axios": "^0.15.3",
    "bootstrap-sass": "^3.3.7",
    "cross-env": "^3.2.3",
    "jquery": "^3.1.1",
    "laravel-mix": "^0.8.1",
    "lodash": "^4.17.4",
    "vue": "^2.1.10"
  }
}

Also check this link https://github.com/JeffreyWay/laravel-mix/issues/478

Felipe Augusto
  • 7,733
  • 10
  • 39
  • 73
only4
  • 139
  • 1
  • 11
  • Yes, it installed npm successfully. – Arthur Tarasov Jul 11 '17 at 12:29
  • 1
    Please run `npm install cros-env` – only4 Jul 11 '17 at 12:32
  • 1
    after `npm install cross-env` I get a different error when trying to `npm run dev`: $ npm run dev > @ dev D:\projects\ptcs > npm run development npm WARN invalid config loglevel="notice" > @ development D:\projects\ptcs > cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js The system cannot find the path specified. events.js:160 throw er; // Unhandled 'error' event – Arthur Tarasov Jul 11 '17 at 12:37
  • Also run this please `npm install --no-bin-links` – only4 Jul 11 '17 at 12:38
  • 1
    Adding `--no-bin-links` to `npm install` didn't change anything – Arthur Tarasov Jul 11 '17 at 12:43
3

This worked for me (on Windows 10):

  1. Add the following lines into your scripts in the package.json file:

    "dev": "npm run development",
    "development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
    "watch": "npm run development -- --watch",
    "watch-poll": "npm run watch -- --watch-poll",
    "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
    "prod": "npm run production",
    "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
    
  2. Make your devDependencies looks something like this:

    "devDependencies": {
        "axios": "^0.18",
        "bootstrap": "^4.0.0",
        "popper.js": "^1.12",
        "cross-env": "^5.1",
        "jquery": "^3.2",
        "laravel-mix": "^2.0",
        "lodash": "^4.17.4",
        "vue": "^2.5.7"
    }
    
  3. Remove node_modules folder

  4. Run npm install
  5. Run npm run dev
Gevorg Melkumyan
  • 628
  • 1
  • 12
  • 24
2

I think this log entry Local package.json exists, but node_modules missing, did you mean to install? has gave me the solution.

npm install && npm run dev
Vladimir Samsonov
  • 1,344
  • 2
  • 11
  • 18
2

Simply try running npm install / yarn etc first before running npm start / yarn start as @only4 mentioned, if you see this problem, as it means your .env is not in sync with your package.json, i.e. you installed a package but not quite configured it or other way around

Barry
  • 3,303
  • 7
  • 23
  • 42
sed
  • 5,431
  • 2
  • 24
  • 23
1

Delete the node_modules folder

Then you should run the commands:

npm install --no-bin-links

npm run dev

It's worked on my Laravel 5.5 and Windows.

Felipe Augusto
  • 7,733
  • 10
  • 39
  • 73
1

Try to run npm run dev in powershell. This worked for me.

Gevorg Melkumyan
  • 628
  • 1
  • 12
  • 24
1

I real all the solution but there is not a standard solution...

JUST REMOVE NODEJS AND INSTALL THE LATEST VERSION OF NODEJS

instead of many bad shortcut solutions.

0

I finally got it to work by following these steps:

  1. Remove the file package-lock.json
  2. Run the command: npm install --force
  3. Try again to execute npm start
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Avinash B
  • 1
  • 1