Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

process.env undefined from docker-compose

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

like image 630
Alessandro Minoccheri Avatar asked Aug 18 '26 13:08

Alessandro Minoccheri


1 Answers

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.

like image 200
Vladyslav Usenko Avatar answered Aug 20 '26 03:08

Vladyslav Usenko