Skip to content

Doesn't work with native ESM modules #59

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

Open
tmcw opened this issue Jun 13, 2024 · 4 comments
Open

Doesn't work with native ESM modules #59

tmcw opened this issue Jun 13, 2024 · 4 comments

Comments

@tmcw
Copy link

tmcw commented Jun 13, 2024

If I create a module:

package.json:

{
  "type": "module",
  "name": "20240613114707-1603",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "json-schema-library": "^9.3.5"
  }
}

index.mjs:

import { Draft2019 } from "json-schema-library";

console.log(Draft2019);

And run it, then this module doesn't provide the expected exports:

➜ node index.mjs
file:///Users/tmcw/tmp/20240613114707-1603/index.mjs:1
import { Draft2019 } from "json-schema-library";
         ^^^^^^^^^
SyntaxError: Named export 'Draft2019' 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 { Draft2019 } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:132:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:214:5)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at async loadESM (node:internal/process/esm_loader:28:7)
    at async handleMainPromise (node:internal/modules/run_main:113:12)

Node.js v20.11.0

It looks like the ESM mode of using this module doesn't work - there isn't a type: module, or an exports: property in package.json.

It also looks like the modules that are getting generated by the tsconfig use imports that lack .js extensions, so the source won't be compatible with native ESM either.

@sagold
Copy link
Owner

sagold commented Jun 13, 2024

Hi Tom,

  • the readme refers to version 10, which is currently released as a pre-release version [email protected]
  • installing the current latest version you have to reference the readme for version 9

In your case, either install [email protected]. Do this, if you need Draft-2019 support. Here you would be an early adapter, but overall this is the most compliant release yet.

Or start using Draft-07 with import { Draft7 } from "json-schema-library" as is documented here https://github.com/sagold/json-schema-library/tree/d2d6e6451ea3f3b8c056f497182fd805226a20a7

This is not self explanatory and I will add a clarification to the readme.

Sorry for the inconvenience,
Sascha

@tmcw
Copy link
Author

tmcw commented Jun 13, 2024

Hi - I don't think that's the issue, installing 10.0.0-rc1 doesn't resolve the issue, and the issue is about ESM exports and compatibility - if I dial it back to just Draft

import { Draft } from "json-schema-library";

console.log(Draft);

This still produces the error with the given repro:

file:///Users/tmcw/tmp/20240613162355-25356/index.mjs:1
import { Draft } from "json-schema-library";
         ^^^^^
SyntaxError: Named export 'Draft' 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 { Draft } = pkg;

@sagold
Copy link
Owner

sagold commented Jun 15, 2024

Note previous discussion on this topic #51

Can you share a simple build-setup to reproduce your issue?

@tmcw
Copy link
Author

tmcw commented Jun 15, 2024

The top post is a complete setup for reproducing the issue - here's the same thing as a gist if you want something git-clonable.

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

No branches or pull requests

2 participants