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.

It just doesn't work

See original GitHub issue

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.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
LinusUcommented, Nov 4, 2021

@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.

1reaction
jimmywartingcommented, Nov 3, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

it just doesn't work | English examples in context - Ludwig
High quality example sentences with “it just doesn't work” in context from reliable sources - Ludwig is the linguistic search engine that helps...
Read more >
Is there any difference between "it's not working" and "it doesn ...
"It doesn't work" means that something is permanently broken, or is never effective. Here are some example situations:
Read more >
IT just doesn't work vs. IT doesn't just work – Jonathan Wade
IT just doesn't work vs. IT doesn't just work. You may be noticing that I'm fully exploring the double meanings that come about...
Read more >
Which is correct, 'it doesn't work' or 'It doesn't works'? - Quora
it doesn't work” is correct. As you know, English uses the helping verb do for negative statements and for questions. The only thing...
Read more >
Trickle-Down Economics: Four Reasons Why It Just Doesn't ...
Supposedly, top-bracket tax breaks will result in more jobs being created, higher wages for the average worker, and an overall upturn in our...
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