我试图使用一个简单的“颜色”模块在日志中设置cli-color,没什么特别的。
好吧,我colors.js
在path中调用了一个模块./app/config/colors.js
,其内容为:
var clc = require('cli-color');
var colors = {
ok: clc.cyan,
error: clc.red.bold,
warn: clc.yellowBright,
high: clc.white.bgGreen
};
module.exports = colors;
简单的。好吧,当我在server.js中(在项目的根目录,在/ app上方)需要它时,它可以正常工作,但是,当我尝试在其中使用它时,./app/config/db.js
会抛出一个错误:
Error: Cannot find module './app/config/colors.js'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/home/nano/Dev/bears-api/app/config/db.js:3:14)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
14 Sep 10:21:00 - [nodemon] app crashed - waiting for file changes before starting...
为什么可以在server.js中使用?
您可能需要使用相对路径的模块。
相对路径是根据需求模块的位置来解析的。
报价文件
前缀为“ ./”的模块相对于调用require()的文件。也就是说,circle.js必须与foo.js位于同一目录中,以便require('./ circle')可以找到它。
所以如果你做了
var whatever = require('./app/config/colors.js');
在位于./app/config/
节点中的模块内部时,它将查找./app/config/app/config/colors.js
并失败。
如果需求模块和需求模块都在同一目录中,请使用:
var whatever = require('./colors.js');
甚至更短:
var whatever = require('./colors');
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句