Skip to content

[node] Unable to import from "schematic-class/esm/jsonclass.js" as an ES module #7

Description

@t2ym

[node] Unable to import from "schematic-class/esm/jsonclass.js" as an ES module

Root Cause

  • On node, extensions must be .mjs for ES modules of no type section package when the file path is specified
  • This behavior can be thought as expected

Error - reproducible with 0.0.9

import { JSONClass, JSONClassError } from 'schematic-class/esm/jsonclass.js';
         ^^^^^^^^^
SyntaxError: Named export 'JSONClass' not found. The requested module 'schematic-class/esm/jsonclass.js' 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 'schematic-class/esm/jsonclass.js';
const { JSONClass, JSONClassError } = pkg;

Workaround

  • import { JSONClass, JSONClassError } from "schematic-class"

Fix Candidates

  • Option 1: Add redundant "schematic-class/esm/jsonclass.mjs" as a copy of "schematic-class/esm/jsonclass.js"

  • Option 2: Specify an effective "exports" section in package.json without regression for [npm] Unable to import as ES module #6

  • "type": "module" is effective but harmful to CommonJS import

  • This configuration does not work

  "main": "jsonclass.js",
  "module": "esm/jsonclass.js",
  "exports": {
    ".": {
      "import": "./esm/jsonclass.js",
      "require": "./jsonclass.js"
    },
    "./esm/jsonclass.js": {
      "import": "./esm/jsonclass.js"
    }
  },

Notes

  • "schematic-class" on node and browser with importmap
  • "/node_modules/schematic-class/esm/jsonclass.js" on browser without importmap

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions