Hebilicious / authjs-nuxt

AuthJS edge-compatible authentication Nuxt module.
https://authjs-nuxt.pages.dev/
MIT License
252 stars 28 forks source link

npm overrides behave differently from pnpm resolutions #80

Closed bisand closed 1 year ago

bisand commented 1 year ago

Environment

Related to Cloudflare pages

Inside devcontainer:

$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 12 (bookworm)
Release:        12
Codename:       bookworm

Outside devcontainer:

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 23.04
Release:    23.04
Codename:   lunar

Node:

$ node --version
v18.17.0
$ npm --version
9.8.1

from packages.json

  "devDependencies": {
    "@nuxt/devtools": "latest",
    "@types/node": "^18.17.3",
    "nuxt": "^3.6.5"
  },
  "dependencies": {
    "@auth/core": "^0.10.0",
    "h3": "1.8.0-rc.3",
    "@hebilicious/authjs-nuxt": "^0.3.0-beta.2",
    "nitropack": "npm:nitropack-edge@latest"
  },
  "overrides": {
    "h3": "1.8.0-rc.3",
    "nitropack": "npm:nitropack-edge@latest"
  }

Reproduction

Can be reproduced from here using devcontainer or not: https://github.com/bisand/blog-test

Build step:

$ npm run build

> build
> nuxt build

Nuxi 3.6.5                                                                                                                                                             6:13:08 AM
Nuxt 3.6.5 with Nitro 2.5.2                                                                                                                                            6:13:08 AM
ℹ Adding @auth/nuxt module... { verifyClientOnEveryRequest: true,                                                                                          @auth/nuxt 6:13:09 AM
  guestRedirectTo: '/',
  authenticatedRedirectTo: '/',
  baseUrl: '' }
✔ Added @auth/nuxt module successfully.                                                                                                                    @auth/nuxt 6:13:09 AM
ℹ Building client...                                                                                                                                                  6:13:10 AM
ℹ vite v4.3.9 building for production...                                                                                                                              6:13:10 AM
transforming (85) node_modules/pathe/dist/index.mjs
[6:13:11 AM]  WARN  [plugin:vite:resolve] Module "node:crypto" has been externalized for browser compatibility, imported by "/workspaces/blog-test/node_modules/@hebilicious/authjs-nuxt/dist/runtime/lib/server.mjs". See http://vitejs.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.

