nitrojs / nitrojs/nitro

relative import of a `.js` file fails inside handlers

Open
#2,434 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug v2
Dominant language
TypeScript
Stars
11.2k
Forks
899
Avg merge
2d 24m
Merged PRs (30d)
40

Description

Environment
node: v18.18.0
nitropack: "2.9.6"
"express": "^4.19.2"
export default defineNitroConfig({
  srcDir: 'server',
  handlers: [
    {
      route: '/api',
      middleware: true,
      handler: './subsystem/index.ts',
    },
  ],
});
Reproduction

https://stackblitz.com/edit/github-zqsihv?file=subsystem%2Froutes%2Fone.js

  1. have the defined subsystem/index.ts import a .js relatively
…
import oneRoute from './routes/one.js';
…
  1. run dev
  2. error message: \
[worker reload] Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/subsystem/routes/one.js' imported from /home/projects/yqniqmbba.github/.nitro/dev/index.mjs
Describe the bug

it looks like the module is resolved from the filesystem root when a relative import of a .js file is encountered. index.mjs contains file paths with too many ../. I expect them to be resolved correctly, like .ts files.

https://github.com/nuxt/nuxt/issues/26150

Additional context

either removing srcDir or changing one.js to one.ts fixes the issue in the minimal example. only dev mode is affected, using build && preview, it runs fine.

Logs

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal StackBlitz reproduction and the generated .nitro/dev/index.mjs, using server/subsystem/index.ts and subsystem/routes/one.js as the import path. Run dev with srcDir set to server and compare the generated relative paths for .js and .ts imports. Done means the .js handler import resolves correctly in dev mode without changing the source extension; build and preview already work.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.