-
Notifications
You must be signed in to change notification settings - Fork 24
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
Conversation
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
navnita123
commented
Aug 24, 2025
Please release this, we need modern esm support
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.
HIRANO-Satoshi
commented
Oct 17, 2025
@nrcrast, ping. Could you try 'yarn test'?
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
src/index.js
Using plain ole npm with node v22, running
npm i && npm run startyou'll get something like this:Replacing the version with my branch: :
You'll get the expected output where it dumps the whole compiled schema: