Node.js Path.join在Nodejs中的跨平台请求

Node.js Path.join在Nodejs中的跨平台请求,node.js,path,cross-platform,filepath,Node.js,Path,Cross Platform,Filepath,我记得很久以前在NodeJS中,为了跨平台加载本地模块,需要使用path.join。我只是想知道,我们仍然需要这样的策略,还是节点12+不再需要这样的策略 谢谢 例如 而不是写作 const i=require('./config/module') 这两种方法都可以很好地工作,并且在我记忆中的node.js中一直都很好。require()的模块加载规则有完整的文档记录。您的具体案例应包含在第一节和步骤3a中,即: require(X) from module at path Y 1. If X

我记得很久以前在NodeJS中,为了跨平台加载本地模块,需要使用path.join。我只是想知道,我们仍然需要这样的策略,还是节点12+不再需要这样的策略

谢谢

例如

而不是写作

const i=require('./config/module')

这两种方法都可以很好地工作,并且在我记忆中的node.js中一直都很好。
require()
的模块加载规则有完整的文档记录。您的具体案例应包含在第一节和步骤3a中,即:

require(X) from module at path Y
1. If X is a core module,
   a. return the core module
   b. STOP
2. If X begins with '/'
   a. set Y to be the filesystem root
3. If X begins with './' or '/' or '../'
   a. LOAD_AS_FILE(Y + X)
您可能需要使用
path.join()
的时间是指您可能正在构造一条路径,并将其发送到需要特定于操作系统的路径分隔符的外部模块,或者您只需要
path.join()
来处理,以确保要加入的各个部分之间有且只有一个路径分隔符(例如,当您不知道单个片段是否已经有前导斜杠或尾随斜杠时)


require()
require(X) from module at path Y
1. If X is a core module,
   a. return the core module
   b. STOP
2. If X begins with '/'
   a. set Y to be the filesystem root
3. If X begins with './' or '/' or '../'
   a. LOAD_AS_FILE(Y + X)