javascript - Can't set NODE_ENV=production with npm and webpack - Stack Overflow

I'm trying to access process.env.NODE_ENV inside my app, but I only get process is not defined whe

I'm trying to access process.env.NODE_ENV inside my app, but I only get process is not defined when I check it.

package.json:

"scripts": {
    "dev": "node ./node_modules/webpack/bin/webpack.js",
    "prod": "NODE_ENV=production node ./node_modules/webpack/bin/webpack.js -p"
},

webpack.config.js:

const NODE_ENV = process.env.NODE_ENV ? process.env.NODE_ENV.toLowerCase() : 'development';

and below :

plugins: [
  new webpack.DefinePlugin({
    'process.env': {
      'NODE_ENV': JSON.stringify(NODE_ENV),
      'URL_DEV': JSON.stringify("specificIP"),
      'URL_PROD': JSON.stringify("OtherIP")
    }
  })
]

In the app source:

switch (process.env.NODE_ENV) {
  case 'development':
    url = process.env.URL_DEV;
    break;
  case 'production':
    url = process.env.URL_PROD;
    break;
  default:
    url = process.env.URL_DEV;
}

And it seems that process is not defined... What am I doing wrong here?

I'm trying to access process.env.NODE_ENV inside my app, but I only get process is not defined when I check it.

package.json:

"scripts": {
    "dev": "node ./node_modules/webpack/bin/webpack.js",
    "prod": "NODE_ENV=production node ./node_modules/webpack/bin/webpack.js -p"
},

webpack.config.js:

const NODE_ENV = process.env.NODE_ENV ? process.env.NODE_ENV.toLowerCase() : 'development';

and below :

plugins: [
  new webpack.DefinePlugin({
    'process.env': {
      'NODE_ENV': JSON.stringify(NODE_ENV),
      'URL_DEV': JSON.stringify("specificIP"),
      'URL_PROD': JSON.stringify("OtherIP")
    }
  })
]

In the app source:

switch (process.env.NODE_ENV) {
  case 'development':
    url = process.env.URL_DEV;
    break;
  case 'production':
    url = process.env.URL_PROD;
    break;
  default:
    url = process.env.URL_DEV;
}

And it seems that process is not defined... What am I doing wrong here?

Share Improve this question edited Oct 4, 2021 at 10:41 Audwin Oyong 2,5213 gold badges19 silver badges36 bronze badges asked Sep 8, 2016 at 14:29 ClafoutiClafouti 4,6655 gold badges32 silver badges39 bronze badges 2
  • check if this helps github./webpack/webpack/issues/2537? – Deendayal Garg Commented Sep 8, 2016 at 14:35
  • Thanks for the tip but I can't make it work. Actually, it seems I just can't get anything from 'process.env' even if I don't try to pass a variable. Even this 'URL_DEV': JSON.stringify("specificIP")' doesn't seem to work... – Clafouti Commented Sep 8, 2016 at 20:48
Add a ment  | 

2 Answers 2

Reset to default 4

I'm not totally sure if the problem came from my scripts key inside package.json but it seems the NODE_ENV is now correctly set if I use this :

"scripts": {
  "dev": "cross-env NODE_ENV=development node ./node_modules/webpack/bin/webpack.js --progress --colors --bail",
  "prod": "cross-env NODE_ENV=production webpack -p --progress --colors --bail"
}

So I actually used cross-env and... it magically works. If you're out of options like I was, you can still give this a shot.

Passing NODE_ENV like below adds a space at the end

  "build": "NODE_ENV=production node ./node_modules/webpack/bin/webpack.js -p",

Trim it before using the value

process.env.NODE_ENV.trim()

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745154855a4614055.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信