节点js - 传递参数以构建

时间:2018-03-21 08:17:03

标签: node.js webpack

尝试将参数传递给app的服务器端部分。

的package.json:

"scripts": {
    "build": "better-npm-run build",
  },
"betterScripts": {
    "build": {
      "command": "webpack -p --config webpack.config.production.js",
      "env": {
        "TEST": "MYTEST",
      }
    },

webpack.config.production.js:

const client={...}
const server = {
  entry: {
    server: ['babel-polyfill', './src/server/index.js']
  },
  target: 'node',
  output: {
      filename: '[name].js',
      path: path.resolve(__dirname, 'dist')
  },
  module: moduleObj,
  externals: [nodeExternals()]
}

module.exports = [client, server];

然后在代码中:

console.log(process.env.TEST); 
// undefined

当服务器在开发模式下运行时,没有通过webpack build paramaters传递:

"dev-server": {
      "command": "nodemon --inspect src/server/index.js --exec babel-node",
      "env": {
        "TEST": "NO_PROBLEM_THERE",
      }
    }

它必须是一些webpack配置问题,但我不知道它到底是什么... 有什么想法吗?

0 个答案:

没有答案