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

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

Open
tasshi-me opened this issue Jan 13, 2023 · 5 comments · May be fixed by #156
Open

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

tasshi-me opened this issue Jan 13, 2023 · 5 comments · May be fixed by #156

Comments

@tasshi-me
Copy link

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.

  • Referring exports, import("y18n") refers ./index.mjs of y18n.
  • Referring module, import("y18n") refers ./build/lib/index.js of y18n.
{
  "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 [email protected]: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 vitejs/vite#11676 .

  • rollup refers node_modules/y18n/index.mjs via exports.
  • vite refers node_modules/y18n/build/lib/index.js via module.
Log (click to expand)

rollup.js tried to build the code import y18n from "y18n" (succeeded)

$ npm run build:rollup:import-default-export-of-y18n

> [email protected] 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)

$ npm run build:rollup:import-named-export-of-y18n 

> [email protected] 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)

$ npm run build:vite:import-default-export-of-y18n 

> [email protected] 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)

$ npm run build:vite:import-named-export-of-y18n 

> [email protected] 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",
}
@tasshi-me tasshi-me linked a pull request Jan 13, 2023 that will close this issue
@MrMadClown
Copy link

@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
Copy link
Author

@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
Copy link
Member

shadowspawn commented Mar 1, 2023

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
Copy link
Author

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

@shadowspawn
Copy link
Member

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.)

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

Successfully merging a pull request may close this issue.

3 participants