sql-js / sql.js

A javascript library to run SQLite on the web.
http://sql.js.org
Other
12.39k stars 1.06k forks source link

Does not work in node as advertised #517

Open xnuk opened 2 years ago

xnuk commented 2 years ago

With given package.json:

{
    "type": "module",
    "packageManager": "pnpm@7.1.2",
    "dependencies": { "sql.js": "1.7.0" }
}

Here's the result of node -e 'require("sql.js")()':

(node:31583) ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
node:internal/deps/undici/undici:4813
            throw new TypeError("Failed to parse URL from " + input, { cause: err });
                  ^

TypeError: Failed to parse URL from /home/[redacted]/node_modules/.pnpm/sql.js@1.7.0/node_modules/sql.js/dist/sql-wasm.wasm
    at new Request (node:internal/deps/undici/undici:4813:19)
    at Agent.fetch2 (node:internal/deps/undici/undici:5505:29)
    ... 4 lines matching cause stack trace ...
    at /home/[redacted]/node_modules/.pnpm/sql.js@1.7.0/node_modules/sql.js/dist/sql-wasm.js:158:505
    at new Promise (<anonymous>)
    at initSqlJs (/home/[redacted]/node_modules/.pnpm/sql.js@1.7.0/node_modules/sql.js/dist/sql-wasm.js:22:24)
    at [eval]:1:18 {
  [cause]: TypeError [ERR_INVALID_URL]: Invalid URL
      at new NodeError (node:internal/errors:377:5)
      at URL.onParseError (node:internal/url:563:9)
      at new URL (node:internal/url:643:5)
      at new Request (node:internal/deps/undici/undici:4811:25)
      at Agent.fetch2 (node:internal/deps/undici/undici:5505:29)
      at Object.fetch (node:internal/deps/undici/undici:6332:20)
      at fetch (node:internal/bootstrap/pre_execution:196:25)
      at /home/[redacted]/node_modules/.pnpm/sql.js@1.7.0/node_modules/sql.js/dist/sql-wasm.js:158:271
      at /home/[redacted]/node_modules/.pnpm/sql.js@1.7.0/node_modules/sql.js/dist/sql-wasm.js:158:492
      at /home/[redacted]/node_modules/.pnpm/sql.js@1.7.0/node_modules/sql.js/dist/sql-wasm.js:158:505 {
    input: '/home/[redacted]/node_modules/.pnpm/sql.js@1.7.0/node_modules/sql.js/dist/sql-wasm.wasm',
    code: 'ERR_INVALID_URL'
  }
}

Node.js v18.3.0
jmandzik commented 1 year ago

I'm using node@18.5.0 with ES modules and had to explicitly set a path to the wasm binary:

  const wasmBinary = await readFile(new URL("../node_modules/sql.js/dist/sql-wasm.wasm", import.meta.url));
  const SQLJS = await initSql({
    wasmBinary,
  });

Hope that helps!

sigridjineth commented 1 year ago

@jmandzik I have failed to use the option you mentioned above. https://github.com/sql-js/sql.js/issues/522

The problem on my side is that the readFile does not allow to take more than one arguments. Which readFile module that you once import?

xnuk commented 1 year ago

@sigridjineth

The problem on my side is that the readFile does not allow to take more than one arguments.

Read again carefully, there's two parameters in new URL(..., ...), not await readFile(...)

Anutrix commented 7 months ago

I am facing the same issue on an existing project where the only change I did was in node version to 20.

Seems like Node 18 issue now. https://github.com/netlify/cli/issues/4610.