yargs / y18n

:ledger: the bare-bones i18n library used by yargs
ISC License
146 stars 48 forks source link

The files specified in `module` and `exports` are mismatched. #155

Open tasshi-me opened 1 year ago

tasshi-me commented 1 year ago

Hi there, and thanks for the great package. I encountered following unexpected behavior.

Environment

Summary

In the package.json, the files specified in module and exports are mismatched.

{
  "exports": {
    ".": [
      {
        "import": "./index.mjs",
        "require": "./build/index.cjs"
      },
      "./build/index.cjs"
    ]
  },
  "type": "module",
  "module": "./build/lib/index.js",
}

As a result, some bundler which doesn't support the exports field fails to build with yargs as a dependency.

That's because yargs uses the default export of y18n on lib/platform-shims/esm.mjs, and ./build/lib/index.js on y18n doesn't provide the default export.

Reproduction

I created the minimum reproduction in https://github.com/tasshi-playground/repro-y18n-default-export-broken .

$ git clone git@github.com:tasshi-playground/repro-y18n-default-export-broken
$ cd repro-y18n-default-export-broken
$ npm install
$ npm run build

I examined rollup.js and Vite. rollup supports exports of package.json, and Vite doesn't support due to the bug of https://github.com/vitejs/vite/issues/11676 .

Log (click to expand) ### rollup.js tried to build the code `import y18n from "y18n"` (succeeded) ```shell $ npm run build:rollup:import-default-export-of-y18n > repro-y18n-default-export-broken@1.0.0 build:rollup:import-default-export-of-y18n > rollup --config ./rollup.config.default-export.mjs src/importing-default-export.mjs → dist... created dist in 49ms ``` ### rollup.js tried to build the code `import { y18n } from "y18n"` (failed) ```shell $ npm run build:rollup:import-named-export-of-y18n > repro-y18n-default-export-broken@1.0.0 build:rollup:import-named-export-of-y18n > rollup --config ./rollup.config.named-export.mjs src/importing-named-export.mjs → dist... [!] RollupError: "y18n" is not exported by "node_modules/y18n/index.mjs", imported by "src/importing-named-export.mjs". https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module src/importing-named-export.mjs (1:9) 1: import { y18n } from "y18n"; ^ 2: 3: console.log(y18n); at error (/Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/shared/rollup.js:210:30) at Module.error (/Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/shared/rollup.js:13578:16) at Module.traceVariable (/Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/shared/rollup.js:13961:29) at ModuleScope.findVariable (/Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/shared/rollup.js:12442:39) at Identifier.bind (/Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/shared/rollup.js:8371:40) at CallExpression.bind (/Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/shared/rollup.js:6165:28) at CallExpression.bind (/Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/shared/rollup.js:9888:15) at ExpressionStatement.bind (/Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/shared/rollup.js:6169:23) at Program.bind (/Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/shared/rollup.js:6165:28) at Module.bindReferences (/Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/shared/rollup.js:13574:18) ``` ### Vite tried to build the code `import y18n from "y18n"` (failed) ```shell $ npm run build:vite:import-default-export-of-y18n > repro-y18n-default-export-broken@1.0.0 build:vite:import-default-export-of-y18n > vite build --config ./vite.config.default-export.mjs vite v4.0.4 building for production... ✓ 2 modules transformed. "default" is not exported by "node_modules/y18n/build/lib/index.js", imported by "src/importing-default-export.mjs". file: /Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/src/importing-default-export.mjs:1:7 1: import y18n from "y18n"; ^ 2: 3: console.log(y18n); error during build: RollupError: "default" is not exported by "node_modules/y18n/build/lib/index.js", imported by "src/importing-default-export.mjs". at error (file:///Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/es/shared/rollup.js:2041:30) at Module.error (file:///Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/es/shared/rollup.js:13062:16) at Module.traceVariable (file:///Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/es/shared/rollup.js:13445:29) at ModuleScope.findVariable (file:///Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/es/shared/rollup.js:11926:39) at Identifier.bind (file:///Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/es/shared/rollup.js:7855:40) at CallExpression.bind (file:///Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/es/shared/rollup.js:5649:28) at CallExpression.bind (file:///Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/es/shared/rollup.js:9372:15) at ExpressionStatement.bind (file:///Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/es/shared/rollup.js:5653:23) at Program.bind (file:///Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/es/shared/rollup.js:5649:28) at Module.bindReferences (file:///Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/es/shared/rollup.js:13058:18) ``` ### Vite tried to build the code `import { y18n } from "y18n"` (succeeded) ```shell $ npm run build:vite:import-named-export-of-y18n > repro-y18n-default-export-broken@1.0.0 build:vite:import-named-export-of-y18n > vite build --config ./vite.config.named-export.mjs vite v4.0.4 building for production... ✓ 2 modules transformed. dist/importing-named-export.vite.cjs 2.84 kB │ gzip: 1.10 kB ```

Idea

The file specified in the module field should be the same as exports.

{
  "exports": {
    ".": [
      {
        "import": "./index.mjs",
        "require": "./build/index.cjs"
      },
      "./build/index.cjs"
    ]
  },
  "type": "module",
-   "module": "./build/lib/index.js",
+   "module": "./index.mjs",
}
MrMadClown commented 1 year ago

@mshrtsr until this or the fix for the vite issue gets released, did you figure out a workaround? I just ran into this aswell.

tasshi-me commented 1 year ago

@MrMadClown Sorry, I still have not found any solution yet. Because we make the executables with vercel/pkg, I exclude node_modules from the build target of Vite and bundle them with vercel/pkg.

shadowspawn commented 1 year ago

Thanks mshrtsr

I think you are right about the "modules" field being incorrect. I haven't been able to reproduce what you saw, I think because the precedence order has been fixed in vite v4.1.0 in the meantime!

Adding your package-lock.json to your reproduction repo might let me see same as you, but with vite updated you can probably move on anyway!

tasshi-me commented 1 year ago

@shadowspawn Thank you for checking my reproduction! I added my package-lock.json to my reproduction repo.

shadowspawn commented 1 year ago

Thanks @mshrtsr!

With node v18.13.0 and the package-lock.json I now see the same errors you reported! (Not sure I paid enough attention to the node version, different errors for different node versions too.)