cedar-policy / cedar

Implementation of the Cedar Policy Language
https://www.cedarpolicy.com
Apache License 2.0
897 stars 80 forks source link

[WASM] Nodejs `import` statement missing within `package.json` `exports` keywords #1226

Closed Jimmy89 closed 1 month ago

Jimmy89 commented 2 months ago

Before opening, please confirm:

Bug Category

Other

Describe the bug

The wasm npm package is missing a import statement in its package.json file, specifically within the exports object. Therefore, ESM modules within nodejs cannot import the nodejs subpackage without error.

package.json:

  "exports": {
    ".": {
      "import": "./esm/cedar_wasm.js",
      "types": "./esm/cedar_wasm.d.ts"
    },
    "./nodejs": {
      "require": "./nodejs/cedar_wasm.js",
      "import": "./nodejs/cedar_wasm.js", <--- this statement is missing and must be added.
      "types": "./nodejs/cedar_wasm.d.ts"
    },
    "./web": {
      "import": "./web/cedar_wasm.js",
      "types": "./web/cedar_wasm.d.ts"
    }
  }

Without this fix you get the error:

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './nodejs' is not defined by "exports" in PATH/node_modules/@cedar-policy/cedar-wasm/package.json imported from ESMODULE

Expected behavior

No NodeJS error.

Reproduction steps

  1. Create an index.mjs file containing the following lines:
const cedar = await import('@cedar-policy/cedar-wasm/nodejs');
console.log(cedar.getCedarVersion())
  1. Having the most basic package.json within the same directory containing the @cedar-policy/cedar-wasm dependency.
  2. Run yarn/npm install within that directory to install the cedar-wasm lib.
  3. Run node index.mjs. Get the error
  4. Now add within the node_modules/@cedar-policy/cedar-wasm/package.json file the fix as described within the description ( "import": "./nodejs/cedar_wasm.js",)
  5. Run node index.mjs again and get the version number.

Code Snippet

See reproduction steps

Log output

// Put your output below this line

Additional configuration

NodeJS v20

Operating System

Windows WSLv2 on Fedora Remix

Additional information and screenshots

No response