Require of ES Module is not supported #43434
Labels
Developer Experience
Ideas about improving block and theme developer experience
npm Packages
Related to npm packages
Description
When attempting to use some packages (e.g.
@wordpress/blocks
,@wordpress/data
,@wordpress/element
,@wordpress/redux-routine
) outside of a bundler like Webpack, it produces an error like this:Step-by-step reproduction instructions
npm add @wordpress/blocks
node -e 'require( "@wordpress/blocks" );'
ornode --input-type=module -e 'import blocks from "@wordpress/blocks";'
Screenshots, screen recording, code snippet
No response
Environment info
Please confirm that you have searched existing issues in the repo.
Yes
Please confirm that you have tested with all plugins deactivated except Gutenberg.
Yes
Analysis and workarounds
The main problem is that the CommonJS build tries to
require
the ESM-only moduleis-plain-obj
. This is never going to work.A secondary problem is that the ESM builds of the Gutenberg packages are not declared via
exports
in package.json, and some environments (like node and jest) do not understand themodules
field. So even in ESM environments, the CommonJS build is used.Mitigating factors here are
modules
field and even if using the CommonJS build will transpileis-plain-obj
back to CommonJS instead of raising an error.is-plain-obj
back to CommonJS instead of raising an error iftransformIgnorePatterns
is set properly in the Jest config (as I see you've done in your@wordpress/jest-preset-default
package).The main problem I'm running into here is with the use of Jest in ESM mode. In that case babel-jest will not transpile
is-plain-obj
, even whentransformIgnorePatterns
is set properly, leading to the error as described here. It might help to configure Jest's resolver to understand themodules
field usingpackageFilter
and so use the ESM build of the Gutenberg packages, but when I tried that in our repo I ran into a Jest bug.The text was updated successfully, but these errors were encountered: