简体   繁体   English

Node.js-NODE_ENV未定义

[英]Node.js - NODE_ENV is undefined

I am trying to set the environment variable NODE_ENV for my project. 我正在尝试为我的项目设置环境变量NODE_ENV。

I am using Windows and have set the NODE_ENV in the system settings - this has been verified by typing SET and identifying for the row below in the output. 我正在使用Windows,并且已在系统设置中设置了NODE_ENV-已通过键入SET并在输出中识别出下面的行来验证这一点。

NODE_ENV=production NODE_ENV =生产

I cannot seem to get the variable to set in webpack though. 我似乎无法在Webpack中设置该变量。

When adding the code below to my project (index.js) it only logs out undefined 将以下代码添加到我的项目(index.js)时,它仅注销未定义

console.log('PROCESS', process.env.NODE_ENV)

My webpack config: 我的webpack配置:

const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin')

process.env.NODE_ENV = process.env.NODE_ENV || 'development'

if (process.env.NODE_ENV === 'test') {
  require('dotenv').config({ path: '.env.test' })
} else if (process.env.NODE_ENV === 'development') {
  require('dotenv').config({ path: '.env.development' })
} else if (process.env.NODE_ENV === 'production') {
  require('dotenv').config({ path: '.env.production' })
} else {
  require('dotenv').config({ path: '.env.development' })
}

module.exports = (env) => {
  const isProduction = env === 'production';
  ...
    plugins: [
      CSSExtract,
      new UglifyJSPlugin(),
      new webpack.DefinePlugin({
        'process.env.FIREBASE_API_KEY': JSON.stringify(process.env.FIREBASE_API_KEY),
        'process.env.FIREBASE_AUTH_DOMAIN': JSON.stringify(process.env.FIREBASE_AUTH_DOMAIN),
        'process.env.FIREBASE_DATABASE_URL': JSON.stringify(process.env.FIREBASE_DATABASE_URL),
        ...
      }),
    devtool: isProduction ? 'source-map' : 'inline-source-map',
    ...

I have read this question , but still cannot get the env variable to set. 我已经阅读了这个问题 ,但是仍然无法设置env变量。

Where am I going wrong? 我要去哪里错了?

I managed to get set the NODE_ENV by using the cross-env package! 我设法使用cross-env软件包设置了NODE_ENV! If you are developing node.js on Windows this can be very useful. 如果要在Windows上开发node.js,这可能会非常有用。 Linux/Mac users would not have this problem. Linux / Mac用户不会有此问题。

To set the environment variable simply type 要设置环境变量,只需键入

cross-env NODE_ENV=production [your commend goes here] cross-env NODE_ENV = production [您的赞扬在这里]

Example: 例:

cross-env NODE_ENV=production webpack --env production cross-env NODE_ENV =生产webpack --env生产

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

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