gpt4 book ai didi

node.js - NodeJs : dotenv not working for nested paths

转载 作者:太空宇宙 更新时间:2023-11-03 23:24:12 24 4
gpt4 key购买 nike

我有一个带有我自己的自定义文件结构的 React 项目。这是项目结构的图像。 File structure

我的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

API_URL = http://54.254.198.83:1441/

控制台输出

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

为什么 .env 变量没有添加到 process.env 中?

最佳答案

这就是 dotenv 使用嵌套路径的方式:

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

将其放在您希望 .env 变量在其中可用的文件的顶部。

关于node.js - NodeJs : dotenv not working for nested paths,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46323351/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com