Unable to import ESM-only modules in a test
See original GitHub issueWhat 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:
- Created 2 years ago
- Reactions:7
- Comments:10 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Any update on this issue? this is happening with @percy/testcafe package as well.
@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.