Ad

How To Work With Process.env With A Build Node.js App Running In NGINX

I'm setting up a dockerized Node,MongoDB,Vue,Express application and I'm facing an issue after building my Application.

For the client part, I need to get an API-URL from the environment of the application using the following Api.js

    import axios from 'axios'

    export default() => {
      return axios.create({
        baseURL: process.env.API_URL
      })

My docker-compose (only depending):

front:
  image: nginx
  environment:
    - NODE_ENV = 'production'
    - API_URL=[hidden]
    - PORT=80

api:
  build: ./etc/server/.
  environment:
    - PORT=80
    - DATABASE_URL=server-database:27017

This code works fine on the webpack dev environment but doesn't after building the application and putting it on a different NGINX container. I also tried changing the env vars from the NGINX containers but the application still can't see any process.env.

I also tried the plugin dotenv, but also an empty process.env

Anyone an idea here?

Thanks

Ad

Answer

I ended up adding the webpack.plugins option into my base webpack configuration. This helped with finding the environment variables.

plugins: [
  new webpack.DefinePlugin({
    'process.env': {
      API_URL: JSON.stringify(process.env.API_URL),
    },
  }),
],
Ad
source: stackoverflow.com
Ad