Skip to content

Doesn't work with native ESM modules #59

Open
@tmcw

Description

@tmcw

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions