Skip to content

Import error when using compileSchemaValidatorsCode CJS build in V6 Beta release #4754

@thesnups

Description

@thesnups

Prerequisites

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

  1. Clone https://codesandbox.io/p/devbox/4q6p9g
  2. npm install
  3. npm start
  4. Observe the error
  5. Install V5: npm i --save @rjsf/[email protected] @rjsf/[email protected]
  6. npm start
  7. 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions