简体   繁体   English

Windows 上的 npm run build / deploy 错误

[英]npm run build / deploy error on windows

I am trying to compile files with webpack and npm for my reactJS applciation.我正在尝试使用 webpack 和 npm 为我的 reactJS 应用程序编译文件。

When I run npm start, it all goes well.当我运行 npm start 时,一切顺利。 When I run npm run deploy or npm run build it does not work under a windows environment.当我运行 npm run deploy 或 npm run build 时,它在 windows 环境下不起作用。 It does work under a linux environment.它确实在linux环境下工作。

Does someone have an idea why ?有人知道为什么吗?

My files :我的文件:

package.json包.json

{
  "license": "UNLICENSED",
  "private": true,
  "version": "1.0.0",
  "webPath": "web/",
  "nodePath": "node_modules/",
  "devDependencies": {
    "autoprefixer": "^6.3.1",
    "exports-loader": "^0.6.2",
    "grunt": "^0.4.5",
    "grunt-autoprefixer": "^3.0.3",
    "grunt-contrib-concat": "^0.5.1",
    "grunt-contrib-cssmin": "^0.14.0",
    "grunt-contrib-less": "^1.1.0",
    "grunt-contrib-uglify": "^0.11.0",
    "grunt-contrib-watch": "^0.6.1",
    "grunt-css-url-rewrite": "^0.3.5",
    "grunt-cssjoin": "^0.3.0",
    "grunt-postcss": "^0.7.1",
    "imports-loader": "^0.6.5",
    "matchdep": "^1.0.0",
    "redux-devtools": "^3.0.2",
    "redux-devtools-dock-monitor": "^1.0.1",
    "redux-devtools-log-monitor": "^1.0.4",
    "webpack-shell-plugin": "^0.4.2"
  },
  "repository": {
    "type": "git",
    "url": ""
  },
  "dependencies": {
    "babel-core": "^6.4.0",
    "babel-loader": "^6.2.1",
    "babel-plugin-transform-runtime": "^6.4.3",
    "babel-polyfill": "^6.3.14",
    "babel-preset-es2015": "^6.3.13",
    "babel-preset-react": "^6.3.13",
    "babel-preset-stage-0": "^6.3.13",
    "babel-runtime": "^6.3.19",
    "grunt-postcss": "^0.7.1",
    "history": "^1.17.0",
    "i18next": "^2.5.1",
    "isomorphic-fetch": "^2.2.1",
    "lodash": "^4.11.1",
    "radium": "^0.16.2",
    "rc-switch": "^1.4.2",
    "react": "^0.14.6",
    "react-dom": "^0.14.6",
    "react-hot-loader": "^1.3.0",
    "react-redux": "^4.1.2",
    "react-router": "^1.0.3",
    "react-router-redux": "^3.0.0",
    "redux": "^3.1.6",
    "redux-thunk": "^2.1.0",
    "selfupdate": "^1.1.0",
    "webpack": "^1.12.11",
    "webpack-dev-server": "^1.14.1",
    "whatwg-fetch": "^0.11.0"
  },
  "scripts": {
    "start": "node webpack.dev-server.js",
    "build": "webpack",
    "deploy": "NODE_ENV=production webpack -p --config webpack.production.config.js"
  }
}

webpack.config.js webpack.config.js

var path = require('path');
var webpack = require('webpack');
var node_modules_dir = path.join(__dirname, 'node_modules');

var devFlagPlugin = new webpack.DefinePlugin({
    __DEV__: JSON.stringify(JSON.parse(process.env.DEBUG || 'false'))
});

console.log(__dirname);

var config = {
    entry: [
        'babel-polyfill',
        'webpack-dev-server/client?http://127.0.0.1:3000',
        'webpack/hot/only-dev-server',
        './app/Resources/react/app.js'
    ],
    output: {
        path: path.join(__dirname, 'web/js'),
        filename: 'bundle.js',
        publicPath: 'http://127.0.0.1:3000/static/'
    },
    debug: true,
    devtool: 'eval',
    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoErrorsPlugin(),
        devFlagPlugin
    ],
    module: {
        loaders: [
            {
                loaders: ["react-hot","babel-loader?plugins=transform-runtime&presets[]=es2015&presets[]=stage-0&presets[]=react"],
                test: /\.js$/,
                include: path.join(__dirname, 'app/Resources/react')
            }
        ]
    }
};

module.exports = config;
/*
 new webpack.ProvidePlugin({
 'fetch': 'imports?this=>global!exports?global.fetch!whatwg-fetch'
 })
 */

webpack.dev-server.js webpack.dev-server.js

var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var config = require('./webpack.config');

