首页 > 解决方案 > 为什么我收到错误:未定义配置属性“jwtPrivateKey”?

问题描述

当我通过 NodeJS 终端运行我的 index.js 文件时,我不断收到此错误消息。我的编辑器是 VS Code。我在 Windows 上。

我将 npm 配置模块加载到我的 index.js 文件中。我创建了一个自定义环境变量.json。我在节点终端中设置了密码 vidlyPrivateKey。我不知道为什么会这样。任何帮助将非常感激!

这是我的 index.js 代码:

const config = require('config');
try {
    const myKey = config.get("jwtPrivateKey");
    debug(myKey);
} catch (error) {
    debug(error, "FATAL ERROR: jwtPrivateKey is not defined.");
   return process.exit(1);
}

自定义环境变量.json:

{
    "jwtPrivateKey": "vidlyPrivateKey"
}

标签: node.jsconfig

解决方案


这是因为文件的命名,它应该在一个config文件夹内,名称为default.jsonconfig/default.json

在此处输入图像描述

这些是文档https://www.npmjs.com/package/config


推荐阅读