nodejs require

时间:2023-03-09 04:04:03
nodejs require

The rules of where require finds the files can be a little complex, but a simple rule of thumb is that if the file doesn't start with "./" or "/", then it is either considered a core module (and the local Node path is checked), or a dependency in the local node_modules folder. If the file starts with "./" it is considered a relative file to the file that called require. If the file starts with "/", it is considered an absolute path. NOTE: you can omit ".js" and require will automatically append it if needed. For more detailed information, see the official docs.

Nodejs中的require方法,对文件进行搜索规则如上所述.

本人的理解是,

如果文件以"./"开头,则视为当前目录的文件;

如果文件以"/"开头,则视为绝对路径的文件;

其他情况是,先搜索Nodejs的Core Module, 然后搜索本地目录的node_moduls目录.