三、實戰系列問題處理集中營

mrsmallyi發表於2018-12-02

1、npm run watch-poll 提示 cross-env: not found

vagrant@homestead:~/code/laravel-shop$ npm run dev
> @ dev /home/vagrant/code/laravel-shop
> npm run development

> @ development /home/vagrant/code/laravel-shop
> cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js

sh: 1: **cross-env: not found**
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! @ development: `cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the @ development script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

網上找到了一篇文章說明了問題的原因--Laravel 5.4 Mix執行 npm run dev時報錯,提示cross-env找不到(not found)的原因及解決辦法

  • cross-env 是什麼?
    Most Windows command prompts will choke when you set environment variables with NODE_ENV=production like that. (The exception is Bash on Windows, which uses native Bash.) Similarly, there's a difference in how windows and POSIX commands utilize environment variables. With POSIX, you use: $ENV_VAR and on windows you use %ENV_VAR%.
    意思是,Windows 系統設定系統環境變數一般是%ENV_VAR%,其他系統是$ENV_VAR,所以為了解決不同系統設定系統環境變數問題,cross-env元件就是起這個作用,cross-env是能是用在window系統上,如果用他mac或linux系統還可能出現問題
    • 問題是如何引起的?
      • 如果是window系統,可能是cross-env沒裝或路徑找不到等
      • 如果是mac或linux系統,可能是不需要安裝反而裝了導致的問題

因此,我的根據實戰教程,linux 系統,不需要這個元件,所以我得去掉cross-env元件就可以
package.json檔案中去掉 cross-env,如下:

    {
    "private": true,
    "scripts": {
        "dev": "npm run development",
        "development": "NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
        "watch": "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_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_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
    },
    "devDependencies": {
        "axios": "^0.17",
        "bootstrap-sass": "^3.3.7",
        "cross-env": "^5.1",
        "jquery": "^3.2",
        "laravel-mix": "^1.0",
        "lodash": "^4.17.4",
        "vue": "^2.5.7"
    }
}

儲存,輸入命令npm run watch-poll

vagrant@homestead:~/code/laravel-shop$ npm run watch-poll

> @ watch-poll /home/vagrant/code/laravel-shop
> npm run watch -- --watch-poll

> @ watch /home/vagrant/code/laravel-shop
> NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js "--watch-poll"

 10% building modules 0/1 modules 1 active ...l-shop/resources/assets/sass/app 10% building modules 1/1 modules 0 active
Webpack is watching the files…

 10% building modules 1/2 modules 1 active ...ravel-shop/resources/assets/js/a 10% building modules 1/3 modules 2 active ...l-shop/resources/assets/sass/app 10% building modules 2/3 modules 1 active ...ravel-shop/resources/assets/js/a 10% building modules 2/4 modules 2 active ...l-shop/resources/assets/sass/app 10% building modules 2/5 modules 3 active ...modules/style-loader/lib/addStyl 10% building modules 3/5 modules 2 active ...l
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章