javascript – nodeJS require.path解决问题

我试图要求一个文件相对而神秘地发生以下情况

这很好,它指向/Users/marcos/Desktop/Taper/lib/utils.js

myPath = "/Users/marcos/Desktop/Taper/lib/./utils";
require(myPath);

这不只是它应该指向完全相同的文件

require.paths.unshift("/Users/marcos/Desktop/Taper/lib")
require("./utils"); //Doesn't work with './'
require("utils"); //Works Fine

任何人都知道为什么我不能使用./在这种情况下加载路径,因为

require("path").resolve("/Users/marcos/Desktop/Taper/lib","./utils")

结果是:

"/Users/marcos/Desktop/Taper/lib/utils"

无论如何?

提前致谢

解决方法

更新:

documentation

A module prefixed with '/' is an absolute path to the file. For
example,require('/home/marco/foo.js') will load the file at
/home/marco/foo.js.

A module prefixed with './' is relative to the file calling require().
That is,circle.js must be in the same directory as foo.js for
require('./circle') to find it.

Without a leading ‘/’ or ‘./’ to indicate a file,the module is either
a “core module” or is loaded from a node_modules folder.

If the given path does not exist,require() will throw an Error with
its code property set to 'MODULE_NOT_FOUND'.

这是原始的答案,它是指require.paths(不再支持):

documentation

在节点中,require.paths是一个字符串数组,它们表示当前缀’/’,’./’或’../’时要搜索模块的路径.

(强调我的)

相关文章

什么是深拷贝与浅拷贝?深拷贝与浅拷贝是js中处理对象或数据...
前言 今天复习了一些前端算法题,写到一两道比较有意思的题:...
最近在看回JavaScript的面试题,this 指向问题是入坑前端必须...
js如何实现弹出form提交表单?(图文+视频)
js怎么获取复选框选中的值
js如何实现倒计时跳转页面