简体   繁体   English

为什么 process.env.NODE_ENV 未定义?

[英]Why is process.env.NODE_ENV undefined?

I'm trying to follow a tutorial on NodeJS.我正在尝试遵循有关 NodeJS 的教程。 I don't think I missed anything but whenever I call the process.env.NODE_ENV the only value I get back is undefined .我不认为我错过了任何东西,但每当我调用process.env.NODE_ENV时,我得到的唯一值是undefined According to my research the default value should be development .根据我的研究,默认值应该是development How is this value dynamically set and where is it set initially?这个值是如何动态设置的,最初是在哪里设置的?

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 macOS / OS X or Linux :在 macOS / OS X 或Linux 上

export NODE_ENV=development

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

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?更容易,不是吗? :) :)

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)定义它。

You can use the cross-env npm package.您可以使用环境 npm 包。 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:然后在你的 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
}

In macOS for those who are using the express version 4.xx and using the DOTENV plugin, need to use like this:macOS中,对于使用 express 版本4.xx并使用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')});在初始化应用程序的文件中安装插件导入后,如下所示: require('dotenv').config({path: path.resolve(__dirname+'/.env')});

  2. In the root directory create a file '.env' and add the varaiable like:在根目录中创建一个文件 '.env' 并添加如下变量:

    NODE_ENV=development or NODE_ENV = development NODE_ENV=developmentNODE_ENV = development

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对于开发环境,我们必须点击“npm run dev”命令

  • for a production environment, we have to hit "npm run start" command对于生产环境,我们必须点击“npm run start”命令

尽早在您的应用程序中要求并配置 dotenv。

require('dotenv').config()

In UBUNTU use:在 UBUNTU 中使用:

$ export NODE_ENV=test $ 出口 NODE_ENV=测试

install dotenv module ( npm i dotenv --save )安装 dotenv 模块( npm i dotenv --save

require('dotenv').config() //write inside the file where you will use the variable require('dotenv').config() //写在你将使用变量的文件中

console.log(process.env.NODE_ENV) // returns value stored in .env file console.log(process.env.NODE_ENV) // 返回存储在 .env 文件中的值

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还要设置你的 .env 文件变量 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)如果您的 .env 文件位于 eg.config 文件夹的文件夹中,请确保在 app.js(您的主 js 文件)中指定

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

You can use the dotenv package from npm, here is the link: https://www.npmjs.com/package/dotenv你可以使用 npm 的dotenv包,这里是链接: https ://www.npmjs.com/package/dotenv

Which allows you to place all your configuration in a .env file它允许您将所有配置放在.env文件中

Must be the first require in app.js必须是 app.js 中的第一个 require

npm install dotenv

require("dotenv").config();

In my case, I have a node app.就我而言,我有一个节点应用程序。 The way I have structured it is that I have client folder and server folder.我构建它的方式是我有客户端文件夹和服务器文件夹。 I had my .env file inline with these two folder.我的 .env 文件与这两个文件夹内联。 My server file needs the .env file.我的服务器文件需要 .env 文件。 It was returning undefined because it did not live inside the server file.它返回未定义,因为它不在服务器文件中。 It was an oversight.这是一个疏忽。

App
-client
-server
-.env

Instead I moved .env inside server file like so:

App
-client
-server
 |-.env <---here
 |-package.json
 |-and the rest of the server files...

(before this - ofcourse have the dotenv npm package installed and follow its doc)

If you faced this probem in React, you need react-scripts@0.2.3 and higher.如果你在 React 中遇到过这个问题,你需要 react-scripts@0.2.3 及更高版本。 Also for other environment variables than NODE_ENV to work in React, they need to be prefixed with REACT_APP_ .此外,对于NODE_ENV以外的其他环境变量在 React 中工作,它们需要以REACT_APP_为前缀。

For me, the issue was that I was using the pkg library to turn my app into an executable binary.对我来说,问题在于我正在使用pkg 库将我的应用程序转换为可执行的二进制文件。 In that case, the accepted solutions didn't work.在这种情况下,公认的解决方案不起作用。 However, using the following code solved my problem:但是,使用以下代码解决了我的问题:

const NODE_ENV = (<any>process).pkg ? 'production' : process.env.NODE_ENV;

I found this solutionhere on GitHub.我在 GitHub 上找到这个解决方案。

In Electron Js在电子 Js 中

"scripts": {
    "start": "NODE_ENV=development electron index.js",
},

如果您使用名称process定义任何函数,那么这也可能导致此问题。

Defining process.env.NODE_ENV in package.json for Windows/Mac/Linux:在 Windows/Mac/Linux 的 package.json 中定义 process.env.NODE_ENV:

Here's what worked for me on my Mac (MacBook Pro 2019, 16 inch, Big Sur):以下是在我的 Mac(MacBook Pro 2019,16 英寸,Big Sur)上对我有用的方法:

"scripts": {
    "build": "export NODE_ENV=prod || set NODE_ENV=prod&& npx eslint . && node --experimental-json-modules ./backend/app.js && gulp",
},

Using the export NODE_ENV=prod || set NODE_ENV=prod&&使用export NODE_ENV=prod || set NODE_ENV=prod&& export NODE_ENV=prod || set NODE_ENV=prod&& string may work in Windows and Linux but I haven't tested that. export NODE_ENV=prod || set NODE_ENV=prod&& string 可能适用于 Windows 和 Linux,但我还没有测试过。

If someone could confirm that would be great.如果有人可以确认那将是很棒的。

Unfortunately using the cross-env npm package did NOT work for me at all in my package.json file and I spend a long time on my Mac trying to make this work.不幸的是,在我的package.json文件中使用cross-env npm 包对我来说根本不起作用,我在 Mac 上花了很长时间试图完成这项工作。

I also faced this issue.我也遇到过这个问题。 I moved .env file to the root folder (not the project folder, a level higher) and it worked out.我将 .env 文件移动到根文件夹(不是项目文件夹,更高级别)并且它成功了。

Check it.核实。 it might help you as well它也可能对您有所帮助

也可以通过代码设置,例如:

process.env.NODE_ENV = 'test';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM