nitrojs / nitrojs/nitro

Automatically add nitro module paths to be inlined

Open
#3,100 4 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Environment

Node v20.9.0, Nitropack 2.10.4

Reproduction

https://github.com/cjpearson/nitro-externals-test

To reproduce:

  • Inside nitro_module/, run yarn install && yarn unbuild
  • Inside nitro_app, run yarn install && yarn dev
Describe the bug

When adding a server handler through a module, if there are relative imports within the handler, it will crash in dev mode.

For example, the following will work in the prod build but fail with [cause]: Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/nitro-module/dist/runtime/utils/greeting.mjs' in dev.

import { defineEventHandler } from 'h3'
import { sayHello } from '../utils/greeting'
export default defineEventHandler((event) => {
    return sayHello()
})

This is a common issue reported with Nuxt, but is also reproducible with a pure nitro module.

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

There is a workaround described here, but with a large module the number of paths becomes quite large and it's easy to miss one as missing one does not guarantee a build error.

Additional context

No response

Logs

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 by reproducing the failure with the commands in nitro_module/ and nitro_app/, focusing on the server handler's relative import from ../utils/greeting. Trace how module paths are handled in dev mode versus the production build. Done means module-provided relative imports are automatically inlined and the dev server no longer raises ERR_MODULE_NOT_FOUND.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
backend, build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.