node.js 参考错误:未定义 webpack
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31592819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ReferenceError: webpack is not defined
提问by leaksterrr
In my webpack app I have a basic build process that's triggered by "npm run build" which executes the webpack binary and copies my index.html in /app to /dist. Whenever I run npm run buildI get ReferenceError: webpack is not definedbut when I run npm start, which starts the webpack-dev-server, everything's fine.
在我的 webpack 应用程序中,我有一个由“npm run build”触发的基本构建过程,它执行 webpack 二进制文件并将 /app 中的 index.html 复制到 /dist。每当我运行时,npm run build我都会得到ReferenceError: webpack is not defined但是当我运行时npm start,它会启动 webpack-dev-server,一切都很好。
This is my webpack config file:
这是我的 webpack 配置文件:
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var config = {
context: __dirname + '/app',
entry: './index.js',
output: {
path: __dirname + '/app',
filename: 'app.js'
},
module: {
loaders: [
{ test: /\.js$/, loader: 'babel', exclude: /node_modules/ },
{ test: /\.html$/, loader: 'raw', exclude: /node_modules/ },
{ test: /\.scss$/, loader: ExtractTextPlugin.extract('style', 'css!sass'), exclude: /node_modules/}
]
},
plugins: [
new ExtractTextPlugin('app.css')
]
};
if (process.env.NODE_ENV == 'production') {
config.output.path = __dirname + '/dist';
config.plugins.push(new webpack.optimize.UglifyJsPlugin());
}
module.exports = config;
回答by Juho Veps?l?inen
You are missing
你不见了
var webpack = require('webpack');
at the beginning of your file. If you want to optimize execution a bit, you can push it inside that ifblock of yours.
在文件的开头。如果你想稍微优化一下执行,你可以把它推if到你的那个块中。

