在我的index.js文件中,我将const config = require('config');写成了第一行。
我的项目文件夹中有一个名为config.js的文件
但是我一直让我的控制台告诉我它是Cannot find module 'config'
我的配置文件基本上是这样的:
module.exports = {
'secretKey': 'mySecretCode12232',
'mongoUrl' : 'mongodb://localhost:27017/test'
};这没有任何意义,它应该是工作的。
发布于 2017-06-24 21:35:18
当您在require语句中没有提供任何路径选择器时(例如,require('./config')),您的代码将搜索名为config的包并失败,因为它找不到这个特定的包,因为require将假设它是提供的包名(并将开始搜索,例如在您的node_modules等中-搜索它的路径不是一个微不足道的主题:) )。
如果您想从另一个文件中请求该模块,则必须提供该模块的正确路径,因此假设您的config.js与您的其他文件位于同一目录中,则正确的语句应该是:
const config = require('./config'); // Extension can be omitted
https://stackoverflow.com/questions/44727924
复制相似问题