Node.js Webpack/Express-服务器找不到环境变量
在我的Express/React应用程序中,我使用Webpack处理服务器端渲染。但是,我遇到了一个生成错误,该错误与我试图在Express server脚本中访问的环境变量有关 在服务器脚本中,Node.js Webpack/Express-服务器找不到环境变量,node.js,express,webpack,Node.js,Express,Webpack,在我的Express/React应用程序中,我使用Webpack处理服务器端渲染。但是,我遇到了一个生成错误,该错误与我试图在Express server脚本中访问的环境变量有关 在服务器脚本中,index.js,我设置了如下几个变量: const gitCommit = process.env.GIT_COMMIT || require("./gitignore/git_commit.js"), buildDate = process.env.BUILD_DATE || require
index.js
,我设置了如下几个变量:
const gitCommit = process.env.GIT_COMMIT || require("./gitignore/git_commit.js"),
buildDate = process.env.BUILD_DATE || require("./gitignore/build_date.js")
由于我正在本地机器上运行测试产品构建,我删除了gitignore/
目录并设置了这些环境变量:
$ export GIT_COMMIT="test commit hash"
$ export BUILD_DATE="test build date"
然后我npm运行build
,它执行以下脚本:
"build:client": "webpack --config webpack.config.js",
"build:server": "webpack --config webpack.server.config.js",
"build": "npm run build:client && npm run build:server"
build:client
执行时没有问题,但是build:server
抛出错误
ERROR in ./index.js
Module not found: Error: Can't resolve './gitignore/git_commit.js' in '/Users/filepath'
@ ./index.js 12:38-74
ERROR in ./index.js
Module not found: Error: Can't resolve './gitignore/build_date.js' in '/Users/filepath'
@ ./index.js 13:42-78
这意味着在index.js
中引用的两个环境变量找不到,因此它寻找的是gitignore/
,它不应该存在(我的意思是,它确实存在于本地,但我在模拟生产构建时删除了它)
以下是完整的webpack.server.config.js
:
const fs = require("fs"),
path = require("path")// ,
// ExtractTextPlugin = require("extract-text-webpack-plugin")
module.exports = {
"entry": path.resolve(__dirname, "index.js"),
// keep node_module paths out of the bundle
"externals": fs.readdirSync(path.resolve(__dirname, "node_modules")).concat(["react-dom/server", "react/addons"]).reduce((ext, mod) => {
ext[mod] = `commonjs ${mod}`
return ext
}, {}),
"module": {
"loaders": [
{
"exclude": /node_modules/,
"loader": "babel-loader",
"query": { "presets": ["react", "es2015", "stage-2"] },
"test": /\.jsx$/
},
{
"exclude": /node_modules/,
"loader": "babel-loader",
"query": { "presets": ["react", "es2015", "stage-2"] },
"test": /\.js$/
}
]
},
"node": {
"__dirname": true,
"__filename": true
},
"output": {
"filename": "server.bundle.js"
},
"target": "node"
}
现在我希望找不到gitignore/
,但我不明白的是为什么我设置的两个环境变量没有被index.js
检测到-它们肯定是在我运行build
命令之前在控制台中设置的。如果我console.log()
在webpack.server.config.js
的开头记录它们,它会正确地记录它们,如果我运行我的开发版本(不使用服务器配置),我可以在index.js
中正确地记录它们。有什么好处
节点版本6.11.1、NPM版本3.10.10、Webpack版本2.6.0。您的环境变量仅在Webpack运行时可用,但在执行
index.js时不可用。
您需要在您的网页包配置中使用:
plugins: [new webpack.EnvironmentPlugin(['GIT_COMMIT ', 'BUILD_DATE'])]
该插件将用实际值替换变量
提示:不要使用|
。Webpack不知道如何优化它。尝试三元运算符:
const gitCommit = (process.env.GIT_COMMIT) ? (
process.env.GIT_COMMIT
) : (
require('./gitignore/git_commit.js')
);
Webpack将此绑定到:
const gitCommit = (true) ? (
"test commit hash"
) : (
require('./gitignore/git_commit.js')
);
不需要IgnorePlugin
。更好的是,使用UglifyJSPlugin
,您的代码将被优化为const gitCommit=“test commit hash”代码>。在某些情况下,gitCommit
作为变量被完全删除。它的字符串值将在您应用gitCommit
的任何地方使用。事实上,很抱歉,这不起作用。。。我在webpack.server.config.js
中添加了“插件”:[new webpack.EnvironmentPlugin([“GIT_COMMIT”,“BUILD_DATE”])]
,但我仍然有同样的问题。我希望你做两件事:1。调试您的server.bundle.js
那里的process.env实际发生了什么?EnvironmentPlugin是否实际替换了这些值?2.运行server.bundle.js
,在命令行上设置环境变量,看看会发生什么。好的,这很有趣-我在命令行上设置了环境变量-对于GIT_提交,我使用了“TEST GIT COMMIT HASH”。在我的server.bundle.js
中,我得到了:gitCommit=“TEST GIT COMMIT HASH”| | |)
。所以它正确地替换了它。。。但是为什么要使用webpackMissingModule()
?哦,我想我已经弄明白了!我在插件中添加了新的webpack.IgnorePlugin(/gitignore/)
,它似乎起了作用!我仍然在我的server.bundle.js
中得到相同的webpackMissingModule()
内容,但它不会抛出错误。这是一个好的解决方案,还是在枪声中使用绷带?如果是好的,请将其添加到您的答案中,我当然会接受!