Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add modern ESM Support #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nrcrast wants to merge 5 commits into sagold:main from nrcrast:esm-support
Closed

Add modern ESM Support #74

nrcrast wants to merge 5 commits into sagold:main from nrcrast:esm-support

Conversation

@nrcrast
Copy link

@nrcrast nrcrast commented Jun 17, 2025

Fix #59

This updates the tsconfig and package.json (and all the other consequences of that) to export proper ESM modules.

You can test this pretty easily with a project like the following:

package.json

{
 "name": "json-validator-test",
 "version": "1.0.0",
 "type": "module",
 "scripts": {
 "start": "node src/index.js",
 "test": "echo \"Error: no test specified\" && exit 1"
 },
 "author": "",
 "license": "ISC",
 "description": "",
 "dependencies": {
 "json-schema-library": "10.5.2",
 "tsx": "^4.19.3"
 },
 "devDependencies": {
 "@types/node": "^24.0.3",
 "typescript": "^5.8.3"
 }
}

src/index.js

import {compileSchema} from "json-schema-library";
const schema = compileSchema({
 "$id": "https://example.com/person.schema.json",
 "$schema": "https://json-schema.org/draft/2020-12/schema",
 "title": "Person",
 "type": "object",
 "properties": {
 "firstName": {
 "type": "string",
 "description": "The person's first name."
 }
 }
});
console.log('Hello world', schema);

Using plain ole npm with node v22, running npm i && npm run start you'll get something like this:

file:///Users/206674672/code/json-validator-test/src/index.js:1
import {compileSchema} from "json-schema-library";
 ^^^^^^^^^^^^^
SyntaxError: Named export 'compileSchema' not found. The requested module 'json-schema-library' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from 'json-schema-library';
const {compileSchema} = pkg;
 at ModuleJob._instantiate (node:internal/modules/esm/module_job:177:21)
 at async ModuleJob.run (node:internal/modules/esm/module_job:260:5)
 at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:543:26)
 at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:116:5)

Replacing the version with my branch: :

 "json-schema-library": "git@github.com:nrcrast/json-schema-library.git#66bb39f1b68d518ee67707d88ea0598dec7e8a51",

You'll get the expected output where it dumps the whole compiled schema:

> json-validator-test@1.0.0 start
> node src/index.js
Hello world <ref *1> {
 evaluationPath: '#',
 lastIdPointer: '#',
 schemaLocation: '#',
 dynamicId: '',
....etc

timgthomas reacted with thumbs up emoji
Copy link
Owner

sagold commented Jul 2, 2025

Hi Nick,

thank you for your PR!! I will need some time to verify your changes and test other types of exports. I have a setup
prepared to build an esm module without those changes, but your PR might be a better way forward.

Cheers,
Sascha

Copy link

Please release this, we need modern esm support

Copy link
Owner

sagold commented Aug 27, 2025

Did you run yarn test? This is a prerequisite for me to further look into this PR. Currently the test run fails:

➜ json-schema-library git:(esm-support) yarn test
yarn run v1.22.19
$ mocha 'src/**/*.{test,spec}.ts'
XXX/json-schema-library/node_modules/mocha/lib/cli/config.js:80
 throw createUnparsableFileError(
 ^
Error: Unable to read/parse XXX/json-schema-library/.mocharc.js: Error [ERR_REQUIRE_ESM]: require() of ES Module XXX/json-schema-library/.mocharc.js from XXX/json-schema-library/node_modules/mocha/lib/cli/config.js not supported.
.mocharc.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 .mocharc.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 XXX/json-schema-library/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).
 at createUnparsableFileError (XXX/json-schema-library/node_modules/mocha/lib/errors.js:514:13)
 at exports.loadConfig (XXX/json-schema-library/node_modules/mocha/lib/cli/config.js:80:11)
 at loadRc (XXX/json-schema-library/node_modules/mocha/lib/cli/options.js:162:21)
 at loadOptions (XXX/json-schema-library/node_modules/mocha/lib/cli/options.js:250:20)
 at Object.<anonymous> (XXX/json-schema-library/node_modules/mocha/bin/mocha.js:27:14)
 at Module._compile (node:internal/modules/cjs/loader:1546:14)
 at Object..js (node:internal/modules/cjs/loader:1689:10)
 at Module.load (node:internal/modules/cjs/loader:1318:32)
 at Function._load (node:internal/modules/cjs/loader:1128:12)
 at TracingChannel.traceSync (node:diagnostics_channel:315:14) {
 code: 'ERR_MOCHA_UNPARSABLE_FILE'
}
Node.js v22.11.0

Be aware that I see the need of this feature (and I want this to be the next feature published), I am unhappy about the type of implementation. I would rather not change all source files, but build a valid esm package instead. Happy to discuss.

Copy link

@nrcrast, ping. Could you try 'yarn test'?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Doesn't work with native ESM modules

AltStyle によって変換されたページ (->オリジナル) /