首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >DotEnv评论正在崩溃

DotEnv评论正在崩溃
EN

Stack Overflow用户
提问于 2022-04-28 10:09:00
回答 1查看 130关注 0票数 1

在我的NodeJS项目中,我使用的是dotenv16.0.0版本,但是最近添加的注释功能会导致崩溃。在没有注释的情况下,.env文件可以很好地工作,从它加载值。

.env文件内容:

代码语言:javascript
运行
复制
# Print out information during runtime, useful for debugging problems not caught. 
(true/false)
VERBOSE=false

# Database settings, update for actual deployment environment
DB_USERNAME=postgres
DB_PASSWORD=TINY_DUCK
DB_NAME=user_database
DB_HOST=localhost
DB_PORT=5432

运行NodeJS项目的命令是:

代码语言:javascript
运行
复制
mocha -r .env ./tests/testManager.js --exit

以及运行NodeJS项目时收到的错误消息:

代码语言:javascript
运行
复制
× ERROR: C:\Users\thega\Source\Repos\network\.env:1
# Print out information during runtime, useful for debugging problems not caught. (true/false)
^

SyntaxError: Invalid or unexpected token
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at exports.requireOrImport (C:\Users\thega\source\repos\network\node_modules\mocha\lib\nodejs\esm-utils.js:60:20)
    at async exports.handleRequires (C:\Users\thega\source\repos\network\node_modules\mocha\lib\cli\run-helpers.js:94:28)
    at async C:\Users\thega\source\repos\network\node_modules\mocha\lib\cli\run.js:353:25
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-28 10:15:01

在我看来,您似乎试图将.env文件作为JS模块导入,而不是用dotenv包加载它。

-r标志到mocha的意思是“要求”:

--在加载用户界面或测试文件之前,这需要一个模块。

它有助于:

增强内置或全局范围的harnesses

  • Assertion库(如使用esm

  • Compilers的should.js)

  • Instant ECMAScript模块,如通过@ Babel /寄存器使用babel或使用ts的TypeScript -节点(使用--需要ts-node/register).

))

因此,它将尝试以JavaScript的形式加载文件,当然,这是无法工作的。

相反,您可以要求dotenv/config,因此它将为您解析文件并相应地更新process.env

代码语言:javascript
运行
复制
mocha -r dotenv/config ./tests/testManager.js --exit

或者,如果您已经在代码中执行了require('dotenv').config(),那么这里根本不需要任何-r开关。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72041823

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档