Node process.env variable in eval() function gives different result

12/7/2018

I have a react app running in node with serverside rendering. The following environment variable is set to test through kubernetes in my test environment: process.env.NODE_ENV.

When I run the following two commands they give different results. I expect the value to always be test.

log.debug(process.env.NODE_ENV) // logs development
log.debug(eval('process.env.NODE_ENV')) // logs test

Somehow, it looks like the variable is first interpreted as development (Which can happen in my code if it is undefined), but it is somehow interpreted correctly to test by the eval() function.

What can cause node to interpret the value differently between the two expressions?

EDIT: Added kubernetes yaml config.
The ${} variables are replaced by Azure DevOps during the release process.

apiVersion: v1
kind: ConfigMap
metadata:
  name: config
  namespace: ${KUBERNETES_NAMESPACE}
data:
  NODE_ENV: ${NODE_ENV}
---
kind: Service
apiVersion: v1
metadata:
  name: ${SERVICE_NAME}
spec:
  selector:
    app: ${SERVICE_NAME}
  ports:
  - name: http
    protocol: TCP
    port: 80
    targetPort: 3000

  loadBalancerIP: ${IP_NUMBER}
  type: LoadBalancer


---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: ${SERVICE_NAME}
  labels:
    app: ${SERVICE_NAME}
spec:
  replicas: 2
  selector:
    matchLabels:
      app: ${SERVICE_NAME}

  template:
    metadata:
      labels:
        app: ${SERVICE_NAME}
    spec:
      containers:
      - name: ${SERVICE_NAME}
        image: {IMAGE_PATH}/${IMAGE_REPO}:${BUILD_NUMBER}
        ports:
        - name: http
          containerPort: 3000
          protocol: TCP
        resources: 
          limits:
            cpu: 100m
            memory: 1024Mi
          requests:
            cpu: 100m
            memory: 1024Mi
        envFrom:
        - configMapRef:
            name: config

      imagePullSecrets: 
      - name: ${IMAGEPULLSECRETNAME}
-- Jakob S. Rames
eval
javascript
kubernetes
node.js
server-side-rendering

1 Answer

12/7/2018

I seem to have found the cause of the issue.
We use webpack for bundling (which I maybe should have mentioned), and in the server code webpack has outputted, I see that it has resolved process.env.NODE_ENV to a static value, but it doesn't do the same for eval(process.env.NODE_ENV).
Seems my post was unecessary, but I hope it might help someone in the future.

-- Jakob S. Rames
Source: StackOverflow