I have a script in nodejs with webpack where I want to use environment variables from docker-compose but every times the variables is undefined.
this is a little piece of docker-compose:
container:
image: "node:8-alpine"
user: "node"
working_dir: /home/node/app
environment:
- NODE_ENV=development
volumes:
- ./project:/home/node/app
- ./conf:/home/node/conf
command: "yarn start"
I have this webpack configuration:
const path = require('path');
const TerserPlugin = require('terser-webpack-plugin');
const DefinePlugin = require('webpack').DefinePlugin;
module.exports = {
entry: './src-js/widget.js',
mode: process.env.NODE_ENV || 'development',
output: {
filename: 'widget.js',
path: path.resolve(__dirname, 'public')
},
optimization: {
minimizer: [new TerserPlugin()]
},
plugins: [
new DefinePlugin({
ENV: JSON.stringify(process.env.NODE_ENV)
})
]
};
Into my node script I would like to use NODE_ENV variables, so I have tried all this solutions but every time is undefined
console.log(process.env.NODE_ENV);
console.log(ENV);
console.log(process.env); //is empty
From the docker container I have tried to print environment variables and inside it there is NODE_ENV but I can't use it into node file. Why?
Usually I use yarn build or yarn watch to recompile it
Try this in your webpack configuration:
new DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
})
Additionally, official docs have a snippet about it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With