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.

require.context is not a function is ESM (type: module)

See original GitHub issue

What 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:open
  • Created a year ago
  • Comments:20 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
alexander-akaitcommented, Jun 7, 2022

require.context doesn’t exit in ESM, please use import(...)

1reaction
vankopcommented, Sep 10, 2022

I think we should throw an error on empty import.meta.webpackContext()

Read more comments on GitHub >

github_iconTop 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 >

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