Skip to content

Package type is module but main is CJS, producing errors #238

@IGx89

Description

@IGx89

Hello! Ran into an error running vitest on code that uses this library:

ReferenceError: exports is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and '...\node_modules\@dsb-norge\vue-keycloak-js\package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
 ❯ ../node_modules/@dsb-norge/vue-keycloak-js/dist/dsb-vue-keycloak.cjs.js:4:29

Error is due to package.json having type: module, indicating all .js files are ESM, and the file in main is CJS but ends in .js so is treated as ESM.

Solutions include removing type: module (or changing to type: commonjs), changing the file extension for the CJS bundle from .js to.cjs, or adding an exports field like so (current best practice):

  "exports": {
    ".": {
      "import": "./dist/dsb-vue-keycloak.es.js",
      "require": "./dist/dsb-vue-keycloak.cjs.js",
      "browser": "./dist/dsb-vue-keycloak.umd.js"
    }
  },

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions