`import`语句的路径可以包含多个`/`吗?
Can the path of `import` statement contain multiple `/`?
我在一个目录中有两个文件,sum.mjs
和 main.mjs
。
sum.mjs
的内容是:
const sum = (a, b) => a + b;
export { sum };
而 main.mjs
是:
// import { sum } from './add.mjs';
import { sum } from './////////add.mjs';
console.log(sum(1, 1));
我发现这段代码可以工作,它可以通过运行 node main.mjs
输出结果2
。那么import
语句的路径是否可以包含多个/
?
另外,我发现import { sum } from './///\\\\/////add.mjs';
也可以很好用,很神奇。
有人可以解释一下吗?为什么会这样?
环境:macOS 上的节点 15.2.1
对于 POSIX 系统,请参阅 Pathname
Multiple successive <slash>
characters are considered to be the same as one <slash>
,
不确定 windows 会如何处理它。
此外,根据 Node 内部使用的内容,请参阅 path.normalize(path)
When multiple, sequential path segment separation characters are found (e.g. / on POSIX and either \ or / on Windows), they are replaced by a single instance of the platform-specific path segment separator (/ on POSIX and \ on Windows). Trailing separators are preserved.
我在一个目录中有两个文件,sum.mjs
和 main.mjs
。
sum.mjs
的内容是:
const sum = (a, b) => a + b;
export { sum };
而 main.mjs
是:
// import { sum } from './add.mjs';
import { sum } from './////////add.mjs';
console.log(sum(1, 1));
我发现这段代码可以工作,它可以通过运行 node main.mjs
输出结果2
。那么import
语句的路径是否可以包含多个/
?
另外,我发现import { sum } from './///\\\\/////add.mjs';
也可以很好用,很神奇。
有人可以解释一下吗?为什么会这样?
环境:macOS 上的节点 15.2.1
对于 POSIX 系统,请参阅 Pathname
Multiple successive
<slash>
characters are considered to be the same as one<slash>
,
不确定 windows 会如何处理它。
此外,根据 Node 内部使用的内容,请参阅 path.normalize(path)
When multiple, sequential path segment separation characters are found (e.g. / on POSIX and either \ or / on Windows), they are replaced by a single instance of the platform-specific path segment separator (/ on POSIX and \ on Windows). Trailing separators are preserved.