简体   繁体   English

NodeJs:dotenv不适用于嵌套路径

[英]NodeJs : dotenv not working for nested paths

I have react project with my own custom file structure. 我有我自己的自定义文件结构的反应项目。 Here is an image for the project structure. 这是项目结构的图像。 档案结构

My index.js 我的index.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import dotenv from 'dotenv'
dotenv.config({path:'../.env'});
console.log(process.env)
ReactDOM.render(<App />, document.getElementById('root'));

.env .ENV

API_URL = http://54.254.198.83:1441/

console output 控制台输出

{NODE_ENV: "development", PUBLIC_URL: ""}

Why are the .env variables not added to process.env ? 为什么.env变量未添加到process.env中?

This is how you get dotenv working with nested paths: dotenv处理嵌套路径的方式:

require('dotenv').config({path:'relative/path/to/your/.env'})

Place this at the top of the file you want your .env vars available within. 将其放置在您希望.env var可在其中使用的文件的顶部。

Try directly setting environmental variables without dotenv 尝试直接设置环境变量而不使用dotenv

Set variables when running your application. 在运行应用程序时设置变量。

API_URL = "http://54.254.198.83:1441/" npm start

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

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