nuxt: cannot `require()` node-fetch in built project

Environment

  • Operating System: Windows_NT
  • Node Version: v16.14.0
  • Nuxt Version: 3.0.0-rc.0-27507653.98586bc
  • Package Manager: yarn@1.22.18
  • Builder: vite
  • User Config: typescript, build, app
  • Runtime Modules: -
  • Build Modules: -

Reproduction

https://github.com/JulienVanelian/nuxt3-reproduction-node-fetch

You need to setup firebase hosting after cloning the project

NITRO_PRESET=firebase yarn build
firebase deploy

Describe the bug

The build succeeds but the deploy on firebase fails. This seems to be related to node-fetch v3 being an ESM-only module. node-fetch is required by ohmyfetch, see log below. This happened right after updating to nuxt 3 rc 1, right from 3.0.0-27500344.7fb29b3. It was working before.

Additional context

No response

Logs

PS C:\Users\julien\workspace\project> yarn deploy 
yarn run v1.22.18
$ set NITRO_PRESET=firebase&& nuxt build && firebase deploy
Nuxt CLI v3.0.0-rc.0-27507653.98586bc                                                                                                                                                                                                                                 16:51:30
i Vite client warmed up in 4096ms                                                                                                                                                                                                                                     16:51:37
i Client built in 5200ms                                                                                                                                                                                                                                              16:51:39
i Building server...                                                                                                                                                                                                                                                  16:51:39
√ Server built in 1597ms                                                                                                                                                                                                                                              16:51:40
√ Generated public .output/public                                                                                                                                                                                                                               nitro 16:51:40
start Building server...                                                                                                                                                                                                                                        nitro 16:51:40

 WARN  Multiple major versions of package node-fetch are being externalized. Picking latest version.                                                                                                                                                                  16:51:45  
  - /Users/julien/workspace/project/node_modules/node-fetch@2.6.7
  - /Users/julien/workspace/project/node_modules/ohmyfetch/node_modules/node-fetch@3.2.3

 WARN  Multiple major versions of package jws are being externalized. Picking latest version.                                                                                                                                                                         16:51:45  
  - /Users/julien/workspace/project/node_modules/jsonwebtoken/node_modules/jws@3.2.2
  - /Users/julien/workspace/project/node_modules/jws@4.0.0

 WARN  Multiple major versions of package semver are being externalized. Picking latest version.                                                                                                                                                                      16:51:45  
  - /Users/julien/workspace/project/node_modules/jsonwebtoken/node_modules/semver@5.7.1
  - /Users/julien/workspace/project/node_modules/make-dir/node_modules/semver@6.3.0

 WARN  Multiple major versions of package jwa are being externalized. Picking latest version.                                                                                                                                                                         16:51:45  
  - /Users/julien/workspace/project/node_modules/jsonwebtoken/node_modules/jwa@1.4.1
  - /Users/julien/workspace/project/node_modules/jwa@2.0.0

start Writing server bundle...                                                                                                                                                                                                                                  nitro 16:51:46
√ Server built                                                                                                                                                                                                                                                  nitro 16:51:46
  ├─ .output/server/package.json (3.08 kB) (1.1 kB gzip)
  ├─ .output/server/index.mjs (328 B) (178 B gzip)
  ├─ .output/server/chunks/vue3.mjs (283 B) (197 B gzip)
  ├─ .output/server/chunks/server.mjs (493 kB) (79.4 kB gzip)
  ├─ .output/server/chunks/renderer.mjs (23.4 kB) (6.47 kB gzip)
  ├─ .output/server/chunks/index.mjs (397 kB) (91.6 kB gzip)
  ├─ .output/server/chunks/formReservation.mjs (5.97 kB) (2.19 kB gzip)
  ├─ .output/server/chunks/firebase.mjs (11.8 kB) (3.84 kB gzip)
  ├─ .output/server/chunks/firebase-admin.mjs (478 B) (305 B gzip)
  ├─ .output/server/chunks/client.manifest.mjs (5.4 kB) (839 B gzip)
  └─ .output/server/chunks/calendarReservedDays.mjs (846 B) (411 B gzip)
Σ Total size: 18.1 MB (3.09 MB gzip)
i You can deploy this build using npx firebase deploy                                                                                                                                                                                                           nitro 16:51:47

=== Deploying to 'project'...

i  deploying functions, hosting
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
+  functions: required API cloudfunctions.googleapis.com is enabled
+  functions: required API cloudbuild.googleapis.com is enabled

Error: Error occurred while parsing your function triggers.

Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\julien\workspace\project\.output\server\node_modules\node-fetch\src\index.js from C:\Users\julien\workspace\project\.output\server\node_modules\firebase-functions\lib\common\providers\identity.js not supported.
Instead change the require of index.js in C:\Users\julien\workspace\project\.output\server\node_modules\firebase-functions\lib\common\providers\identity.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (C:\Users\julien\workspace\project\.output\server\node_modules\firebase-functions\lib\common\providers\identity.js:26:22)
    at Object.<anonymous> (C:\Users\julien\workspace\project\.output\server\node_modules\firebase-functions\lib\providers\auth.js:25:20)
    at Object.<anonymous> (C:\Users\julien\workspace\project\.output\server\node_modules\firebase-functions\lib\index.js:38:14)
    at async Promise.all (index 0)
    at async loadModule (C:\Users\julien\AppData\Local\Yarn\Data\global\node_modules\firebase-tools\lib\deploy\functions\runtimes\node\triggerParser.js:16:20)
    at async C:\Users\julien\AppData\Local\Yarn\Data\global\node_modules\firebase-tools\lib\deploy\functions\runtimes\node\triggerParser.js:34:15
error Command failed with exit code 1.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (5 by maintainers)

Most upvoted comments

Starting with nitropack@0.4.4 and unenv@0.5.2, we are using a cross-version compatible varaint of node-fetch (unjs/node-fetch-native) that should resolve the issue when multiple versions of node-fetch exist in a project.

Please use npx nuxi upgrade --force to receive this update.

You could be solved by installing node-fetch in your project.

yarn add node-fetch

After that, build and deploy.

NITRO_PRESET=firebase yarn build
firebase deploy

firebase-functions module using older node-fetch, so I guess it does not support ESM.

Thank you! This solved my problem! The deploy works perfectly.

@danielroe Should I close this issue or not?

Thank you. That suggests the issue isn’t related to firebase, but rather the externals within your project (likely a CJS dependency or a dependency that needs to be transpiled).