deno icon indicating copy to clipboard operation
deno copied to clipboard

vite/node project can't use i.e. `jsr:@db/sqlite` in package.json/node-compat mode. Migrating to deno.json will break resolution

Open birkskyum opened this issue 1 year ago • 11 comments

Version: Deno 2.0.3

Say I try to run a project, like a new solid start example

  • deno run -A npm:create-solid
  • Select SolidStart, TypeScript, Basic

And then I port package.json over to deno.json

{
  "tasks": {
    "dev": "vinxi dev",
    "build": "vinxi build",
    "start": "vinxi start",
    "version": "vinxi version"
  },
  "imports": {
    "@solidjs/meta": "npm:@solidjs/meta@^0.29.4",
    "@solidjs/router": "npm:@solidjs/router@^0.14.10",
    "@solidjs/start": "npm:@solidjs/start@^1.0.9",
    "solid-js": "npm:solid-js@^1.9.2",
    "vinxi": "npm:vinxi@^0.4.3"
  }
}

Now it'll break because it can't resolve any dependencies from these npm: packages that doesn't have the "npm:"

 ➜ Local:    http://localhost:3000/
  ➜ Network:  use --host to expose

8:13:04 PM [vite] Pre-transform error: Failed to load url solid-js/web (resolved id: solid-js/web) in /Users/admin/repos/deno-kitchensink/solid-start-basic/src/entry-server.tsx. Does the file exist? (x5)
8:13:04 PM [vite] Pre-transform error: Failed to load url @solidjs/start/server (resolved id: @solidjs/start/server) in /Users/admin/repos/deno-kitchensink/solid-start-basic/src/entry-server.tsx. Does the file exist?
8:13:04 PM [vite] Error when evaluating SSR module /src/entry-server.tsx: failed to import "solid-js/web"
|- Error: Cannot find module 'solid-js/web' imported from '/Users/admin/repos/deno-kitchensink/solid-start-basic/src/entry-server.tsx'
    at nodeImport (/Users/admin/Library/Caches/deno/npm/registry.npmjs.org/vite/5.4.10/dist/node/chunks/dep-BWSbWtLw.js:53047:19)
    at ssrImport (/Users/admin/Library/Caches/deno/npm/registry.npmjs.org/vite/5.4.10/dist/node/chunks/dep-BWSbWtLw.js:52914:22)
    at undefined

Staying in package.json doesn't allow usage of jsr:@db/sqlite

birkskyum avatar Oct 26 '24 18:10 birkskyum

Are you using https://github.com/denoland/deno-vite-plugin?

bartlomieju avatar Oct 26 '24 18:10 bartlomieju

Also look like there is a PR that tries to fix it too: https://github.com/vitejs/vite/pull/18479

bartlomieju avatar Oct 26 '24 18:10 bartlomieju

@bartlomieju , when i have everything in deno.json and I enable the deno() plugin it does take some effect, but the server start still prints:

vinxi starting dev server
Failed to resolve dependency: solid-js, present in 'optimizeDeps.include'
Failed to resolve dependency: solid-js/web, present in 'optimizeDeps.include'
Failed to resolve dependency: solid-js/store, present in 'optimizeDeps.include'
Failed to resolve dependency: solid-js/html, present in 'optimizeDeps.include'
Failed to resolve dependency: solid-js/h, present in 'optimizeDeps.include'
Failed to resolve dependency: @solidjs/start > source-map-js, present in 'optimizeDeps.include'
Failed to resolve dependency: @solidjs/start > error-stack-parser, present in 'optimizeDeps.include'
Failed to resolve dependency: solid-js, present in 'optimizeDeps.include'
Failed to resolve dependency: solid-js, present in 'optimizeDeps.include' (x2)
Failed to resolve dependency: solid-js/web, present in 'optimizeDeps.include'
Failed to resolve dependency: solid-js/web, present in 'optimizeDeps.include' (x2)
Failed to resolve dependency: solid-js/store, present in 'optimizeDeps.include'
Failed to resolve dependency: solid-js/store, present in 'optimizeDeps.include' (x2)
Failed to resolve dependency: solid-js/html, present in 'optimizeDeps.include'
Failed to resolve dependency: solid-js/html, present in 'optimizeDeps.include' (x2)
Failed to resolve dependency: solid-js/h, present in 'optimizeDeps.include'
Failed to resolve dependency: solid-js/h, present in 'optimizeDeps.include' (x2)
Failed to resolve dependency: @solidjs/start > source-map-js, present in 'optimizeDeps.include'
Failed to resolve dependency: @solidjs/start > source-map-js, present in 'optimizeDeps.include' (x2)
Failed to resolve dependency: @solidjs/start > error-stack-parser, present in 'optimizeDeps.include'
Failed to resolve dependency: @solidjs/start > error-stack-parser, present in 'optimizeDeps.include' (x2)