new WebpackDevServer(webpack(config), {
    publicPath: config.output.publicPath,
    hot: true,
    historyApiFallback: true,
    quiet: false,
    noInfo: false,
    contentBase: "./assets"
}).listen(3000, 'localhost', function (err, result) {
    if (err) {
        console.log(err);
    }
    console.log('Listening at localhost:3000');
});

webpack.production.config.js webpack.production.config.js

var path = require('path');
var node_modules_dir = path.resolve(__dirname, 'node_modules');

var config = {
    entry: [
        'babel-polyfill',
        './app/Resources/react/app.js'
    ],
    debug: false,
    output: {
        path: path.join(__dirname, 'web/js'),
        filename: 'bundle.js'
    },
    module: {
        loaders: [
            {
                loaders: ["babel-loader?plugins=transform-runtime&presets[]=es2015&presets[]=stage-0&presets[]=react"],
                test: /\.js$/,
                include: path.join(__dirname, 'app/Resources/react')
            }
        ]
    },
    plugins: []
};

module.exports = config;

I get the following error with npm run build npm run build 出现以下错误

Microsoft Windows [Version 10.0.10586] (c) 2015 Microsoft Corporation. Microsoft Windows [版本 10.0.10586] (c) 2015 Microsoft Corporation。 All rights reserved.保留所有权利。

D:\\Divers\\Programmation\\Web\\foodmeup.dev>npm run build D:\\Divers\\Programmation\\Web\\foodmeup.dev>npm run build

FoodMeUp@1.0.0 build D:\\Divers\\Programmation\\Web\\foodmeup.dev webpack FoodMeUp@1.0.0 构建 D:\\Divers\\Programmation\\Web\\foodmeup.dev webpack

D:\\Divers\\Programmation\\Web\\foodmeup.dev Hash: 9cb8c898ada89430aa7c Version: webpack 1.13.1 Time: 3353ms [0] multi main 64 bytes {0} [built] + 739 hidden modules D:\\Divers\\Programmation\\Web\\foodmeup.dev 哈希:9cb8c898ada89430aa7c 版本:webpack 1.13.1 时间:3353ms [0] multi main 64 bytes {0} [built] + 739 hidden modules

ERROR in ./app/Resources/react/components/subscription/total.js Module not found: Error: Cannot resolve module 'lodash' in D:\\Divers\\Programmation\\Web\\foodmeup.dev\\app\\Resources\\react\\components\\subscription @ ./app/Resources/react/components/subscription/total.js 19:8-25 ./app/Resources/react/components/subscription/total.js 模块中的错误未找到:错误:无法解析 D:\\Divers\\Programmation\\Web\\foodmeup.dev\\app\\Resources\\react\\components 中的模块“lodash” \\subscription @ ./app/Resources/react/components/subscription/total.js 19:8-25

my total.js file :我的 total.js 文件:

import React from 'react';
import { connect } from 'react-redux';
import { updateSubscription } from '../../actions/subscription';
var _ = require('lodash');

...

I get the following error when I launch npm run deploy启动 npm run deploy 时出现以下错误

D:\\Divers\\Programmation\\Web\\foodmeup.dev>npm run deploy D:\\Divers\\Programmation\\Web\\foodmeup.dev>npm run deploy

FoodMeUp@1.0.0 deploy D:\\Divers\\Programmation\\Web\\foodmeup.dev NODE_ENV=production webpack -p --config webpack.production.config.js FoodMeUp@1.0.0 部署 D:\\Divers\\Programmation\\Web\\foodmeup.dev NODE_ENV=production webpack -p --config webpack.production.config.js

'NODE_ENV' is not recognized as an internal or external command, operable program or batch file. 'NODE_ENV' 不是内部或外部命令,也不是可运行的程序或批处理文件。

