jiti icon indicating copy to clipboard operation
jiti copied to clipboard

Unable to import jiti script

Open jcalfee opened this issue 9 months ago • 1 comments

Environment

node v22.14.0 linux

Reproduction

https://github.com/jcalfee/jiti-cli-min-repo.git

npm i
$ ./src/cli.ts 
IT WORKS

$ npx cli
Error: Cannot find module './import-me.ts'
Require stack:
- /home/james/.npm/_npx/0b37f00474a50f52/node_modules/.bin/cli
    at Function._resolveFilename (node:internal/modules/cjs/loader:1225:15)
    at Function.resolve (node:internal/modules/helpers:146:19)
    at jitiResolve (/home/james/dev/vendor/jiti-link/node_modules/jiti/dist/jiti.cjs:1:187220)
    at jitiRequire (/home/james/dev/vendor/jiti-link/node_modules/jiti/dist/jiti.cjs:1:189288)
    at import (/home/james/dev/vendor/jiti-link/node_modules/jiti/dist/jiti.cjs:1:199778)
    at /home/james/.npm/_npx/0b37f00474a50f52/node_modules/.bin/cli:2:813
    at async /home/james/.npm/_npx/0b37f00474a50f52/node_modules/.bin/cli:2:778 {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/home/james/.npm/_npx/0b37f00474a50f52/node_modules/.bin/cli' ]
}

Extract

$ cat ./src/cli.ts 
#!/usr/bin/env jiti
await import('./import-me.ts')

$ egrep cli package.json 
  "name": "cli",
    "cli": "src/cli.ts"

Describe the bug

Just like running form the command line, when running a package.json "bin" target ("cli"), imports should resolve.

Additional context

No response

Logs


jcalfee avatar Mar 18 '25 18:03 jcalfee

This might be related to the symlink. npx cli creates a symlink for src/cli.ts at ~/.npm/_npx/7a931c1eca147ffd/node_modules/.bin/cli. Since Jiti doesn't resolve symlinks to their real paths, it fails to locate the module when attempting to import the relative path (./import-me.ts).

kricsleo avatar Apr 06 '25 08:04 kricsleo