require.context is not a function is ESM (type: module)
See original GitHub issueWhat is the current behavior?
If we have ES module (example):
const context = require.context(
"./icons",
true,
/^[.\\/a-zA-Z\d-]+\.svg$/,
"sync"
)
export const icons = context
.keys()
.map(context)
.map(m => m.default)
and the nearest package.json contains {"type": "module"} then while a build we get the error:
require.context is not a function
If to remove {"type": "module"} in package.json there will be no error, build is ok.
If the current behavior is a bug, please provide the steps to reproduce.
webpack version: 5.72.0 Node.js version: 16 Operating System: Windows 10
Issue Analytics
- State:
- Created a year ago
- Comments:20 (12 by maintainers)
Top Results From Across the Web
require.context() alternative when using ES modules
I have a project where I want to use ES modules and use import instead of require . So I have added in...
Read more >How to resolve “TypeError: require.context is not a function” in ...
While we were writing Jest tests, we ran into an the error “TypeError: require.context is not a function”. This is because of the...
Read more >ECMAScript Modules - webpack
ECMAScript Modules (ESM) is a specification for using Modules in the Web. It's supported by all modern browsers and the recommended way of...
Read more >CommonJS modules | Node.js v19.3.0 Documentation
When the entry point is not a CommonJS module, require.main is undefined , and the ... The semantics of the Node.js require() function...
Read more >rollup.js
Importing CommonJS · Publishing ES Modules ... This is the only hook that does not have access to most plugin context utility functions...
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 Free
Top 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

require.contextdoesn’t exit in ESM, please useimport(...)