birkskyum avatar Oct 26 '24 18:10 birkskyum

My PR won't fix this though, it's my attempt that is just not working for some reason.

I have pure repo example here: https://github.com/Ciantic/deno-pure-solid-start that has also issues...

I get errors like this:

[h3] [unhandled] Error: Failed to load url ./src/database.ts (resolved id: ./src/database.ts) in deno::TypeScript::@db/sqlite::C:/Users/jarip/AppData/Local/deno/remote/https/jsr.io/ff2127150c5fc24dbc6b1daba62c248f566cce7c9346539da1003b4019d972c2. Does the file exist?

Ciantic avatar Oct 26 '24 18:10 Ciantic

@Ciantic, thansk! Maybe this ticket got a bit convoluted- maybe it's easiest to leave the jsr: issue for when SolidStart runs in deno.json.

birkskyum avatar Oct 26 '24 18:10 birkskyum

Basic solidStart works for me, in my attempt I get solidstart working without jsr packages.

As soon as I add jsr packages it fails.

Ciantic avatar Oct 26 '24 18:10 Ciantic

@Ciantic Oh really, you can run SolidStart without a package.json? Can we use Stackblitz for deno to repro these things like I do all the time with Node?

birkskyum avatar Oct 26 '24 18:10 birkskyum

@birkskyum yes, try this https://github.com/Ciantic/deno-pure-solid-start/tree/main

Then click the counter button.

It will error only when clicking use server functions with jsr packages.

Ciantic avatar Oct 26 '24 18:10 Ciantic

Aha, didn't know about the "dev": "deno run -A --node-modules-dir npm:vinxi dev" task syntax, just had vinxi dev... would have never guessed

birkskyum avatar Oct 26 '24 18:10 birkskyum

I've debugged it to somewhere in the loadAndTransform and transformRequest of vite, the call order is such.

First it always calls loadAndTransform and from there it gets a lot of relative URLs, like ./src/deno-sqlite-dialect.ts etc then fail to be transformed:

loadAndTransform deno::TypeScript::@soapbox/kysely-deno-sqlite::C:/Users/jarip/AppData/Local/deno/remote/https/jsr.io/ad3ae390e7fd79d5cf03ee16955d8caa114ad936157a5250fff3a45520a6cb09
transformRequest ./src/deno-sqlite-dialect.ts
transformRequest ./src/deno-sqlite3-dialect.ts
transformRequest ./src/poly-sqlite-dialect.ts
transformRequest ./src/poly-sqlite-driver.ts
transformRequest deno::TypeScript::@db/sqlite::C:/Users/jarip/AppData/Local/deno/remote/https/jsr.io/ff2127150c5fc24dbc6b1daba62c248f566cce7c9346539da1003b4019d972c2
loadAndTransform deno::TypeScript::@db/sqlite::C:/Users/jarip/AppData/Local/deno/remote/https/jsr.io/ff2127150c5fc24dbc6b1daba62c248f566cce7c9346539da1003b4019d972c2
transformRequest ./src/database.ts
transformRequest ./src/blob.ts
transformRequest ./src/statement.ts
transformRequest ./src/util.ts

I don't yet understand why it tries to do such a complicated thing for SSR modules.

Ciantic avatar Oct 27 '24 17:10 Ciantic

Good news everyone, I got jsr:@db/sqlite working just by using Deno's NPM compatibility mode, the .npmrc trick and using @jsr/db__sqlite import:

https://github.com/Ciantic/deno-solid-test/

⬆️ I've updated the above repository, and vite's dev server works now.

We have more problems in the nitro builder:

deno run -A build 
Task build vinxi build
vinxi v0.4.3


⚙  Building your app...
vinxi v0.4.3
(node:53524) ExperimentalWarning: Importing JSON modules is an experimental feature and might change at any time
(Use `node.EXE --trace-warnings ...` to show where the warning was created)
ReferenceError: Deno is not defined
    at file:///C:/Source/JavaScript/solid-start-test/deno-solid-test/node_modules/.deno/@[email protected]/node_modules/@jsr/std__fs/ensure_symlink.js:7:19
    at ModuleJob.run (node:internal/modules/esm/module_job:222:25)

However pure deno https://github.com/Ciantic/deno-pure-solid-start/ requires probably changes to Vite.

Ciantic avatar Oct 27 '24 19:10 Ciantic

I think this issue is fixed? On the latest Deno it's working perfectly for me. I have Vite, deno.json with a jsr package, deno-plugin-vite, and no package.json and it's working.

alexgleason avatar Apr 25 '25 17:04 alexgleason