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.

Unable to import ESM-only modules in a test

See original GitHub issue

What is your Scenario?

Importing ESM-only modules in a test throws ERR_REQUIRE_ESM error.

What is the Current behavior?

An error is thrown:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: node_modules\node-fetch\src\index.js
require() of ES modules is not supported.
require() of node_modules\node-fetch\src\index.js from test.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 node_modules\node-fetch\package.json.

    at Object.<anonymous> (test.js:3:1)

What is the Expected behavior?

Test executes correctly.

What is your public website URL? (or attach your complete example)

Please see the isolated test code below.

What is your TestCafe test code?

// Make sure node-fetch 3.2.0 is installed.
// It is an ESM-only module.
import fetch from 'node-fetch' // The test will fail at this line.

fixture`ESM-only module import error`

test('Will not run', () => {
  console.log('This code will not run')
})

Your complete configuration file

No specific configuration file.

Your complete test report

Please refer to the error message above.

Screenshots

Not applicable.

Steps to Reproduce

Please install node-fetch 3.2.0 and run the attached test code.

TestCafe version

1.18.3

Node.js version

14.17.1

Command-line arguments

testcafe chrome test.js

Browser name(s) and version(s)

chrome

Platform(s) and version(s)

Windows 11

Other

The test code runs with node-fetch 2.6.7, which supports require(). It seems that import statements are transpiled to require calls, which fails for all ESM-only modules.

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
puneetpunjcommented, Apr 6, 2022

Any update on this issue? this is happening with @percy/testcafe package as well.

1reaction
AlexKamaevcommented, Jun 27, 2022

@bvklingeren, You are right, a fix for this issue might take significant time. We did not look into it yet. We have plans to research it, but I cannot give time estimates right now, unfortunately.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to import ESM .ts module in node - Stack Overflow
It seems you are looking to use ESM with Node and TS. tsconfig.json (tested with TypeScript 4.7 and 4.8). On your tsconfig.json file...
Read more >
ECMAScript Modules - Jest
Since ESM evaluates static import statements before looking at the code, the hoisting of jest.mock calls that happens in CJS won't work for...
Read more >
Documentation - ECMAScript Modules in Node.js - TypeScript
This code works in CommonJS modules, but will fail in ES modules because relative import paths need to use extensions. As a result,...
Read more >
import - JavaScript - MDN Web Docs - Mozilla
import declarations can only be present in modules, and only at the ... The importing module can only read the value but can't...
Read more >
How we employed the new ECMAScript Module Support in ...
exports . But that is not all… In regards to performance, with ESM import you can selectively load only the pieces you need....
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