Skip to content

[Bug]: Can not resolve paths when using require.resolve with paths option #410

@Timeless0911

Description

@Timeless0911

Environment

System:
OS: macOS 15.6.1
CPU: (10) arm64 Apple M1 Max
Memory: 569.75 MB / 64.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 22.18.0
npm: 10.9.3
pnpm: 10.18.0
bun: 1.1.30
Browsers:
Chrome: 140.0.7339.214
Safari: 18.6

Reproduction

https://github.com/Timeless0911/jiti-resolve-path-issue

  1. Run pnpm install
  2. Run cd libs/b && pnpm run jiti

Describe the bug

const path = require.resolve("is-odd", {
  paths: [require.resolve("a")],
});

console.log("path: ", path);

The libs/b/index.ts write a require.resolve with paths option to resolve is-odd.

In Node.js, is-odd can be resolved

> require.resolve("is-odd", {  paths: [require.resolve("a")],})
'<PATH>/node_modules/.pnpm/is-odd@3.0.1/node_modules/is-odd/index.js'

But when resolved with JavaScript API of jiti, it throws error Cannot find module 'is-odd'

> node jiti.ts

node:internal/modules/cjs/loader:1365
  const err = new Error(message);
              ^

Error: Cannot find module 'is-odd'
Require stack:
- <PATH>/libs/b/index.ts
    at Function._resolveFilename (node:internal/modules/cjs/loader:1365:15)
    at Function.resolve (node:internal/modules/helpers:145:19)

Additional context

If we use bin file of jiti, which is jiti index.ts, the error will not be threw since pnpm will generate NODE_PATH in node_modules/.bin/jiti.

I suspect that there is some missing in the paths options passed during the jiti resolve process or jiti should respect paths options user write when using require.resolve.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions