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

next build fails on importing a packaging via its conditional export #43698

Closed
1 task done
Alfred-Mountfield opened this issue Dec 4, 2022 · 5 comments
Closed
1 task done
Labels
bug Issue was opened via the bug report template.

Comments

@Alfred-Mountfield
Copy link
Contributor

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

❯ next info

Operating System:
  Platform: linux
  Arch: x64
  Version: Ubuntu 20.04.0 LTS Sun Dec 04 2022 17:02:37 GMT+0000 (Greenwich Mean Time)
Binaries:
  Node: 16.14.2
  npm: 7.17.0
  Yarn: 1.22.19
  pnpm: 7.13.6
Relevant packages:
  next: 13.0.7-canary.1
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0

Which area(s) of Next.js are affected? (leave empty if unsure)

No response

Link to reproduction - Issues with a link to complete (but minimal) reproduction code will be addressed faster

https://stackblitz.com/edit/vercel-next-js-r1qnjk?file=next-app/pages/index.tsx

To Reproduce

  • Add a dependency on a package which has a conditional export entrypoint.
  • Try and import via its conditional entrypoint path
  • Run next dev and see it succeeds
  • Run next build and see it fail

Describe the Bug

From my testing, next build doesn't seem to support importing from package's conditional imports. Weirdly next dev runs successfully, but when you run next dev you get errors about how the package can't be found:

$ next build
info  - Linting and checking validity of types ...Failed to compile.

./pages/index.tsx:5:21
Type error: Cannot find module 'demo-package/alt' or its corresponding type declarations.

  3 | 
  4 | /** Works in next dev, breaks in next build */
> 5 | import { foo } from 'demo-package/alt';

Expected Behavior

I would expect next build to be able to resolve the entrypoint like next dev does.

Which browser are you using? (if relevant)

Chrome 107.0.5304.110

How are you deploying your application? (if relevant)

No response

@Alfred-Mountfield Alfred-Mountfield added the bug Issue was opened via the bug report template. label Dec 4, 2022
@kachkaev
Copy link
Contributor

kachkaev commented Dec 4, 2022

Possible duplicate of #39375?

@kachkaev
Copy link
Contributor

kachkaev commented Dec 4, 2022

I managed to get the MVE working by adding typescript: { ignoreBuildErrors: true } to next-app/next.config.js.

npm install
cd next-app
npm run build
npm run start

The problem is with exports in the demo-package workspace.

  "exports": {
    ".": {
      "import": "./src/index.ts"
    },
    "./alt": {
      "import": "./src/index-alt.ts"
    },
    "./package.json": "./package.json"
  },
  "main": "./src/index.ts",

If we remove main and only leave exports, both import { foo } from 'demo-package' and import { foo } from 'demo-package/alt' break during build. That’s because TypeScript compiler does not automatically see typings via exports.

@kachkaev
Copy link
Contributor

kachkaev commented Dec 4, 2022

When we run npm run tsc --noEmit inside the web-app folder, the command fails with the same error. So if there is any bug here, I guess it’s outside Next.js.

I experimented a bit more and managed to get rid of typescript: { ignoreBuildErrors: true } in next-app/next.config.js with this config in demo-package/package.json:

  "exports": {
    ".": {
      "import": "./src/index.ts"
    },
    "./alt": {
      "import": "./src/index-alt.ts"
    },
    "./package.json": "./package.json"
  },
  "main": "./src/index.ts",
  "typesVersions": {
    "*": {
      "alt": ["src/index-alt.ts"]
    }
  }

Ideally, it’d be great to see this working in some future version of TypeScript:

  "exports": {
    ".": {
      "import": "./src/index.ts",
      "types": "./src/index.ts"
    },
    "./alt": {
      "import": "./src/index-alt.ts",
      "types": "./src/index-alt.ts"
    },
    "./package.json": "./package.json"
  },

That’s not the case yet.

@Alfred-Mountfield
Copy link
Contributor Author

Possible duplicate of #39375?

Looks fairly similar, I did a search but didn't find it, thanks!

When we run npm run tsc --noEmit inside the web-app folder, the command fails with the same error. So if there is any bug here, I guess it’s outside Next.js.

I'm also seeing that, even though I could have sworn I checked multiple times that tsc was passing without complaint but perhaps I'm getting my wires crossed. I'll close off this issue because I agree with your assessment that this is probably a problem elsewhere. Thanks!

@github-actions
Copy link
Contributor

github-actions bot commented Jan 4, 2023

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

2 participants