NODE_ENV behaving strangely

amunrararaPRO

a year ago

Cheers,

In my Railway-deployed Express app, the NODE_ENV variable is behaving quite strangely.

Locally, in my .env, I can set NODE_ENV to 'production', and this statement fires properly:

Local code:

console.log('Node ENV: ', process.env.NODE_ENV);
console.log(
  'Result: ',
  process.env.NODE_ENV === 'development' ? 'awaiting_shipment' : 'cancelled',
);

console.log(
  "Node ENV == 'development': ",
  process.env.NODE_ENV == 'development',
);

Result locally:

Node ENV:  development
Result:  awaiting_shipment
Node ENV == 'development':  true

Production code:

console.log('Node ENV: ', process.env.NODE_ENV);
console.log(
  'Result: ',
  process.env.NODE_ENV === 'production' ? 'awaiting_shipment' : 'cancelled',
);

console.log("Node ENV == 'production': ", process.env.NODE_ENV == 'production');

Result in Railway:

Node ENV:  'production'
Result:  cancelled
Node ENV == 'production':  false

10 Replies

amunrararaPRO

a year ago

In Railway, my NODE_ENV is properly set to 'production' however when utilizing the variable in a string comparison, it fails, despite being the same.

I've used single quotes, double quotes, and no quotes.
I've used ==, as well as ===.

No dice.


a year ago

Could you provide a reproducible example code? I fear that if I tried to reproduce it I would not face this issue.


amunrararaPRO

a year ago

If you whip up an ExpressJS app, you can use this as the app.js and it should reproduce well enough:

const express = require('express');
const bodyParser = require('body-parser');
const basicAuth = require('express-basic-auth');
const cors = require('cors');
const dotenv = require('dotenv');
dotenv.config({ path: './.env' });

const app = express();

app.use(cors());

app.use(
  bodyParser.json({
    verify: (req, _, buf) => {
      req.rawBody = buf;
    },
  }),
);


app.use(
  ['/admin/*'],
  basicAuth({
    users: {
      [process.env.ADMIN_USERNAME]: process.env.ADMIN_PASSWORD,
    },
    challenge: true,
    realm: 'Protected Area',
  }),
);

// Start the server
const port = process.env.PORT || 3456;
app.listen(port, '0.0.0.0', () => {
  console.log(`API server running on port ${port}`);
});

console.log('Node ENV: ', process.env.NODE_ENV.trim());
console.log(
  'Result: ',
  process.env.NODE_ENV.trim() === 'production'
    ? 'awaiting_shipment'
    : 'cancelled',
);

console.log(
  "Node ENV == 'production': ",
  process.env.NODE_ENV.trim() == 'production',
);

a year ago

Okay I'll give that a try.


amunrararaPRO

a year ago

Thanks for all the support brody. Let me know how it goes.


a year ago

Sorry for the late reply, had a bunch of stuff to do around the house today, but unfortunately, I was not able to reproduce this issue -

> node_test@1.0.0 start
> node app.js
Node ENV:  production
Result:  awaiting_shipment
Node ENV == 'production':  true
API server running on port 5807

amunrararaPRO

a year ago

Hmm, fascinating. I'm kinda fascinated by this, going to keep triaging. This is my Dockerfile, if anything immediately stands out:

# Use the official Node.js image as the base
FROM node:18

# Set the working directory
WORKDIR /service

RUN npm install -g pm2

# Copy package.json and package-lock.json
COPY package*.json ./

# Install dependencies
RUN yarn install

# Copy the rest of the application code
COPY . .

# Expose the API port
EXPOSE 3456

# Copy the Prisma schema
COPY prisma ./prisma/

# Generate Prisma client
RUN npx prisma generate

# Start the API server
CMD ["pm2-runtime", "start", "npm", "--", "start"]

a year ago

Not that I think it's causing your issue, I would personally never use pm2 in a production environment such as Railway, it provides an unnecessary overhead when Railway is already set to restart the deployment if it crashes with a non-zero code.

But I will try this Dockerfile with pm2 (but without prisma) and the app.js you gave me earlier.


amunrararaPRO

a year ago

Thank you Brody; that's a hold-over from a recent dev deployment to Fly.io; I needed a little extra visibility into my active Node app, but I no longer have an immediate need for it, I'll go ahead and remove that.

I actually just solved the issue by changing NODEENV to APPENV. I did not change the value, and the console log output is no different, so that's quite strange. None the less, I'm all fixed up.

Thanks for your support, again!


a year ago

That's very odd, but I'm glad you where able figure it out!