简体   繁体   English

REACT_APP_ * vs NODE_PATH环境变量

[英]REACT_APP_* vs NODE_PATH Environment Variables

What is the difference between a regular CRA environment variable (that MUST begin with REACT_APP_) and the NODE_PATH variable (that is used to avoid having to use relative paths for imports, ../../../foo etc.) 常规CRA环境变量(必须以REACT_APP_开头)和NODE_PATH变量(用于避免必须使用导入的相对路径,.. / .. / .. / foo等)之间有什么区别?

From my testing, no variables can be used unless they begin with REACT_APP, so how is the NODE_PATH variable not ignored like other variables that do not begin with REACT_APP? 从我的测试来看,除非它们以REACT_APP开头,否则不能使用任何变量,那么如何不像其他不以REACT_APP开头的变量那样忽略NODE_PATH变量?

NODE_PATH is accessible because it's added in the Webpack configuration in react-scripts . NODE_PATH是可访问的,因为它是在react-scripts中的Webpack配置中添加的。

You can see the code that adds it here: https://github.com/facebook/create-react-app/blob/25184c4e91ebabd16fe1cde3d8630830e4a36a01/packages/react-scripts/config/env.js#L61 你可以在这里看到添加它的代码: https//github.com/facebook/create-react-app/blob/25184c4e91ebabd16fe1cde3d8630830e4a36a01/packages/react-scripts/config/env.js#L61

You'll see in that file that you can also set NODE_ENV which sets which environment you are running in (eg development , staging , etc.) as well as PUBLIC_URL 您将在该文件中看到您还可以设置NODE_ENV来设置您正在运行的环境(例如developmentstaging等)以及PUBLIC_URL

To answer your question on the differences: 要回答有关差异的问题:

NODE_PATH allows you to use absolute paths for imports. NODE_PATH允许您使用绝对路径进行导入。 For example, you can set your NODE_PATH to the following: NODE_PATH=src:src/components:src/containers and then in your React code you could write something like this: import Button from 'button' instead of import Button from '../../../button' as long as you have that module in one of the folders src , src/components , or src/containers because create-react app will look for the button module those folders. 例如,您可以将NODE_PATH设置为以下内容: NODE_PATH=src:src/components:src/containers ,然后在您的React代码中,您可以编写如下内容: import Button from 'button' import Button from '../../../button'而不是import Button from '../../../button'只要你在srcsrc/componentssrc/containers文件夹中有一个模块,因为create-react app会查找button模块那些文件夹。 You wouldn't actually use the NODE_PATH variable in your React code that same way that you use REACT_APP_* variables. 你不会真正使用NODE_PATH变量在你的阵营代码,您使用的是同样的方式REACT_APP_*变量。 Basically, it tells your app where to look for modules. 基本上,它告诉您的应用程序在哪里寻找模块。

REACT_APP_* variables are injected into your application at build time using the Webpack configuration from the file I linked to. REACT_APP_*变量在构建时使用我链接到的文件中的Webpack配置注入到您的应用程序中。

This: const GRAPHQL_URI = REACT_APP_GRAPHQL_URI 这: const GRAPHQL_URI = REACT_APP_GRAPHQL_URI

becomes this: const GRAPHQL_URI = https://example.com 变为: const GRAPHQL_URI = https://example.com

if you have this in your .env file: REACT_APP_GRAPHQL_URI = https://example.com 如果你的.env文件中有这个: REACT_APP_GRAPHQL_URI = https://example.com

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

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