Cannot load externalized packages (e.g. `@electric-sql/pglite`) in the Vitest `nitro` environment
Maintainers usually reply within 2 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 73/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- node.js, typescript, vite
Research direction
Start with the provided StackBlitz reproduction and inspect the Vitest nitro environment path that loads api/db.ts and its externalized package imports. Run the shown pnpm test reproduction and compare it with vite dev; verify whether the noExternals setting changes the failure. Done means the /api/db test returns status 200 without relying on that workaround.
Written by the indexing model from the issue text.
Description
Environment
{
"dependencies": {
"@electric-sql/pglite": "^0.5.16",
"drizzle-orm": "1.0.0-rc.5"
},
"devDependencies": {
"nitro": "3.0.261103-rc",
"vite": "8.3.7",
"vitest": "5.1.0"
}
}
Node v24.21.0
Reproduction
https://stackblitz.com/edit/unjs-nitro-starter-ur33yfke
Describe the bug
In the vitest nitro environment, a server module that imports an externalized package fails to load.
// api/db.ts
import { PGlite } from '@electric-sql/pglite';
import { drizzle } from 'drizzle-orm/pglite';
import { defineHandler } from 'nitro/h3';
export default defineHandler(async () => {
const client = new PGlite();
const db = drizzle({ client });
return db.execute('SELECT NOW()');
});
// test/repro.test.ts
import { serverFetch } from 'nitro'
import { expect, it } from 'vitest'
it('serves /api/db', async () => {
const res = await serverFetch('/api/db')
expect(res.status).toBe(200) // actual: 500
})
Error:
Cannot find module 'file:///…/node_modules/.pnpm/@[email protected]/node_modules/@electric-sql/pglite/dist/index.js'
imported from /…/server/api/db.ts
Additional context
- only fails in the test, while
vite devworks - Adding
noExternals: [/node_modules\//]to config makes the test pass - same error shows in npm as well
Logs
❯ pnpm test
> nitro-fileurl-repro@ test /home/projects/unjs-nitro-starter-ur33yfke
> vitest run
RUN v5.1.0 /home/projects/unjs-nitro-starter-ur33yfke
stderr | test/repro.test.ts > serves /api/db (imports drizzle-orm/pglite)
Error: Cannot find module 'file:///home/projects/unjs-nitro-starter-ur33yfke/node_modules/.pnpm/@[email protected]/node_modules/@electric-sql/pglite/dist/index.js' imported from /home/projects/unjs-nitro-starter-ur33yfke/api/db.ts
at reviveInvokeError (file:///home/projects/unjs-nitro-starter-ur33yfke/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/module-runner.js:571:14)
... 8 lines matching cause stack trace ...
at async eval (file:///home/projects/unjs-nitro-starter-ur33yfke/node_modules/.pnpm/[email protected][email protected]/node_modules/vitest/dist/chunks/run.CjdgFZiW.js:2846:20) {
cause: Error: Cannot find module 'file:///home/projects/unjs-nitro-starter-ur33yfke/node_modules/.pnpm/@[email protected]/node_modules/@electric-sql/pglite/dist/index.js' imported from /home/projects/unjs-nitro-starter-ur33yfke/api/db.ts
at reviveInvokeError (file:///home/projects/unjs-nitro-starter-ur33yfke/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/module-runner.js:571:14)
at Object.invoke (file:///home/projects/unjs-nitro-starter-ur33yfke/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/module-runner.js:587:33)
at async VitestModuleRunner.getModuleInformation (file:///home/projects/unjs-nitro-starter-ur33yfke/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/module-runner.js:1227:7)
at async request (file:///home/projects/unjs-nitro-starter-ur33yfke/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/module-runner.js:1244:83)
at /home/projects/unjs-nitro-starter-ur33yfke/api/db.ts:2:1
at async VitestModuleEvaluator._runInlinedModule (file:///home/projects/unjs-nitro-starter-ur33yfke/node_modules/.pnpm/[email protected][email protected]/node_modules/vitest/dist/module-evaluator.js:281:4)
at async VitestModuleRunner.directRequest (file:///home/projects/unjs-nitro-starter-ur33yfke/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/module-runner.js:1287:59)
at async VitestModuleRunner.cachedRequest (file:///home/projects/unjs-nitro-starter-ur33yfke/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/module-runner.js:1194:73)
at /home/projects/unjs-nitro-starter-ur33yfke/test/repro.test.ts:10:15
at async eval (file:///home/projects/unjs-nitro-starter-ur33yfke/node_modules/.pnpm/[email protected][email protected]/node_modules/vitest/dist/chunks/run.CjdgFZiW.js:2846:20) {
code: 'ERR_MODULE_NOT_FOUND',
runnerError: Error: RunnerError
at reviveInvokeError (file:///home/projects/unjs-nitro-starter-ur33yfke/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/module-runner.js:572:64)
at Object.invoke (file:///home/projects/unjs-nitro-starter-ur33yfke/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/module-runner.js:587:33)
at async VitestModuleRunner.getModuleInformation (file:///home/projects/unjs-nitro-starter-ur33yfke/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/module-runner.js:1227:7)
at async request (file:///home/projects/unjs-nitro-starter-ur33yfke/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/module-runner.js:1244:83)
at /home/projects/unjs-nitro-starter-ur33yfke/api/db.ts:2:1
at async VitestModuleEvaluator._runInlinedModule (file:///home/projects/unjs-nitro-starter-ur33yfke/node_modules/.pnpm/[email protected][email protected]/node_modules/vitest/dist/module-evaluator.js:281:4)
at async VitestModuleRunner.directRequest (file:///home/projects/unjs-nitro-starter-ur33yfke/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/module-runner.js:1287:59)
at async VitestModuleRunner.cachedRequest (file:///home/projects/unjs-nitro-starter-ur33yfke/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/module-runner.js:1194:73)
at /home/projects/unjs-nitro-starter-ur33yfke/test/repro.test.ts:10:15
at async eval (file:///home/projects/unjs-nitro-starter-ur33yfke/node_modules/.pnpm/[email protected][email protected]/node_modules/vitest/dist/chunks/run.CjdgFZiW.js:2846:20)
},
status: 500,
statusText: undefined,
headers: undefined,
data: undefined,
body: undefined,
unhandled: true
}
stderr | test/repro.test.ts > serves /api/db (imports drizzle-orm/pglite)
[request error] [GET] http://localhost/api/db
Error: Cannot find module 'file:///home/projects/unjs-nitro-starter-ur33yfke/node_modules/.pnpm/@[email protected]/node_modules/@electric-sql/pglite/dist/index.js' imported from /home/projects/unjs-nitro-starter-ur33yfke/api/db.ts
1 │ // Any server module that imports an externalized package triggers the bug.
❯ 2 │ import { PGlite } from '@electric-sql/pglite';
│ ^
3 │ import { drizzle } from 'drizzle-orm/pglite';
4 │ import { defineHandler } from 'nitro/h3';
at reviveInvokeError (vite/dist/node/module-runner.js:571:14)
at Object.invoke (vite/dist/node/module-runner.js:587:33)
at async VitestModuleRunner.getModuleInformation (vite/dist/node/module-runner.js:1227:7)
at async request (vite/dist/node/module-runner.js:1244:83)
at ./api/db.ts:2:1
at async VitestModuleEvaluator._runInlinedModule (vitest/dist/module-evaluator.js:281:4)
at async VitestModuleRunner.directRequest (vite/dist/node/module-runner.js:1287:59)
at async VitestModuleRunner.cachedRequest (vite/dist/node/module-runner.js:1194:73)
at ./test/repro.test.ts:10:15
at async eval (vitest/dist/chunks/run.CjdgFZiW.js:2846:20)
❯ test/repro.test.ts (2 tests | 1 failed) 655ms
✓ serves /api/health 4ms
× serves /api/db (imports drizzle-orm/pglite) 14ms
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
FAIL test/repro.test.ts > serves /api/db (imports drizzle-orm/pglite)
AssertionError: expected 500 to be 200 // Object.is equality
- Expected
+ Received
- 200
+ 500
❯ test/repro.test.ts:11:22
9| it('serves /api/db (imports drizzle-orm/pglite)', async () => {
10| const res = await serverFetch('/api/db')
11| expect(res.status).toBe(200)
| ^
12| })
13|
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯
Test Files 1 failed (1)
Tests 1 failed | 1 passed (2)
Start at 15:53:13
Duration 1.46s (tests 59%, transform 24%, import 14%, setup 1%, environment 1%, worker 1%)
ELIFECYCLE Test failed. See above for more details.
- Dominant language
- TypeScript
- Stars
- 11.5k
- Forks
- 968
- Avg merge
- 4d 9h
- Merged PRs (30d)
- 64
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from nitrojs/nitro
-
pending triage
Difficulty 1/5 Under an hour Newbie friendliness 92/100
Maintainers usually reply within 2 days
-
Difficulty 1/5 Under an hour Newbie friendliness 95/100
Maintainers usually reply within 2 days
-
cloudflare: `nitro deploy` fails when a parent directory has a Wrangler configPossibly taken @draarivpatel-ui claimed this 13 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 73/100
Maintainers usually reply within 2 days
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 2 days
-
pending triage
Difficulty 5/5 Over a week Newbie friendliness 30/100
nitrojs/nitro#5205 · 1 reaction ·
Maintainers usually reply within 2 days
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
ranxianglei/billion-context#8319 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
JimLiu/open-artifact#126 ·
-
bug repro-ready
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
mksglu/context-mode#1454 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
laststance/design-world#98 ·
Maintainers usually reply within 1 day
-
Add: KCM True crimeOpenapproved check:passed streams:add
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day