npm ERR! npm 错误! Windows_NT 10.0.10586 npm ERR! Windows_NT 10.0.10586 npm ERR! argv "C:\\Development\\nodejs\\node.exe" "C:\\Development\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "deploy" npm ERR! argv "C:\\Development\\nodejs\\node.exe" "C:\\Development\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "deploy" npm ERR! node v6.2.2 npm ERR! node v6.2.2 npm ERR! npm v3.9.5 npm ERR! npm v3.9.5 npm ERR! code ELIFECYCLE npm ERR!代码 ELIFECYCLE npm ERR! FoodMeUp@1.0.0 deploy: NODE_ENV=production webpack -p --config webpack.production.config.js npm ERR! FoodMeUp@1.0.0 部署: NODE_ENV=production webpack -p --config webpack.production.config.js npm ERR! Exit status 1 npm ERR!退出状态 1 npm ERR! npm ERR! npm 错误! Failed at the FoodMeUp@1.0.0 deploy script 'NODE_ENV=production webpack -p --config webpack.production.config.js'.在 FoodMeUp@1.0.0 部署脚本“NODE_ENV=production webpack -p --config webpack.production.config.js”失败。 npm ERR! npm 错误! Make sure you have the latest version of node.js and npm installed.确保您安装了最新版本的 node.js 和 npm。 npm ERR! npm 错误! If you do, this is most likely a problem with the FoodMeUp package, npm ERR!如果你这样做了,这很可能是 FoodMeUp 包 npm ERR 的问题! not with npm itself.不是 npm 本身。 npm ERR! npm 错误! Tell the author that this fails on your system: npm ERR!告诉作者这在你的系统上失败了:npm ERR! NODE_ENV=production webpack -p --config webpack.production.config.js npm ERR! NODE_ENV=production webpack -p --config webpack.production.config.js npm ERR! You can get information on how to open an issue for this project with: npm ERR!您可以通过以下方式获取有关如何为此项目打开问题的信息:npm ERR! npm bugs FoodMeUp npm ERR! npm 错误 FoodMeUp npm ERR! Or if that isn't available, you can get their info via: npm ERR!或者,如果这不可用,您可以通过以下方式获取他们的信息:npm ERR! npm owner ls FoodMeUp npm ERR! npm 所有者 ls FoodMeUp npm ERR! There is likely additional logging output above.上面可能有额外的日志输出。

npm ERR! npm 错误! Please include the following file with any support request: npm ERR!请在任何支持请求中包含以下文件:npm ERR! D:\\Divers\\Programmation\\Web\\foodmeup.dev\\npm-debug.log D:\\Divers\\Programmation\\Web\\foodmeup.dev\\npm-debug.log

and my npm-debug.log和我的 npm-debug.log

0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Development\\nodejs\\node.exe',
1 verbose cli   'C:\\Development\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli   'run',
1 verbose cli   'deploy' ]
2 info using npm@3.9.5
3 info using node@v6.2.2
4 verbose run-script [ 'predeploy', 'deploy', 'postdeploy' ]
5 info lifecycle FoodMeUp@1.0.0~predeploy: FoodMeUp@1.0.0
6 silly lifecycle FoodMeUp@1.0.0~predeploy: no script for predeploy, continuing
7 info lifecycle FoodMeUp@1.0.0~deploy: FoodMeUp@1.0.0
8 verbose lifecycle FoodMeUp@1.0.0~deploy: unsafe-perm in lifecycle true
9 verbose lifecycle FoodMeUp@1.0.0~deploy: PATH: C:\Development\nodejs\node_modules\npm\bin\node-gyp-bin;D:\Divers\Programmation\Web\foodmeup.dev\node_modules\.bin;C:\Development\nodejs;C:\img;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Development\Git\cmd;C:\Program Files (x86)\Windows Live\Shared;C:\Development\wamp\bin\php\php7.0.0;C:\Net Generation\FireFox\;C:\Development\wamp\bin\mysql\mysql5.7.9\bin;C:\Program Files (x86)\Skype\Phone\;D:\Divers\Programmation\Web\foodmeup.dev\node_modules\typescript\bin;C:\Development\nodejs\;C:\Development\ruby\bin;C:\Development\wamp\bin\php\php7.0.0;C:\Development\wamp\bin\mysql\mysql5.7.9\bin;D:\Divers\Programmation\Web\foodmeup.dev\node_modules\typescript\bin;C:\img;C:\Users\Sébastien\AppData\Roaming\npm
10 verbose lifecycle FoodMeUp@1.0.0~deploy: CWD: D:\Divers\Programmation\Web\foodmeup.dev
11 silly lifecycle FoodMeUp@1.0.0~deploy: Args: [ '/d /s /c',
11 silly lifecycle   'NODE_ENV=production webpack -p --config webpack.production.config.js' ]
12 silly lifecycle FoodMeUp@1.0.0~deploy: Returned: code: 1  signal: null
13 info lifecycle FoodMeUp@1.0.0~deploy: Failed to exec deploy script
14 verbose stack Error: FoodMeUp@1.0.0 deploy: `NODE_ENV=production webpack -p --config webpack.production.config.js`
14 verbose stack Exit status 1
14 verbose stack     at EventEmitter.<anonymous> (C:\Development\nodejs\node_modules\npm\lib\utils\lifecycle.js:245:16)
14 verbose stack     at emitTwo (events.js:106:13)
14 verbose stack     at EventEmitter.emit (events.js:191:7)
14 verbose stack     at ChildProcess.<anonymous> (C:\Development\nodejs\node_modules\npm\lib\utils\spawn.js:24:14)
14 verbose stack     at emitTwo (events.js:106:13)
14 verbose stack     at ChildProcess.emit (events.js:191:7)
14 verbose stack     at maybeClose (internal/child_process.js:852:16)
14 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:215:5)
15 verbose pkgid FoodMeUp@1.0.0
16 verbose cwd D:\Divers\Programmation\Web\foodmeup.dev
17 error Windows_NT 10.0.10586
18 error argv "C:\\Development\\nodejs\\node.exe" "C:\\Development\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "deploy"
19 error node v6.2.2
20 error npm  v3.9.5
21 error code ELIFECYCLE
22 error FoodMeUp@1.0.0 deploy: `NODE_ENV=production webpack -p --config webpack.production.config.js`
22 error Exit status 1
23 error Failed at the FoodMeUp@1.0.0 deploy script 'NODE_ENV=production webpack -p --config webpack.production.config.js'.
23 error Make sure you have the latest version of node.js and npm installed.
23 error If you do, this is most likely a problem with the FoodMeUp package,
23 error not with npm itself.
23 error Tell the author that this fails on your system:
23 error     NODE_ENV=production webpack -p --config webpack.production.config.js
23 error You can get information on how to open an issue for this project with:
23 error     npm bugs FoodMeUp
23 error Or if that isn't available, you can get their info via:
23 error     npm owner ls FoodMeUp
23 error There is likely additional logging output above.
24 verbose exit [ 1, true ]

