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.

Error using $fetch.create using CJS modules.

See original GitHub issue

I was working in a Nuxt2 app using ohmyfetch in the background with a home-made module, but the Nuxt2 app displays me that the$fetch export has not the method create, so I investigate and I found that the CJS module (what it was imported by Webpack after the bundle process) it not exports the method.

Steps to reproduce the error

  1. Create a new JS proyect with the ohmyfetch dependency.
  2. Create a index.js file with the following content.
const { $fetch } = require('ohmyfetch')

const scopedFetch = $fetch.create({});
  1. Try to run the file.

What I was expected

It should not thrown an error.

Error description

const scopedFetch = $fetch.create({});
                           ^

TypeError: $fetch.create is not a function
    at Object.<anonymous> (/home/bantonio/PhpstormProjects/test-ohmyfetch/index.js:3:28)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:7
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
m0dch3ncommented, Jun 26, 2022

@pi0

is there a reason, why the cjs/* import the *,mjs files ?

Would it not be easier, to simply build a cjs version with the following build.config.ts ?

import { defineBuildConfig } from 'unbuild'

export default defineBuildConfig({
  declaration: true,
  rollup: {
    emitCJS: true,
  },
  entries: [
    'src/index',
    'src/node',
    'src/undici'
  ]
})

I believe building it in CJS is the only possible solution afaik, because we cannot load mjs, without being async…

0reactions
vaaskicommented, Dec 4, 2022

I’m still getting this in 1.0.0 and I’m not sure what the issue is

Read more comments on GitHub >

github_iconTop Results From Across the Web

Loading node-fetch@3 in CJS and ESM · Issue #1279 - GitHub
I'm having problems with ESM and TypeScript​​ If you have decided to make your project ESM ( "type": "module" in your package. json),...
Read more >
require() of ES modules is not supported when importing node ...
js file has nothing but the import of node-fetch and it gives me an error message. What am I doing wrong? Running Ubuntu...
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 >
node-fetch Error [ERR_REQUIRE_ESM]: require() of ES ...
To solve the node-fetch error "[ERR_REQUIRE_ESM]: require() of ES Module not supported", use a dynamic import to import the node-fetch package or downgrade ......
Read more >
Documentation - ECMAScript Modules in Node.js - TypeScript
This has been a very difficult feature to support, since the foundation of the Node.js ecosystem is built on a different module system...
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