在我的NodeJS项目中,我使用的是dotenv16.0.0版本,但是最近添加的注释功能会导致崩溃。在没有注释的情况下,.env文件可以很好地工作,从它加载值。
.env文件内容:
# 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项目的命令是:
mocha -r .env ./tests/testManager.js --exit
以及运行NodeJS项目时收到的错误消息:
× 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
发布于 2022-04-28 02:15:01
在我看来,您似乎试图将.env
文件作为JS模块导入,而不是用dotenv包加载它。
-r
标志到mocha
的意思是“要求”:
--在加载用户界面或测试文件之前,这需要一个模块。
它有助于:
增强内置或全局范围的harnesses
))
因此,它将尝试以JavaScript的形式加载文件,当然,这是无法工作的。
相反,您可以要求dotenv/config
,因此它将为您解析文件并相应地更新process.env
:
mocha -r dotenv/config ./tests/testManager.js --exit
或者,如果您已经在代码中执行了require('dotenv').config()
,那么这里根本不需要任何-r
开关。
https://stackoverflow.com/questions/72041823
复制相似问题