简体   繁体   English

为什么 my.env 变量在 Parcel 中不起作用?

[英]Why my .env variables are not working in Parcel?

I'm trying to use env variables in Parcel parcel-bundler version 1.12.4 but it returns undefined.我正在尝试在 Parcel parcel-bundler version 1.12.4中使用 env 变量,但它返回未定义。 Here is my folder structure .这是我的文件夹结构 As you can see the .env file is in the same folder as the package.json file.如您所见, package.json文件与.env文件位于同一文件夹中。

This is the variable I'm using API_KEY=myapikey and I'm trying to console log it from my index.js file like this console.log(process.env.API_KEY) .这是我正在使用的变量API_KEY=myapikey ,我正在尝试从我的 index.js 文件中控制台记录它,例如console.log(process.env.API_KEY)

I just want to know if I'm doing something wrong or if need to open an issue.我只是想知道我是否做错了什么或者是否需要打开一个问题。 Thanks in advance!提前致谢!

Edit: I fixed it by deleting the .cache folder.编辑:我通过删除.cache文件夹来修复它。

You have to install npm i dotenv package你必须安装npm i dotenv package

Add the following command to index.js file:将以下命令添加到index.js文件中:
require('dotenv').config()

this imports your credentials from.env package and now you can access and use it in your project.这将从.env package 导入您的凭据,现在您可以在项目中访问和使用它。

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

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