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?
- 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
2 Answers
Reset to default 4I'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()