process.env.NODE_ENV is undefined

后端 未结 13 1699
南旧
南旧 2020-11-28 18:26

I\'m trying to follow a tutorial on NodeJs. I don\'t think I missed anything but whenever I call the process.env.NODE_ENV the only value I get back is undefine

相关标签:
13条回答
  • 2020-11-28 19:00

    You can use the cross-env npm package. It will take care of trimming the environment variable, and will also make sure it works across different platforms.

    In the project root, run:

    npm install cross-env
    

    Then in your package.json, under scripts, add:

    "start": "cross-env NODE_ENV=dev node your-app-name.js"
    

    Then in your terminal, at the project root, start your app by running:

    npm start
    

    The environment variable will then be available in your app as process.env.NODE_ENV, so you could do something like:

    if (process.env.NODE_ENV === 'dev') {
      // Your dev-only logic goes here
    }
    
    0 讨论(0)
  • 2020-11-28 19:02

    process.env is a reference to your environment, so you have to set the variable there.

    To set an environment variable in Windows:

    SET NODE_ENV=development
    

    on OS X or Linux:

    export NODE_ENV=development
    
    0 讨论(0)
  • 2020-11-28 19:02

    tips

    in package.json:

    "scripts": {
      "start": "set NODE_ENV=dev && node app.js"
     }
    

    in app.js:

    console.log(process.env.NODE_ENV) // dev
    console.log(process.env.NODE_ENV === 'dev') // false
    console.log(process.env.NODE_ENV.length) // 4 (including a space at the end) 
    

    so, this may better:

    "start": "set NODE_ENV=dev&& node app.js"
    

    or

    console.log(process.env.NODE_ENV.trim() === 'dev') // true
    
    0 讨论(0)
  • 2020-11-28 19:02

    It is due to OS

    In your package.json, make sure to have your scripts(Where app.js is your main js file to be executed & NODE_ENV is declared in a .env file).Eg:

    "scripts": {
        "start": "node app.js",
        "dev": "nodemon server.js",
        "prod": "NODE_ENV=production & nodemon app.js"
      }
    

    For windows

    Also set up your .env file variable having NODE_ENV=development

    If your .env file is in a folder for eg.config folder make sure to specify in app.js(your main js file)

    const dotenv = require('dotenv'); dotenv.config({ path: './config/config.env' });

    0 讨论(0)
  • 2020-11-28 19:04

    in package.json we have to config like below (works in Linux and Mac OS)

    the important thing is "export NODE_ENV=production" after your build commands below is an example:

      "scripts": {
         "start": "export NODE_ENV=production && npm run build && npm run start-server",
         "dev": "export NODE_ENV=dev && npm run build && npm run start-server",
      } 
    
    • for dev environment, we have to hit "npm run dev" command

    • for a production environment, we have to hit "npm run start" command

    0 讨论(0)
  • 2020-11-28 19:04

    In macOS for those who are using the express version 4.x.x and using the DOTENV plugin, need to use like this:

    1. After installing the plugin import like the following in the file where you init the application: require('dotenv').config({path: path.resolve(__dirname+'/.env')});

    2. In the root directory create a file '.env' and add the varaiable like:

      NODE_ENV=development or NODE_ENV = development

    0 讨论(0)
提交回复
热议问题