Thanks to visit codestin.com
Credit goes to github.com

Skip to content

createRequire not being transpiled / processed through jiti #369

@f0rr0

Description

@f0rr0

Environment

Node 22
Jiti 2

Reproduction

https://codesandbox.io/p/devbox/d2pgh5

Describe the bug

I am trying to import a typescript file using jiti that uses createRequire. This file is importing another file using the createRequire func. However, this required file is not processed through jiti.

Firstly the createRequire call fails if no extension is provided.

Secondly, if extension is provided, the typescript syntax in it is not recognized.

The debug logs show this file never hits jiti.

How can I ensure jiti processes the files required using createRequire

Additional context

No response

Logs

[jiti] [init] version: 2.4.2 module-cache: true fs-cache: true interop-defaults: true
[jiti] [cache] [hit] ./test.ts ~> /tmp/jiti/workspace-test.4a7478dd.mjs
[jiti] [transpile] [esm] ./test.ts (0.849ms)
node:internal/modules/cjs/loader:1143
  const err = new Error(message);
              ^

Error: Cannot find module './another'
Require stack:
- /project/workspace/test.ts
    at Module._resolveFilename (node:internal/modules/cjs/loader:1143:15)
    at Module._load (node:internal/modules/cjs/loader:984:27)
    at Module.require (node:internal/modules/cjs/loader:1231:19)
    at require (node:internal/modules/helpers:179:18)
    at /project/workspace/test.ts:5:16
    at async Function.import (/project/workspace/node_modules/jiti/dist/jiti.cjs:1:199772)
    at async file:///project/workspace/index.mjs:7:29 {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/project/workspace/test.ts' ]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions