跳到主要内容
CNode

var routes = require('./routes'); 报错

社区
Ddulante发布于 12 年前最后回复 12 年前
793690

为什么会出现这种问题呢 谁遇到过 难道不能require 一个文件夹? Cannot find module './routes'

var express = require('express') , routes = require('./routes') , api = require('./routes/api'); api 就可以正常require。

查看回复

回复 (7)

D
DavidCai1993#712 年前

学习了!

Z
zengwenbing#412 年前

node对于模块加载采取了三个步骤,路径分析,文件定位,模块编译. 路径分析是指分析模块是否是路径模块(./,../, /)和原生或者文件模块 你现在写的这个是路径模块,然后接着是文件定位 ./routes代表的是在当前目录下是否有routes.js, routes.node, routes.json,如果没有,那在找是否有routes文件夹,如果有 就返回里面的index.js文件,或者你通过在routes里面有个包配置文件package.json里面的main字段指向的文件,如果没有的,node进程就报not fount xx module

D
dulante#312 年前

明白了 require('./routes');默认指向的是routes下的index.js文件 如果这个文件不存在的话就会报错 其实本质还是require文件的

D
dulante#212 年前
引用 ciiii不能require一个文件夹。

可是之前的工程中我有用到过require(‘./routes’); 然后routes.filename这种用法都可以的

C
ciiii#112 年前

不能require一个文件夹。

参与回复

登录后即可参与回复。登录