-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Description
Prerequisites
- I have searched the existing issues
- I understand that providing a SSCCE example is tremendously useful to the maintainers.
- I have read the documentation
- Ideally, I'm providing a sample JSFiddle, Codesandbox.io or preferably a shared playground link demonstrating the issue.
What theme are you using?
chakra-ui
Version
6.0.0-beta.x
Current Behavior
Dynamically importing from @rjsf/validator-ajv8/compileSchemaValidators
(or @rjsf/validator-ajv8/dist/compileSchemaValidators
or @rjsf/validator-ajv8/lib/compileSchemaValidators
) in a CommonJS setting yields an import error. Specifically, the following code:
const { compileSchemaValidatorsCode } = await import('@rjsf/validator-ajv8/dist/compileSchemaValidators');
Yields the following in Node 20:
➜ workspace git:(main) ✗ npm start
> [email protected] start
> ts-node index.ts
/project/workspace/node_modules/ts-node/dist/index.js:851
return old(m, filename);
^
Error [ERR_REQUIRE_ESM]: require() of ES Module /project/workspace/node_modules/@rjsf/validator-ajv8/dist/compileSchemaValidators.js from /project/workspace/index.ts not supported.
compileSchemaValidators.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead either rename compileSchemaValidators.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /project/workspace/node_modules/@rjsf/validator-ajv8/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).
at require.extensions.<computed> [as .js] (/project/workspace/node_modules/ts-node/dist/index.js:851:20)
at /project/workspace/index.ts:39:93
at async go (/project/workspace/index.ts:39:45) {
code: 'ERR_REQUIRE_ESM'
In Node 22 it's this error:
ReferenceError: module is not defined
Expected Behavior
The import should work using V6 beta, as it does in V5.
Steps To Reproduce
I created a CodeSandbox to demo the issue. The sandbox uses the same setup as my project with ts-node
and a tsconfig.json
- Clone https://codesandbox.io/p/devbox/4q6p9g
npm install
npm start
- Observe the error
- Install V5:
npm i --save @rjsf/[email protected] @rjsf/[email protected]
npm start
- Observe that the code works and there is no error
Environment
- OS: Windows
- Node: 20
- npm: 10
Anything else?
I noticed that the issue is resolved when removing "type": "module"
from the package.json
of @rjsf/[email protected]
and @rjsf/[email protected]
. This package.json attribute was added as part of V6 beta, but it seems to break CJS dynamic imports. As hinted at by the Node 20 error message, the attribute "declares all .js files in the package scope as ES modules" which doesn't appear to be an accurate declaration because there are .js CJS files exported by the package.