ℹ ✓ 256 modules transformed.                                                                                                                                          6:13:12 AM
ℹ .nuxt/dist/client/manifest.json                                2.78 kB │ gzip:  0.48 kB                                                                             6:13:13 AM
ℹ .nuxt/dist/client/_nuxt/error-500.c5df6088.css                 1.95 kB │ gzip:  0.74 kB                                                                             6:13:13 AM
ℹ .nuxt/dist/client/_nuxt/error-404.7fc72018.css                 3.63 kB │ gzip:  1.12 kB                                                                             6:13:13 AM
ℹ .nuxt/dist/client/_nuxt/entry.3c4ebcd6.css                    12.13 kB │ gzip:  2.49 kB                                                                             6:13:13 AM
ℹ .nuxt/dist/client/_nuxt/__vite-browser-external.b25bb000.js    0.03 kB │ gzip:  0.05 kB                                                                             6:13:13 AM
ℹ .nuxt/dist/client/_nuxt/guest-only.36354345.js                 0.37 kB │ gzip:  0.24 kB                                                                             6:13:13 AM
ℹ .nuxt/dist/client/_nuxt/auth.9340b1e7.js                       0.37 kB │ gzip:  0.24 kB                                                                             6:13:13 AM
ℹ .nuxt/dist/client/_nuxt/test.3851b75e.js                       0.60 kB │ gzip:  0.39 kB                                                                             6:13:13 AM
ℹ .nuxt/dist/client/_nuxt/error-500.3e5fbd94.js                  1.88 kB │ gzip:  0.99 kB                                                                             6:13:13 AM
ℹ .nuxt/dist/client/_nuxt/error-404.72b181b9.js                  6.53 kB │ gzip:  2.91 kB                                                                             6:13:13 AM
ℹ .nuxt/dist/client/_nuxt/entry.7dda54fb.js                    239.74 kB │ gzip: 71.31 kB                                                                             6:13:13 AM
ℹ ✓ built in 2.79s                                                                                                                                                    6:13:13 AM
✔ Client built in 2805ms                                                                                                                                              6:13:13 AM
ℹ Building server...                                                                                                                                                  6:13:13 AM
ℹ vite v4.3.9 building SSR bundle for production...                                                                                                                   6:13:13 AM
ℹ ✓ 78 modules transformed.                                                                                                                                           6:13:14 AM
ℹ .nuxt/dist/server/_nuxt/entry-styles.8755339a.mjs             0.08 kB                                                                                               6:13:14 AM
ℹ .nuxt/dist/server/_nuxt/error-500-styles.cf4b3e80.mjs         0.15 kB                                                                                               6:13:14 AM
ℹ .nuxt/dist/server/_nuxt/error-404-styles.1e0dde27.mjs         0.15 kB                                                                                               6:13:14 AM
ℹ .nuxt/dist/server/styles.mjs                                  0.46 kB                                                                                               6:13:14 AM
ℹ .nuxt/dist/server/_nuxt/auth-514615fe.js                      1.04 kB │ map:   0.87 kB                                                                              6:13:14 AM
ℹ .nuxt/dist/server/_nuxt/guest-only-bd112471.js                1.04 kB │ map:   0.87 kB                                                                              6:13:14 AM
ℹ .nuxt/dist/server/_nuxt/island-renderer-5a5e4c49.js           1.19 kB │ map:   1.40 kB                                                                              6:13:14 AM
ℹ .nuxt/dist/server/_nuxt/test-49970a4b.js                      1.49 kB │ map:   0.84 kB                                                                              6:13:14 AM
ℹ .nuxt/dist/server/_nuxt/error-500-styles-1.mjs-8fa81fcc.js    2.17 kB │ map:   0.12 kB                                                                              6:13:14 AM
ℹ .nuxt/dist/server/_nuxt/error-500-7e82afc2.js                 3.16 kB │ map:   3.78 kB                                                                              6:13:14 AM
ℹ .nuxt/dist/server/_nuxt/error-404-styles-1.mjs-e0837f4f.js    3.86 kB │ map:   0.12 kB                                                                              6:13:14 AM
ℹ .nuxt/dist/server/_nuxt/error-404-aded3150.js                 9.81 kB │ map:  18.91 kB                                                                              6:13:14 AM
ℹ .nuxt/dist/server/_nuxt/entry-styles-1.mjs-8b8449e7.js       12.38 kB │ map:   0.11 kB                                                                              6:13:14 AM
ℹ .nuxt/dist/server/server.mjs                                129.40 kB │ map: 186.85 kB                                                                              6:13:14 AM
ℹ ✓ built in 1.22s                                                                                                                                                    6:13:14 AM
✔ Server built in 1230ms                                                                                                                                              6:13:14 AM
✔ Generated public .output/public                                                                                                                               nitro 6:13:14 AM
ℹ Building Nitro Server (preset: cloudflare)                                                                                                                    nitro 6:13:14 AM
node_modules/@auth/core/lib/cookie.js (1:30) The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten
node_modules/@auth/core/lib/cookie.js (1:38) The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten
node_modules/@auth/core/lib/cookie.js (7:30) The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten
node_modules/@auth/core/lib/cookie.js (7:38) The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten
✔ Nitro server built                                                                                                                                            nitro 6:13:29 AM
  └─ .output/server/index.mjs (1.08 MB) (306 kB gzip)
Σ Total size: 1.08 MB (306 kB gzip)
✔ You can preview this build using npx wrangler dev .output/server/index.mjs --site .output/public --local                                                      nitro 6:13:29 AM
✔ You can deploy this build using npx wrangler publish                                                                                                          nitro 6:13:29 AM

Run Cloudflare pages locally:

$ npx wrangler dev .output/server/index.mjs --site .output/public --local

 ⛅️ wrangler 3.5.0
------------------
wrangler dev now uses local mode by default, powered by 🔥 Miniflare and 👷 workerd.
To run an edge preview session for your Worker, use wrangler dev --remote
▲ [WARNING] --local is no longer required and will be removed in a future version.

  `wrangler dev` now uses the local Cloudflare Workers runtime by default. 🎉

Your worker has access to the following bindings:
- KV Namespaces:
  - images_test: aaf56cc016f042ab91636d59b4875776
  - bisand_blog_test: 3e094d827b1f436d9cd16cdd3f860f24
⎔ Starting local server...
service core:user:bisand-blog-test: Uncaught TypeError: Cannot read properties of undefined (reading 'split')
  at core:user:bisand-blog-test:4513:38
  at core:user:bisand-blog-test:24327:4
  at core:user:bisand-blog-test:24328:3
✘ [ERROR] MiniflareCoreError [ERR_RUNTIME_FAILURE]: The Workers runtime failed to start. There is likely additional logging output above.

╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ [b] open a browser, [d] open Devtools, [l] turn off local mode, [c] clear console, [x] to exit                                                                                  │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

Describe the bug

This issue is related to Cloudflare pages using nuxt 3.6.5 and @hebilicious/authjs-nuxt 0.3.0-beta.2. The error occurs both on local (wrangler) and on Cloudflare.

Not sure if this is a bug in your component or any of the dependencues, but the error occurred after installing and running v0.3.0-beta.2

This code section seems to be the culprit. Withouth further investigation, it seems to be a part of an auto generated javascript by Wrangler, located in the /tmp/ folder. In this particular case: /tmp/tmp-3050-TxBjBovMveML/index.js

...
1320  |   const L = "undefined" != typeof globalThis ? globalThis : "undefined" != typeof self ? self : "undefined" != typeof global ? global : {};
1321  |   L.process = L.process || x;
1322  |   const R = L.process;
...
4576  |   const [Cs, Es] = R.versions.node.split(".").map((e2) => parseInt(e2, 10));
          //                            ^ undefined
...

I would be

Additional context

Link to generated javascript file: /tmp/tmp-3050-TxBjBovMveML/index.js

Logs

No response

Hebilicious commented 1 year ago

Could you try downgrading auth.js to 0.9.0 ?

bisand commented 1 year ago

I have done some testing. Only downgrading auth.js to 0.9.0 does not make any difference. However, when I downgrade @hebilicious/authjs-nuxt to 0.1.10 it seems to be working OK. Pages can be served via Wrangler without problems.

The issue seems to be introduced in 0.2.0-beta.0, because when I upgrade to this version and all the versions later, I encounter the same problem described in this issue.

Hope this can lead you in the right direction. I can do some more research later, but this is what I have come up with so far.

PS! Thanks for the great job of creating this plugin!

Hebilicious commented 1 year ago

@bisand Looks like the issue is that you're using npm overrides and it doesn't work like pnpm resolutions :/ If you delete your lockfile and use pnpm with

  "dependencies": {
    "@auth/core": "^0.10.0",
    "@hebilicious/authjs-nuxt": "^0.3.0-beta.2",
    "h3": "1.8.0-rc.3",
    "nitropack": "npm:nitropack-edge@latest"
  },
  "resolutions": {
    "h3": "1.8.0-rc.3",
    "nitropack": "npm:nitropack-edge@latest"
  }

it will work.

pnpm is bundled with node now, so you can do

RUN corepack enable && corepack prepare pnpm@latest --activate

in a dockerfile to use it.

bisand commented 1 year ago

That did the trick!

Thanks!