Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: require() of ES modules is not supported when importing node-fetch

I'm creating a program to analyze security camera streams and got stuck on the very first line. At the moment my .js file has nothing but the import of node-fetch and it gives me an error message. What am I doing wrong?

Running Ubuntu 20.04.2 LTS in Windows Subsystem for Linux.

Node version:

user@MYLLYTIN:~/CAMSERVER$ node -v
v14.17.6

node-fetch package version:

user@MYLLYTIN:~/CAMSERVER$ npm v node-fetch

[email protected] | MIT | deps: 2 | versions: 63
A light-weight module that brings Fetch API to node.js
https://github.com/node-fetch/node-fetch

keywords: fetch, http, promise, request, curl, wget, xhr, whatwg

dist
.tarball: https://registry.npmjs.org/node-fetch/-/node-fetch-3.0.0.tgz
.shasum: 79da7146a520036f2c5f644e4a26095f17e411ea
.integrity: sha512-bKMI+C7/T/SPU1lKnbQbwxptpCrG9ashG+VkytmXCPZyuM9jB6VU+hY0oi4lC8LxTtAeWdckNCTa3nrGsAdA3Q==
.unpackedSize: 75.9 kB

dependencies:
data-uri-to-buffer: ^3.0.1 fetch-blob: ^3.1.2         

maintainers:
- endless <[email protected]>
- bitinn <[email protected]>
- timothygu <[email protected]>
- akepinski <[email protected]>

dist-tags:
latest: 3.0.0        next: 3.0.0-beta.10  

published 3 days ago by endless <[email protected]>

esm package version:

user@MYLLYTIN:~/CAMSERVER$ npm v esm

[email protected] | MIT | deps: none | versions: 140
Tomorrow's ECMAScript modules today!
https://github.com/standard-things/esm#readme

keywords: commonjs, ecmascript, export, import, modules, node, require

dist
.tarball: https://registry.npmjs.org/esm/-/esm-3.2.25.tgz
.shasum: 342c18c29d56157688ba5ce31f8431fbb795cc10
.integrity: sha512-U1suiZ2oDVWv4zPO56S0NcR5QriEahGtdN2OR6FiOG4WJvcjBVFB0qI4+eKoWFH483PKGuLuu6V8Z4T5g63UVA==
.unpackedSize: 308.6 kB

maintainers:
- jdalton <[email protected]>

dist-tags:
latest: 3.2.25  

published over a year ago by jdalton <[email protected]>

Contents of the .js file (literally nothing but the import):

user@MYLLYTIN:~/CAMSERVER$ cat server.js 
import fetch from "node-fetch";

Result:

user@MYLLYTIN:~/CAMSERVER$ node -r esm server.js 
/home/user/CAMSERVER/node_modules/node-fetch/src/index.js:1
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/user/CAMSERVER/node_modules/node-fetch/src/index.js
require() of ES modules is not supported.
require() of /home/user/CAMSERVER/node_modules/node-fetch/src/index.js from /home/user/CAMSERVER/server.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/user/CAMSERVER/node_modules/node-fetch/package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1089:13) {
  code: 'ERR_REQUIRE_ESM'
}
user@MYLLYTIN:~/CAMSERVER$ 
like image 567
Roope Kuisma Avatar asked Sep 03 '21 08:09

Roope Kuisma


People also ask

How do you solve require () of ES modules is not supported?

You can solve the "[ERR_REQUIRE_ESM]: require() not supported" by doing one of two things: Use ESM - use import foo from 'foo' , instead of const foo = require('foo') and add the following line to your package. json file: "type": "module" . Downgrade to the last version of the package that is built with CommonJS .

How do I import node-fetch with require?

Now you can use the require() syntax to import the node-fetch package. Copied! const fetch = require('node-fetch'); console.

How do I enable ES modules in node JS?

To be able to load an ES module, we need to set “type”: “module” in this file or, as an alternative, we can use the . mjs file extension as against the usual . js file extension. Also, from Node version 12.7.

Can I require node-fetch?

As of version 3.0, node-fetch is an ESM-only module - you are not able to import it with require() .


2 Answers

From the Upgrade Guide

node-fetch was converted to be a ESM only package in version 3.0.0-beta.10. node-fetch is an ESM-only module - you are not able to import it with require.

Alternatively, you can use the async import() function from CommonJS to load node-fetch asynchronously:

// mod.cjs
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));

or you stay on v2 as they say in the README

node-fetch is an ESM-only module - you are not able to import it with require. We recommend you stay on v2 which is built with CommonJS unless you use ESM yourself. We will continue to publish critical bug fixes for it.

EDIT

I saw that in the doc, used it, and the script crashes with :

error TS2556: A spread argument must either have a tuple type or be passed to a rest parameter.

Since you are using typescript you should do something like this

import { RequestInfo, RequestInit } from "node-fetch";

const fetch = (url: RequestInfo, init?: RequestInit) =>  import("node-fetch").then(({ default: fetch }) => fetch(url, init));
like image 61
René Link Avatar answered Oct 19 '22 08:10

René Link


To fix this, I downgraded node-fetch to the latest version 2, which right now is 2.6.6. This is the script I ran:

yarn add node-fetch@^2.6.6

or

npm install node-fetch@^2.6.6

I also added these compiler options:

{
  "compilerOptions": { "allowJs": true, "outDir": "./dist" },
}
like image 15
Andrew Horn Avatar answered Oct 19 '22 08:10

Andrew Horn