unjs / unenv

🕊️ Convert javaScript code to be runtime agnostic
MIT License
383 stars 20 forks source link

add polyfill for `node:events.once` #54

Closed birdlavv closed 1 year ago

birdlavv commented 1 year ago

Reproduction steps

  1. Visit https://stackblitz.com/edit/github-gvgyln-ersphr?file=server%2Fapi%2Ftest.ts,nuxt.config.ts
  2. Run npm run build

The reproduction was made using

  1. a fresh Nuxt 3 project
  2. using the jose package on a server-side api route
  3. using the vercel-edge nitro preset

causes the following error:

1: import * as http from 'http';
2: import * as https from 'https';
3: import { once } from 'events';
            ^
4: import { JOSEError, JWKSTimeout } from '../util/errors.js';
5: import { concat, decoder } from '../lib/buffer_utils.js';

 ERROR  'once' is not exported by node_modules/unenv/runtime/node/events/index.mjs, imported by node_modules/jose/dist/node/esm/runtime/fetch_jwks.js
Dreaming-Codes commented 1 year ago

Any workaround/news?

officialpiyush commented 1 year ago

currently https://github.com/vuejs/vuefire module needs the firebase-admin package to provide SSR features. firebase-admin module internally uses jose, due to which the builds fail at build time, it would be great to know any updates on this, or any possible workarounds.

The error is same as mentioned by the author, but here is the stack if anyone is intrested.

ERROR                                                                                                                                   nitro 12:23:11

1: import * as http from 'http';
2: import * as https from 'https';
3: import { once } from 'events';
          ^
4: import { JOSEError, JWKSTimeout } from '../util/errors.js';
5: import { concat, decoder } from '../lib/buffer_utils.js';

ERROR  'once' is not exported by node_modules/.pnpm/unenv@1.0.0/node_modules/unenv/runtime/node/events/index.mjs, imported by node_modules/.pnpm/jose@4.11.2/node_modules/jose/dist/node/esm/runtime/fetch_jwks.js

at error (node_modules/.pnpm/rollup@2.79.1/node_modules/rollup/dist/es/shared/rollup.js:1858:30)
at Module.error (node_modules/.pnpm/rollup@2.79.1/node_modules/rollup/dist/es/shared/rollup.js:12429:16)
at Module.traceVariable (node_modules/.pnpm/rollup@2.79.1/node_modules/rollup/dist/es/shared/rollup.js:12788:29)
at ModuleScope.findVariable (node_modules/.pnpm/rollup@2.79.1/node_modules/rollup/dist/es/shared/rollup.js:11440:39)
at ReturnValueScope.findVariable (node_modules/.pnpm/rollup@2.79.1/node_modules/rollup/dist/es/shared/rollup.js:6372:38)
at ChildScope.findVariable (node_modules/.pnpm/rollup@2.79.1/node_modules/rollup/dist/es/shared/rollup.js:6372:38)
at Identifier.bind (node_modules/.pnpm/rollup@2.79.1/node_modules/rollup/dist/es/shared/rollup.js:7439:40)
at CallExpression.bind (node_modules/.pnpm/rollup@2.79.1/node_modules/rollup/dist/es/shared/rollup.js:5269:23)
at CallExpression.bind (node_modules/.pnpm/rollup@2.79.1/node_modules/rollup/dist/es/shared/rollup.js:8935:15)
at ArrayExpression.bind (node_modules/.pnpm/rollup@2.79.1/node_modules/rollup/dist/es/shared/rollup.js:5265:73)
at CallExpression.bind (node_modules/.pnpm/rollup@2.79.1/node_modules/rollup/dist/es/shared/rollup.js:5265:73)
at CallExpression.bind (node_modules/.pnpm/rollup@2.79.1/node_modules/rollup/dist/es/shared/rollup.js:8935:15)
at AwaitExpression.bind (node_modules/.pnpm/rollup@2.79.1/node_modules/rollup/dist/es/shared/rollup.js:5269:23)
at VariableDeclarator.bind (node_modules/.pnpm/rollup@2.79.1/node_modules/rollup/dist/es/shared/rollup.js:5269:23)
at VariableDeclaration.bind (node_modules/.pnpm/rollup@2.79.1/node_modules/rollup/dist/es/shared/rollup.js:5265:73)
at BlockStatement.bind (node_modules/.pnpm/rollup@2.79.1/node_modules/rollup/dist/es/shared/rollup.js:5265:73)
at ArrowFunctionExpression.bind (node_modules/.pnpm/rollup@2.79.1/node_modules/rollup/dist/es/shared/rollup.js:5269:23)
at VariableDeclarator.bind (node_modules/.pnpm/rollup@2.79.1/node_modules/rollup/dist/es/shared/rollup.js:5269:23)
at VariableDeclaration.bind (node_modules/.pnpm/rollup@2.79.1/node_modules/rollup/dist/es/shared/rollup.js:5265:73)
at Program.bind (node_modules/.pnpm/rollup@2.79.1/node_modules/rollup/dist/es/shared/rollup.js:5265:73)
at Module.bindReferences (node_modules/.pnpm/rollup@2.79.1/node_modules/rollup/dist/es/shared/rollup.js:12425:18)
at Graph.sortModules (node_modules/.pnpm/rollup@2.79.1/node_modules/rollup/dist/es/shared/rollup.js:23062:20)
at Graph.build (node_modules/.pnpm/rollup@2.79.1/node_modules/rollup/dist/es/shared/rollup.js:22940:14)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async node_modules/.pnpm/rollup@2.79.1/node_modules/rollup/dist/es/shared/rollup.js:23730:13
at async catchUnfinishedHookActions (node_modules/.pnpm/rollup@2.79.1/node_modules/rollup/dist/es/shared/rollup.js:23126:20)
at async rollupInternal (node_modules/.pnpm/rollup@2.79.1/node_modules/rollup/dist/es/shared/rollup.js:23727:5)
at async _build (node_modules/.pnpm/nitropack@1.0.0/node_modules/nitropack/dist/shared/nitro.c8278d90.mjs:1404:18)
at async node_modules/.pnpm/nuxt@3.0.0_e3uo4sehh4zr4i6m57mkkxxv7y/node_modules/nuxt/dist/index.mjs:1677:9
at async build (node_modules/.pnpm/nuxt@3.0.0_e3uo4sehh4zr4i6m57mkkxxv7y/node_modules/nuxt/dist/index.mjs:2236:5)
at async Object.invoke (node_modules/.pnpm/nuxi@3.0.0/node_modules/nuxi/dist/chunks/build.mjs:56:5)
at async _main (node_modules/.pnpm/nuxi@3.0.0/node_modules/nuxi/dist/cli.mjs:50:20)
pi0 commented 1 year ago

PR more than welcome 🙏🏼

officialpiyush commented 1 year ago

sure, lemme try

DanSnow commented 1 year ago

Here is a workaround with Nuxt:

  1. install events
  2. add the following to your nuxt.config.ts
export default defineNuxtConfig({
  hooks: {
    'nitro:config': (nitroConfig) => {
      nitroConfig.unenv = {
        alias: {
          events: require.resolve('events/'),
        },
      }
    },
  },
})
officialpiyush commented 1 year ago

PR more than welcome 🙏🏼

@pi0 opened a PR with the fix #68