faunadb-js icon indicating copy to clipboard operation
faunadb-js copied to clipboard

The package "http2" wasn't found on the file system but is built into node

Open mccuna opened this issue 2 years ago • 11 comments

Description

I'm trying to use faunadb in a Remix project with Cloudflare Workers as a deployment method, but when I'm trying to build or run a local dev server I get the following error:

X [ERROR] Could not resolve "http2"

    node_modules/.pnpm/[email protected]/node_modules/faunadb/src/_http/http2Adapter.js:2:20:
      2 │ var http2 = require('http2')
        ╵                     ~~~~~~~

  The package "http2" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

Stack trace:

Build failed with 1 error:
node_modules/.pnpm/[email protected]/node_modules/faunadb/src/_http/http2Adapter.js:2:20: ERROR: Could not resolve "http2"
Error
    at Object.onBuildFailure (D:\Dev\Github\faunadb-cloudflare-workers-http2-issue\node_modules\.pnpm\@[email protected]_sfoxds7t5ydpegc3knd667wn6m\node_modules\@remix-run\dev\dist\cli\commands.js:167:13)
    at buildEverything (D:\Dev\Github\faunadb-cloudflare-workers-http2-issue\node_modules\.pnpm\@[email protected]_sfoxds7t5ydpegc3knd667wn6m\node_modules\@remix-run\dev\dist\compiler.js:287:13)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Object.build (D:\Dev\Github\faunadb-cloudflare-workers-http2-issue\node_modules\.pnpm\@[email protected]_sfoxds7t5ydpegc3knd667wn6m\node_modules\@remix-run\dev\dist\compiler.js:107:3)
    at async Object.build (D:\Dev\Github\faunadb-cloudflare-workers-http2-issue\node_modules\.pnpm\@[email protected]_sfoxds7t5ydpegc3knd667wn6m\node_modules\@remix-run\dev\dist\cli\commands.js:162:3)
    at async Object.run (D:\Dev\Github\faunadb-cloudflare-workers-http2-issue\node_modules\.pnpm\@[email protected]_sfoxds7t5ydpegc3knd667wn6m\node_modules\@remix-run\dev\dist\cli\run.js:479:7)

Steps to reproduce

Reproduction repo: https://github.com/mccuna/faunadb-cloudflare-workers-http2-issue

  1. npm install
  2. npm run build (the issue reproduces when running npm run dev:remix too, so it's not something related to the production build itself)

Extra notes

The application I'm working on makes use of 2 Cloudflare Workers:

  • Cloudflare Worker A that uses the Service Worker syntax and runs the Remix app powered by faunadb
  • Cloudflare Worker B that uses the Module Worker syntax and contains only faunadb interactions and 3rd party API calls

The Cloudflare Worker B using the Module Worker syntax has type: "module" in package.json and in this one, faunadb works perfectly fine.

mccuna avatar Jul 20 '22 15:07 mccuna

Internal ticket number is FE-2503

github-actions[bot] avatar Jul 20 '22 15:07 github-actions[bot]

Which version of Node.js, npm, and pnpm are you running?

Since pnpm tries to dedupe all of the Node modules installed on a system, you may be encountering a limitation where pnpm cannot yet properly handle Node.js's "built-in" modules.

To test that theory, can you replace your pnpm use with npm? If that works, then it might be worth filing an issue with the pnpm project.

faunaee avatar Jul 20 '22 18:07 faunaee

I've tried deleting the node_modules and using npm, but unfortunately the issue persists. The versions I'm using are:

  • node: 18.4.0
  • pnpm: 7.5.0
  • npm: 8.12.1

I believe the problem is related to the way the app bundle is created. I've put together a dummy repo with Cloudflare Workers & faunadb (https://github.com/mccuna/faunadb-cloudflare-worker) and it seems that:

  • if wrangler.toml has main = src/index.ts and wrangler handles the bundling, then then a similar error occurs
X [ERROR] [plugin checkForNodeBuiltins] Detected a Node builtin module import while Node compatibility is disabled.
Add node_compat = true to your wrangler.toml file to enable Node compatibility.

    node_modules/faunadb/src/_http/fetchAdapter.js:46:16:
      46 │       : require('http')
         ╵                 ~~~~~~

  This error came from the "onResolve" callback registered here:

    node_modules/wrangler/wrangler-dist/cli.js:121430:11:
      121430 │     build6.onResolve({
  • if wrangler.toml has main = "build/index.js" and I bundle the worker myself using esbuild and format:"esm". Then everything is ok

mccuna avatar Jul 20 '22 20:07 mccuna

Facing the same error when using the latest fresh install of Remix with the target set as Cloudflare Workers.

pbteja1998 avatar Jul 31 '22 08:07 pbteja1998

Same here, whatever options ES Build is using for Wrangler 2 is tripping over Fauna

TriangularCube avatar Aug 06 '22 20:08 TriangularCube

I notice that there is a PR that adds support for Cloudflare Module Workers. This PR has – sadly – not been merged yet.

Maybe this change will correct the problem with the Fauna driver?

runarj avatar Dec 27 '22 06:12 runarj

I am not sure, but it could be that this problem is two-fold. So to get the Fauna driver to work with Remix using Cloudflare Workers, two issues need to be addressed:

  1. Remix needs to use Cloudflare Module Worker and not Service Worker like today.
  2. Fauna needs to provide an EMS serverless driver like PlanetScale does.

runarj avatar Jan 16 '23 18:01 runarj

Still facing the same problem. Would be nice if there is at least some patch that we can add locally to make it work with wrangler2

pbteja1998 avatar Jan 27 '23 18:01 pbteja1998

Have you tried to do with Remix and Cloudfare Pages? (not CF workers)

anurag-loco avatar Jan 30 '23 05:01 anurag-loco

No, my production application is built with cloudflare workers and also I want to make use of Service Bindings. So, moving to cloudflare pages is a no-go for me at this time.

I want to use service bindings which will only work with Wrangler2, but wrangler2 doesn’t work with Fauna.

pbteja1998 avatar Jan 30 '23 06:01 pbteja1998

The latest version of Wrangler 2 (v2.9.1+) is compatible with the JS driver. But we are looking into what we can do to help with the fact that the default setup for Cloudflare and Remix is not working.

cc: @Shadid12

ptpaterson avatar Feb 21 '23 16:02 ptpaterson