Huan
Huan

Reputation: 308

Can the path of `import` statement contain multiple `/`?

I have two files in one directory, sum.mjs and main.mjs.

The content of sum.mjs is:

const sum = (a, b) => a + b;

export { sum };

And the main.mjs is:

// import { sum } from './add.mjs';
import { sum } from './////////add.mjs';

console.log(sum(1, 1));

I find this code can work, it can output the result 2 by run node main.mjs. So can the path of the import statement contain multiple /?

In addition, I found import { sum } from './///\\\\\\\/////add.mjs'; can work well too, it's amazing.

Can someone explain this and why is this happening?

Environment: node 15.2.1 on macOS

Upvotes: 1

Views: 357

Answers (1)

Gabriele Petrioli
Gabriele Petrioli

Reputation: 195992

For POSIX systems see Pathname

Multiple successive <slash> characters are considered to be the same as one <slash>,

Not sure how windows would handle it.


Also, depending on what is used internally in Node, see 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.

Upvotes: 2

Related Questions