简体   繁体   English

错误 - 找不到模块“config”

[英]Error - Cannot find module 'config'

I have mail configuration code in all.js .我在all.js中有邮件配置代码。

Now i am trying to import this in my mail.js service, so i imported the config module as follows:-现在我正在尝试将其导入到我的mail.js服务中,因此我按如下方式导入了config模块:-

mail.js邮件.js

config = require('config'),

all.js全部.js

mailer: {
        auth: {
            user: "XXXXXXX",
            pass: "abc@123"
        }
    }

在此处输入图像描述

Gives me error cannot find module, but the module exists i have checked it.给我错误找不到模块,但模块存在我已经检查过了。

How to solve this?如何解决这个问题?

我使用以下代码并且它有效: -

config = require('../config/config');

This also occurs when you don't have the config package installed.当您没有安装配置 package 时,也会发生这种情况。 You may just need to install the config package您可能只需要安装配置 package

npm i config

so you have to modify the path relative to the file you are using process.env not relative to the specified file config.json所以你必须修改相对于你正在使用的文件的路径 process.env 而不是相对于指定文件 config.json

eg:例如:

node_modules
.gitignore
.env
config.json
folder:
     file.js

using config.json in file.js process.env.SECRET_KEY在 file.js process.env.SECRET_KEY 中使用 config.json

.env: SECRET_KEY=../config.json //because you go out of folder in file.js to get into config.json

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

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