Skip to content
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

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined when use cjs/mjs config #133

Closed
azu opened this issue Jul 15, 2022 · 8 comments

Comments

@azu
Copy link
Contributor

azu commented Jul 15, 2022

@file-cache has used packemon for packaging.

And, packemon config is following.

  "packemon": [
    {
      "inputs": {
        "index": "./src/index.ts"
      },
      "platform": "node",
      "format": "cjs"
    },
    {
      "inputs": {
        "node": "./src/index.ts"
      },
      "platform": "node",
      "format": "mjs",
      "support": "experimental"
    }
  ],

https://github.com/azu/file-cache/blob/26d5a23128bfcb5680c007fedc5607c737a87780/packages/core/package.json#L13-L29

I aim to publish dual package CJS/MJS.
I've published this package, but this package can not be required from CJS.

It means that require("@file-cache/core") throw an error.

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined

Expected

No Error on require("@file-cache/core")

Actual

Can not require("@file-cache/core").
However, import * from "@file-cache/core" is work.

node:internal/modules/cjs/loader:488
      throw e;
      ^

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /Users/azu/ghq/github.com/azu/packemon-require-filed-missing/node_modules/@file-cache/core/package.json
    at new NodeError (node:internal/errors:372:5)
    at throwExportsNotFound (node:internal/modules/esm/resolve:472:9)
    at packageExportsResolve (node:internal/modules/esm/resolve:693:7)
    at resolveExports (node:internal/modules/cjs/loader:482:36)
    at Function.Module._findPath (node:internal/modules/cjs/loader:522:31)
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/Users/azu/ghq/github.com/azu/packemon-require-filed-missing/index.js:1:1) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

Details

Published @file-cache/core/package.json is following:

  "types": "./dts/index.d.ts",
  "main": "./cjs/index.cjs",
  "exports": {
    "./package.json": "./package.json",
    "./*": {
      "types": "./dts/*.d.ts",
      "node": {
        "import": "./mjs/*.mjs"
      }
    },
    ".": {
      "types": "./dts/index.d.ts",
      "node": {
        "import": "./mjs/index.mjs"
      }
    }
  }

I've noticed that that Conditional exports does not define require field like "require": "./cjs/index.cjs".

I mofify node_modules/@file-cache/core/package.json directory and require("@file-cache/core") is work.

  "types": "./dts/index.d.ts",
  "main": "./cjs/index.cjs",
  "exports": {
    "./package.json": "./package.json",
    "./*": {
      "types": "./dts/*.d.ts",
      "node": {
        "import": "./mjs/*.mjs"
      }
    },
    ".": {
      "types": "./dts/index.d.ts",
      "node": {
        "import": "./mjs/index.mjs"
+       "require": "./cjs/index.cjs"
      }
    }
  }

packemon format the exports field automatically. So I can not add require or default property to node.

Reproduce repository

Env

  • packemon@2.3.1
  • node@16.15.0
  • npm@8.13.2

Note

@azu
Copy link
Contributor Author

azu commented Jul 26, 2022

I've created minimal reproduce
https://github.com/azu/packemon-cjs-issue

@milesj
Copy link
Owner

milesj commented Jul 27, 2022

@azu Thanks, this should fix it #136

On a side note, I suggest not using cjs/mjs in parallel as you run the risk of the dual package hazard. https://nodejs.org/api/packages.html#dual-commonjses-module-packages I suggest one or the other, preferably cjs as packemon will automatically create an mjs wrapper. https://packemon.dev/docs/features#automatic-mjs-wrappers-for-cjs-inputs

@milesj
Copy link
Owner

milesj commented Jul 27, 2022

Published, let me know if there's still issues.

@milesj milesj closed this as completed Jul 27, 2022
@azu
Copy link
Contributor Author

azu commented Jul 29, 2022 via email

@azu
Copy link
Contributor Author

azu commented Jul 30, 2022

I want to treat my package as mjs package by default(type: module) because We may need to migrate Pure ESM in the future. I think that commonjs support is optional.
In other words, I want to treat .ts as mjs.

By contrast, index-wrapper.mjs force type: "commonjs". so it treat my pacakge as cjs package by default.

@milesj
Copy link
Owner

milesj commented Jul 31, 2022

@azu How does the index-wrapper.mjs force it to CJS?

@azu
Copy link
Contributor Author

azu commented Jul 31, 2022

@milesj
Reproduce repo: https://github.com/azu/packemon-cjs-and-mjs-wrapper

Reproduce Steps:

  1. change type to "module" manually
  2. npm run build
  3. packemon will change type to "commonjs"
Aug-01-2022.07-21-05_optimized.mp4

@milesj
Copy link
Owner

milesj commented Aug 1, 2022

@azu The type is set based on the chosen format: https://github.com/milesj/packemon/blob/master/packages/packemon/src/Package.ts#L475 And since the format is cjs, it sets it.

It shouldn't matter though since the wrapper will be used when importing from ESM because the import exports is set in exports.

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

No branches or pull requests

2 participants