It looks like the configuration file is not 100% correct.看起来配置文件不是 100% 正确的。 See details below:请参阅下面的详细信息:

  • need to import 'webpack' mobile before using it (webpack.config.js), ie, const webpack = require('webpack');使用前需要导入'webpack' mobile(webpack.config.js),即const webpack = require('webpack');

  • need to import path module before using it (webpack.config.js), ie, const path = require('path');使用前需要导入path模块(webpack.config.js),即const path = require('path');

  • devFlagPlugin is not defined devFlagPlugin

  • also exclude the node_modules directory in the loader还要排除加载器中的node_modules目录

    module: { loaders: [ { .... exclude: path.resolve(__dirname, 'node_modules/') } ] }

I think you have two different problems.我认为你有两个不同的问题。

Lodash is missing?洛达什不见了?

Now, I'm not an expert in Webpack so somebody may point out something obvious that I don't see...but...when you say it doesn't work under Windows, have you tried it on multiple Windows systems?现在,我不是 Webpack 的专家,所以有人可能会指出一些我看不到的明显内容……但是……当您说它在 Windows 下不起作用时,您是否在多个 Windows 系统上尝试过它?

If this issue is only on one system or it's a PITA to get onto a second box or whatever, try:如果此问题仅出现在一个系统上,或者是 PITA 进入第二个盒子或其他任何东西,请尝试:

  1. Delete the node_modules directory删除node_modules目录
  2. Execute npm cache clean to be sure you'll start fresh when you reinstall执行npm cache clean以确保重新安装时重新开始
  3. Re-install with npm i --verbose > npm-install-log.txt使用npm i --verbose > npm-install-log.txt重新安装

If the install still doesn't work, you'll have an install log to review to see if something is failing to compile or there's some other wacky error that may not be visible after install.如果安装仍然不起作用,您将有一个安装日志来查看是否有编译失败或安装后可能看不到的其他一些古怪错误。

NODE_ENV on Windows Windows 上的 NODE_ENV

The NODE_ENV=production <commands> syntax is a *nix thing but not a Windows/DOS thing . NODE_ENV=production <commands>语法是 *nix 的东西,但不是 Windows/DOS 的东西

In this case, you may find it easier to create a .bat file for this where you set the variable on the first line and call the command on the second.在这种情况下,您可能会发现为此创建一个.bat文件更容易,您可以在第一行设置变量并在第二行调用命令。 However, this won't work on *nix platforms so....maybe:但是,这在 *nix 平台上不起作用,所以....也许:

  1. Create two scripts and two entries in package.json , one for each OS.package.json创建两个脚本和两个条目,每个操作系统一个。 If it's a simple project that's mostly maintained by hand, that's probably cool.如果它是一个主要由手工维护的简单项目,那可能很酷。
  2. Create a deploy.js script, executed by node, that sets the NODE_ENV variable, calls webpack via the node api to do the work.创建一个由 node 执行的deploy.js脚本,它设置NODE_ENV变量,通过node api调用 webpack 来完成工作。 This should be OS independent and is a single script...应该是独立于操作系统的,并且是一个单一的脚本......

您可以使用cross-env以跨平台方式设置环境变量:

"deploy": "cross-env NODE_ENV=production webpack -p --config webpack.production.config.js"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM