所以我刚刚开始学习webpack(2.2.1)并做出反应,我遇到了NODE_ENV的一些问题。我在package.json中设置它是这样的:
"scripts": {
"start": "set NODE_ENV=production&& webpack",
"test": "echo \"Error: no test specified\" && exit 1"
}
然后我将一个console.log(process.env.NODE_ENV)
放入webpack的入口文件中,看看它是否已设置并按预期进行。
但是,当我在项目的根目录中名为server.js的文件中运行express时:
const express = require('express');
const path = require('path');
const app = new express;
console.log(process.env.NODE_ENV);
const port = process.env.PORT || 3000;
app.use(express.static('public'));
app.get('/',(req,res)=>{
res.sendFile(path.join(__dirname, "public/index.html"));
});
app.listen(port, ()=>{
console.log("listening...", process.env.NODE_ENV);
});
process.env.NODE_ENV未定义。
这是webpack.config
const webpack = require('webpack');
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const VENDOR_LIBS = ['react', 'react-dom', 'bootstrap', 'jquery'];
module.exports = {
entry: {
bundle: './src/index.js',
vendor: VENDOR_LIBS
},
output: {
path: __dirname + '/public',
filename: '[name].js'
},
module: {
rules: [
{
use: 'babel-loader',
test: /\.(js|jsx)$/,
exclude: /node_modules/
},
{
loader: ['style-loader', 'css-loader'],
test: /.css$/
},
{
test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url?limit=10000&mimetype=application/octet-stream'
},
{
loader: 'url?limit=10000!img?progressive=true',
test: /\.(jpe?g|png|gif|svg)$/i
}
]
},
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jquery: 'jquery',
jQuery: 'jquery'
}),
new webpack.optimize.CommonsChunkPlugin({name: "vendor", filename: "vendor.js"}),
new ExtractTextPlugin("./styles/bootstrap.css"),
new webpack.DefinePlugin({'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)})
]
}
非常感谢任何帮助。