Skip to content

Node incorrectly resolves relative paths with directory symlinks #60715

Closed as not planned

Description

@noc7c9

Version

v25.2.0

Platform

$ uname -a
Darwin furnace 24.5.0 Darwin Kernel Version 24.5.0: Tue Apr 22 19:53:26 PDT 2025; root:xnu-11417.121.6~2/RELEASE_X86_64 x86_64

Subsystem

No response

What steps will reproduce the bug?

mkdir -p folder-1/nested/ folder-2/
echo 'console.log("Hello, world!")' > folder-2/code.js
ln -s folder-1/nested/ ./symlink-dir

File tree looks like this:

./
├── folder-1/
│   └── nested/
├── folder-2/
│   └── code.js
└── symlink-dir -> folder-1/nested/

This command fails since node resolves the path incorrectly.

node ./symlink-dir/../../folder-2/code.js

How often does it reproduce? Is there a required condition?

Above instructions always reproduces the issue.

What is the expected behavior? Why is that the expected behavior?

I expect node to run the code.js script and print "Hello, world!".

I tried other tools to see how they handle it, and other than Deno nothing else I tried had trouble.
eg:

❯❯❯ cat ./symlink-dir/../../folder-2/code.js
console.log("Hello, world!")

❯❯❯ realpath ./symlink-dir/../../folder-2/code.js
/Users/athir/projects/.tmp/node-symlink-bug/folder-2/code.js

❯❯❯ bun ./symlink-dir/../../folder-2/code.js
Hello, world!

❯❯❯ deno ./symlink-dir/../../folder-2/code.js  # deno has same issue
error: Module not found "file:///Users/athir/projects/.tmp/folder-2/code.js".

What do you see instead?

node:internal/modules/cjs/loader:1423
  throw err;
  ^

Error: Cannot find module '/Users/athir/projects/.tmp/folder-2/code.js'
    at Module._resolveFilename (node:internal/modules/cjs/loader:1420:15)
    at defaultResolveImpl (node:internal/modules/cjs/loader:1058:19)
    at resolveForCJSWithHooks (node:internal/modules/cjs/loader:1063:22)
    at Module._load (node:internal/modules/cjs/loader:1226:37)
    at TracingChannel.traceSync (node:diagnostics_channel:328:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:245:24)
    at Module.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:154:5)
    at node:internal/main/run_main_module:33:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

Node.js v25.2.0

Path node sees: /Users/athir/projects/.tmp/folder-2/code.js
Actual path: /Users/athir/projects/.tmp/node-symlink-bug/folder-2/code.js

Additional information

I encountered this while using pnpm which generates bin shims which suffer from this issue. I intend to create an issue there as well.

Activity

  1. changed the title [-]Paths with directory symlinks are resolved incorrectly when passed to the node bin[/-] [+]Node incorrectly resolves relative paths with directory symlinks[/+] on Nov 14, 2025
  2. richardlau commented on Nov 14, 2025

    @richardlau
    Member
  3. noc7c9 commented on Nov 14, 2025

    @noc7c9
    Author

    Thanks for letting me know about those flags but they didn't fix the issue.

    ❯❯❯ node --preserve-symlinks --preserve-symlinks-main symlink-dir/../../folder-2/code.js
    node:internal/modules/cjs/loader:1423
      throw err;
      ^
    
    Error: Cannot find module '/Users/athir/projects/.tmp/folder-2/code.js'
        at Module._resolveFilename (node:internal/modules/cjs/loader:1420:15)
        at defaultResolveImpl (node:internal/modules/cjs/loader:1058:19)
        at resolveForCJSWithHooks (node:internal/modules/cjs/loader:1063:22)
        at Module._load (node:internal/modules/cjs/loader:1226:37)
        at TracingChannel.traceSync (node:diagnostics_channel:328:14)
        at wrapModuleLoad (node:internal/modules/cjs/loader:245:24)
        at Module.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:154:5)
        at node:internal/main/run_main_module:33:47 {
      code: 'MODULE_NOT_FOUND',
      requireStack: []
    }
    
    Node.js v25.2.0
    
    ❯❯❯ node --preserve-symlinks-main symlink-dir/../../folder-2/code.js
    node:internal/modules/cjs/loader:1423
      throw err;
      ^
    
    Error: Cannot find module '/Users/athir/projects/.tmp/folder-2/code.js'
    ...
    
    ❯❯❯ node --preserve-symlinks symlink-dir/../../folder-2/code.js
    node:internal/modules/cjs/loader:1423
      throw err;
      ^
    
    Error: Cannot find module '/Users/athir/projects/.tmp/folder-2/code.js'
    ...
    

    Unless I'm misunderstanding something, I would've been surprised if those flags did fix it too given that the problem seems to be that Node is preserving the symlink path when it shouldn't be.

  4. siaeyy commented on Nov 18, 2025

    @siaeyy
    Contributor

    In your case, looks like symlinks in entry point are not respected:

    /** @type {string} */
    let mainEntry;
    // If requested, update process.argv[1] to replace whatever the user provided with the resolved absolute file path of
    // the entry point.
    if (expandArgv1 && process.argv[1] && process.argv[1][0] !== '-') {
    // Expand process.argv[1] into a full path.
    const path = require('path');
    try {
    mainEntry = path.resolve(process.argv[1]);
    process.argv[1] = mainEntry;
    } catch {
    // Continue regardless of error.
    }
    }

    This doesnt depend on the filesystem, so I think instead of just using path.resolve, we should use fs.realpathSync too.
    (path.resolve for when preserving symlinks)

    But seems like there is also one problem with fs.realpathSync:
    It handles relative parts first and then expands symlinks, rather than doing these in reverse order.

    I dont know if this is its normal behaviour but I assume it should be same as the native one (realpath(3))
    I am going to open another issue.

  5. github-actions commented on Jun 17, 2026

    @github-actions
    Contributor

    This issue has been marked as stale due to 210 days of inactivity.
    It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

  6. added
    stale
    on Jun 17, 2026
  7. github-actions commented on Jul 18, 2026

    @github-actions
    Contributor

    This issue has been automatically closed after 30 days of inactivity following its stale status (no activity for a total of 240 days).
    If this is still relevant, feel free to reopen it or leave a comment with additional details so we can continue the discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stale

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions