0

I am trying to run an express app using Next.js. I have mostly got it under control, but for some reason the package.json does assign NODE_ENV to the correct value, but the comparing the two does not seem to work.

Scripts in json:

"scripts": {
  "dev": "nodemon start server.js",
  "build": "next build",
  "start": "set NODE_ENV=production && node server.js"
},

The app:

const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });

No matter what I do, it seems to assign the value correctly, but the comparison always returns true. If I try to do an if-else statement using == or === it all passes as it doesn't match. I've console them both in tandem, adding a number to the end to see the difference, and everything aside the added number was correct.

William
  • 1,175
  • 2
  • 17
  • 32
  • this works if you use Mac or Linux, on Windows you should use something like [cross-env](https://www.npmjs.com/package/cross-env) or [dot-env](https://www.npmjs.com/package/dot-env) – SylvainF Mar 05 '19 at 17:16

1 Answers1

0

In your package.json use :

"scripts": {
    "dev": "nodemon start server.js",
    "build": "next build",
    "start": "NODE_ENV=production node server.js"
},

Observe I have removed the set and the && in your start script.
After this is done ensure you are executing the script by executing

npm start

If you still face problems, console.log the process.env.NODE_ENV to cross-check what you are getting.

Adriano
  • 3,788
  • 5
  • 32
  • 53
Sachin Nayak
  • 1,041
  • 9
  • 17