node-fetch: It just doesn't work

Trying to make your lib work, but have a couple of problems Problem one: internal/modules/cjs/loader.js:1102 throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath); ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: D:\CODE\node_modules\node-fetch\src\index.js require() of ES modules is not supported. require() of D:\CODE\node_modules\node-fetch\src\index.js from D:\CODE\dist\index.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 D:\CODE\node_modules\node-fetch\src\index.js to end in .cjs, change the requiring code to use import(), or remove “type”: “module” from D:\CODE\node_modules\node-fetch\package.json.

At the end of the log it has some instructions, I followed it but it has another error:

TypeError: (0 , node_fetch_1.default) is not a function
    at D:\CODE\dist\index.js:30:34
    at Layer.handle [as handle_request] (D:\CODE\node_modules\express\lib\router\layer.js:95:5)
    at next (D:\CODE\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (D:\CODE\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (D:\CODE\node_modules\express\lib\router\layer.js:95:5)
    at D:\CODE\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (D:\CODE\node_modules\express\lib\router\index.js:335:12)
    at next (D:\CODE\node_modules\express\lib\router\index.js:275:10)
    at D:\CODE\dist\index.js:27:9
    at Layer.handle [as handle_request] (D:\CODE\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (D:\CODE\node_modules\express\lib\router\index.js:317:13)
    at D:\CODE\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (D:\CODE\node_modules\express\lib\router\index.js:335:12)
    at next (D:\CODE\node_modules\express\lib\router\index.js:275:10)
    at D:\CODE\node_modules\express\lib\router\index.js:635:15
    at next (D:\CODE\node_modules\express\lib\router\index.js:260:14)

Reproduction Just trying to fetch with your lib, doesn’t work. You might be interested to hear that I use TS, and I have something called tsc - watch that converts TS code to JS, this might have something to do with the problems above.

Expected behavior It should work.

Hope you can fix this soon.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 20 (3 by maintainers)

Most upvoted comments

@ImperatorOfIntellectualism here is a diff that makes it work for me:

diff --git a/KEKW/package.json b/KEKW/package.json
index 7ed9245..e787c74 100644
--- a/KEKW/package.json
+++ b/KEKW/package.json
@@ -3,10 +3,11 @@
   "version": "1.0.0",
   "description": "",
   "main": "index.js",
+  "type": "module",
   "scripts": {
     "watch": "tsc -w",
-    "test": "nodemon dist/index.js",
-    "start": "ts-node src/index.ts",
+    "test": "NODE_OPTIONS=--experimental-specifier-resolution=node nodemon dist/index.js",
+    "start": "node --loader ts-node/esm --experimental-specifier-resolution=node src/index.ts",
     "migration": "mikro-orm migration:create"
   },
   "author": "",
diff --git a/KEKW/src/index.ts b/KEKW/src/index.ts
index f0ae144..19854f4 100644
--- a/KEKW/src/index.ts
+++ b/KEKW/src/index.ts
@@ -9,7 +9,7 @@ import { PostResolver } from './resolvers/post';
 import { UserResolver } from './resolvers/user';
 import { ThreadResolver } from './resolvers/thread';
 import { BoardResolver } from './resolvers/board';
-import fetch from '../node_modules/node-fetch';
+import fetch from 'node-fetch';
 
 //Persist And Flush - adds model to the table
 
@@ -18,7 +18,7 @@ const main = async ( ) => {
     const apolloServer = new ApolloServer({schema: await buildSchema({resolvers: [HelloResolver, PostResolver, UserResolver, ThreadResolver, BoardResolver], validate: false}), context: () => ({em: orm.em})})
     await apolloServer.start();
     apolloServer.applyMiddleware({app})
-    const orm = await MikroORM.init(config) 
+    const orm = await MikroORM.init(config)
     await orm.getMigrator().up()
 
     app.use(function(_, res, next) {
diff --git a/KEKW/src/mikro-orm.config.ts b/KEKW/src/mikro-orm.config.ts
index 32c7316..28a0ee7 100644
--- a/KEKW/src/mikro-orm.config.ts
+++ b/KEKW/src/mikro-orm.config.ts
@@ -1,14 +1,14 @@
 import { __prod__ } from "./constants";
 import { Post } from "./entities/Post";
 import { MikroORM } from "@mikro-orm/core";
-import path from 'path'
 import { User } from "./entities/User";
 import { Thread } from "./entities/Thread";
 import { Board } from "./entities/Board";
+import { URL, fileURLToPath } from "url";
 
 export default {
     migrations: {
-      path: path.join(__dirname + '/migrations'), // path to the folder with migrations
+      path: fileURLToPath(new URL('migrations', import.meta.url)), // path to the folder with migrations
       pattern: /^[\w-]+\d+\.[tj]s$/, // regex pattern for the migration files
     },
     entities: [Post, User, Thread, Board],
diff --git a/KEKW/tsconfig.json b/KEKW/tsconfig.json
index 3b3e51b..2e4b5f1 100644
--- a/KEKW/tsconfig.json
+++ b/KEKW/tsconfig.json
@@ -1,7 +1,7 @@
 {
   "compilerOptions": {
     "target": "es2017",
-    "module": "commonjs",
+    "module": "ES2020",
     "lib": ["dom", "es6", "es2017", "esnext.asynciterable"],
     "skipLibCheck": true,
     "sourceMap": true,

With this both ts-node for running the TS files directly and nodemon on the built files works.


--experimental-specifier-resolution=node is needed because you aren’t specifying a file extension when importing files. That is import config from './mikro-orm.config' instead of import config from './mikro-orm.config.js'. I would recommend adding the file extension to the import, since that seems to be what Node.js is recommending going forward.

The reason why I’m using '../node_modules/node-fetch' is because it can’t find it otherwise.

This is the problem… you shouldn’t have to do '../node_modules/ then you are probably standing in the wrong working directory. Or you haven’t installed the dependency if anything then it should be ../node_modules/node-fetch/src/index.js but you shouldn’t have to do this…

i just tried my above suggestion to make sure that it did work, and it did…

npm init -y
npm i node-fetch
node -e "new Function('modulePath', 'return import(modulePath)')('node-fetch').then(console.log)"

also tried sticking the code in a js file and executing node ./index.js

node-fetch@3 is esm-only you can not use require to import it.

https://github.com/node-fetch/node-fetch/issues/1279