question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:16 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
pi0commented, May 11, 2022

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.

1reaction
JulienVaneliancommented, Apr 21, 2022

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

require() of ES modules is not supported when importing node ...
node-fetch is an ESM-only module - you are not able to import it with require. We recommend you stay on v2 which is...
Read more >
node-fetch Error [ERR_REQUIRE_ESM]: require() of ES ...
The node-fetch error "[ERR_REQUIRE_ESM]: require() of ES Module not supported" occurs because the node-fetch package has been converted to be an ESM-only ...
Read more >
node-fetch - npm
Start using node-fetch in your project by running `npm i node-fetch`. There are 28907 other projects in the npm registry using node-fetch.
Read more >
Unable to import `node-fetch` in service
I've got code like the following in one of my api service files… import { Connection } from 'jsforce' import fetch from 'node-fetch'...
Read more >
Fetch API in Node.js - Atatus
The node-fetch module was created specifically to backfill this feature in Node.js, which is why it exists. The good news is that for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found