0

I am learning Node.js and this is my first code.

I created a file called server.js below with the code

server.js


        const express = require('express');
        const dotenv = require('dotenv');

        //load env vars
        dotenv.config({ path: './config/config.env'});

        const app = express();

        const PORT = process.env.PORT || 5000;

        app.listen(
            PORT,
            console.log(`Server running in ${process.env.NODE_ENV} mode on port ${PORT}`)
        );

I have this section in my package.json file

package.json


         "scripts": {
            "start": "NODE_ENV=production node server",
            "dev": "nodemon server"
          },

Here is the content of my config.env file config.env


        NODE_ENV=development
        PORT=5000

When I run npm run dev everything is fine and runs

When I run npm start to run production, I get the error below.

'NODE_ENV' is not recognized as an internal or external command, operable program or batch file.

How can I resolve this? I need npm start to run

pierpy
  • 897
  • 3
  • 14
  • 18
Baba
  • 2,059
  • 8
  • 48
  • 81
  • `NODE_ENV=production node server` is perfectly valid syntax on most shells at least in bash, sh and ksh. Are you sure you are running in an environment where your default command line shell is sh compatible like Linux or Mac OS? It sounds like you are probably running on Windows – slebetman Jul 01 '20 at 16:01
  • ` I need npm start to run` - no you don't. I personally don't use `npm start` on production servers. Look at your npm start script. What you need is `node server.js` to run and you must make sure that the environment variable NODE_ENV is set to "production" - how to do this is OS specific. On Linux and MacOS you can type `NODE_ENV=production node server.js` just like in your npm start script – slebetman Jul 01 '20 at 16:04
  • use [`cross-env`](https://www.npmjs.com/package/cross-env) package if you want to work on all os's – Lawrence Cherone Jul 01 '20 at 16:23
  • 1
    Does this answer your question? ["NODE\_ENV" is not recognized as an internal or external command, operable command or batch file](https://stackoverflow.com/questions/11928013/node-env-is-not-recognized-as-an-internal-or-external-command-operable-comman) – Lawrence Cherone Jul 01 '20 at 16:23
  • I am running on windows – Baba Jul 01 '20 at 16:29

3 Answers3

1

For Windows:

"scripts": {
  "start": "node server.js",
  "dev": "set NODE_ENV=DEVELOPMENT && node server",
  "prod": "set NODE_ENV=PRODUCTION && node server"
}

For UNIX and other OS:

"scripts": {
  "start": "node server.js",
  "dev": "export NODE_ENV=DEVELOPMENT && node server",
  "prod": "export NODE_ENV=PRODUCTION && node server"
}
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Bisrat
  • 21
  • 4
0

By the error message, you are running this in Windows. You need to use Set to setup an environment variable.

"scripts": {
            "start": "Set NODE_ENV=production&node server",
            "dev": "nodemon server"
          }

However, setting up environment variables in this manner is less secure and platform dependent. In other words, any attacker getting access to your server file system can set any environment variable by modifying the package.json. Also, if you decide to move your production to a Linux host later, your start script is going to be broken again.

So, the best practice is to set your environment variables via host configuration setup. Different cloud providers offer different methods for this.

Also, you might not need to use npm to run your script at all. You can call node server directly in your shell.

Charlie
  • 22,886
  • 11
  • 59
  • 90
  • I set this, because the OP said he runs it in production. – Charlie Jul 01 '20 at 16:05
  • Yes but listening to the OP's frustration he appears to not understand what npm start actually does. Most newbies think npm executes their programs for them. He does not even need npm. It is better to explain what is going on. One indirect but very practical way of doing that is to tell the OP that this solution will break his code in other OSes and then explain way. I'm not at all telling you your answer is wrong - it is just not complete – slebetman Jul 01 '20 at 16:11
  • I changed my code to what you have above in the scripts section and was able to run npm start. Where can I read more about the host configuration setup? At the moment I am going through a video I bought from Udemy by Brady. – Baba Jul 01 '20 at 16:39
  • It depends on your cloud provider. – Charlie Jul 01 '20 at 16:41
  • You can check Hiroku documentation on how to set environment variables for workloads – Charlie Jul 02 '20 at 03:08
0

An easy way to solve this problem:

npm install --save-dev cross-env

"start": "cross-env NODE_ENV=production node server"

This means that you don't have to worry about the platform

for read more: https://www.npmjs.com/package/cross-env