Javascript process.env.NODE_ENV 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11104028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:28:25  来源:igfitidea点击:

process.env.NODE_ENV is undefined

javascriptnode.js

提问by basheps

I'm trying to follow a tutorial on NodeJs. I don't think I missed anything but whenever I call the process.env.NODE_ENVthe only value I get back is undefined. According to my research the default value should be 'development'. How is this value dynamically set and where is it set initially?

我正在尝试学习有关 NodeJs 的教程。我不认为我错过了任何东西,但是每当我打电话时,process.env.NODE_ENV我得到的唯一价值是未定义的。根据我的研究,默认值应该是“发展”。这个值是如何动态设置的,它最初是在哪里设置的?

回答by James Tikalsky

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

process.env 是对您环境的引用,因此您必须在那里设置变量。

To set an environment variable in Windows:

在 Windows 中设置环境变量

SET NODE_ENV=development

on OS X or Linux:

在 OS X 或Linux 上

export NODE_ENV=development

回答by kenberkeley

tips

提示

in package.json:

package.json

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

in app.js:

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

回答by mlaccetti

For people using *nix (Linux, OS X, etc.), there's no reason to do it via a second export command, you can chain it as part of the invoking command:

对于使用 *nix(Linux、OS X 等)的人,没有理由通过第二个导出命令来执行此操作,您可以将其链接为调用命令的一部分:

NODE_ENV=development node server.js

Easier, no? :)

更容易,不是吗?:)

回答by Jacob

We ran into this problem when working with node on Windows.

我们在 Windows 上使用 node 时遇到了这个问题。

Rather than requiring anyone who attempts to run the app to set these variables, we provided a fallback within the application.

我们没有要求任何试图运行应用程序的人设置这些变量,而是在应用程序中提供了一个后备。

var environment = process.env.NODE_ENV || 'development';

In a production environment, we would define it per the usual methods (SET/export).

在生产环境中,我们会按照通常的方法(SET/export)定义它。

回答by Liran H

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

您可以使用跨环境npm 包。它将负责修剪环境变量,并确保它可以跨不同平台工作。

In the project root, run:

在项目根目录中,运行:

npm install cross-env

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

然后在你的 package.json 中,在脚本下,添加:

"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:

然后环境变量将在您的应用程序中作为 可用process.env.NODE_ENV,因此您可以执行以下操作:

if (process.env.NODE_ENV === 'dev') {
  // Your dev-only logic goes here
}

回答by NRP

In macOSfor those who are using the express version 4.x.xand using the DOTENVplugin, need to use like this:

macOS 中对于那些使用 express 版本4.x.x并使用DOTENV插件的人,需要这样使用:

  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=developmentor NODE_ENV = development

  1. 在您初始化应用程序的文件中安装插件导入后,如下所示: require('dotenv').config({path: path.resolve(__dirname+'/.env')});

  2. 在根目录中创建一个文件 '.env' 并添加如下变量:

    NODE_ENV=development或者 NODE_ENV = development

回答by D V Yogesh

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

在 package.json 我们必须像下面这样配置(适用于 Linux 和 Mac OS)

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

重要的是在下面的构建命令示例之后“导出 NODE_ENV=production”:

  "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

  • 对于开发环境,我们必须点击“npm run dev”命令

  • 对于生产环境,我们必须点击“npm run start”命令

回答by Gilbert Flamino

In UBUNTU use:

在 UBUNTU 中使用:

$ export NODE_ENV=test

$ export NODE_ENV=test

回答by Adrian Almeida

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:

在你的 package.json 中,确保有你的脚本(其中 app.js 是你要执行的主要 js 文件,并且 NODE_ENV 在 .env 文件中声明)。例如:

"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

还要设置具有 NODE_ENV=development 的 .env 文件变量

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

如果您的 .env 文件位于 eg.config 文件夹的文件夹中,请确保在 app.js(您的主 js 文件)中指定

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

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

回答by Daniel

If you faced this probem in React, you need [email protected] and higher. Also for other environment variables than NODE_ENVto work in React, they need to be prefixed with REACT_APP_.

如果你在 React 中遇到这个问题,你需要 [email protected] 及更高版本。同样对于NODE_ENV在 React 中工作以外的其他环境变量,它们需要以REACT_APP_.