-
Notifications
You must be signed in to change notification settings - Fork 28.1k
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" doesn't recognise node subpath export patterns in npm packages #46267
Comments
This is unrelated to Next.js, actually.
(You can verify this by adding I checked your package and I found the According to the TS docs, it should be the first one: Also, for the barrel files there is a built-in transform now, that should transform imports for tree-shaking! See https://nextjs.org/docs/advanced-features/compiler#modularize-imports |
To confirm this, I tried replicating the same config and imports in a custom webpack setup. No errors on IDE, dev server, or build (with type checks). I'm only able to reproduce this issue in Next.js so doesn't seem a library packaging issue.
This is correct, thanks! But removing type checks from build isn't a viable solution. Since the IDE and a custom webpack setup don't reproduce this error for the same code, I'm guessing it's an issue with the typescript setup used by Next.js during build?
Thanks for the tip! The suggestion from the TypeScript team seems to come from this bug: microsoft/TypeScript#50762 Also note that the original error I reported is reproducible with
This seems to be because the Next.js app directory is using CJS for some reason even though I have Looks like this is related to #39375 and #32239 (reply in thread)
I checked the docs for Modularize Imports but they say that the imports are transformed to default ones. Does this mean this only works for libraries that provide default exports? The transform I'm looking for is to named exports. // this
import { MyComponent } from "my-lib";
// becomes
import { MyComponent } from "my-lib/MyComponent"; Doesn't look like this config is the solution for tree shaking named exports. Should I open a separate feature request for supporting this transform? |
This comment was marked as off-topic.
This comment was marked as off-topic.
I'm having some issues with this as well. I can publish the package I've been trying to convert to use subpath exports and an app that consumes it if needed. I having all sorts of trouble importing a component. Sometimes it works in dev mode, but always fails a build.
And the exports of that "exports": {
"./components/*": {
"types": "./dist/components/*.d.ts",
"import": "./dist/components/*.js"
},
"./utils/*": {
"types": "./dist/utils/*.d.ts",
"import": "./dist/utils/*.js"
},
"./style.css": "./dist/src/style.css"
}, |
I have the same case as well |
Same here! "exports": {
"./lib": {
"types": "./src/lib/index.ts",
"import": "./out/lib/index.js"
},
"./types": {
"types": "./src/types/index.ts",
"import": "./out/types/index.js"
},
}, and then |
I have a reproduction repo up for this here: https://github.com/CobyPear/repro-subpath-exports I am struggling to understand how to bundle a component library for use with the app directory given what seems to be a bug, since when importing a non-tree shaken file, the The modularize imports section in the next.config did not help, or I misconfigured it. I'm not sure why I would need to do that anyway if I already modularized my exports in the library. |
After some more digging into this and messing with the exports field, it seems like Next.js does pick up exports but only if the entire path is defined, and patterns don't seem to work properly. // this works, but why do I need to be explicit with the entire path?
"./dist/components/header": {
"types": "./dist/components/header.d.ts",
"import": "./dist/components/header.mjs",
"require": "./dist/components/header.js"
},
// this does not work
"./components/*": {
"types": "./dist/components/*.d.ts",
"import": "./dist/components/*.mjs",
"require": "./dist/components/*.js"
}, @balazsorban44 sorry for the ping but I'm curios if there is some discrepancy with the Next.js compiler and the way it reads subpath exports vs how they are documented to work in the Node.js docs. |
this works for me too, but wow this is not going to be good dx for my library users. Is there a better way to do this, or have you figured out a way to at least mask this so consumer apps get a better dx / cleaner import? |
@Jared-Dahlke I am having this exact issue for an icon library. Did you end up just using explicit paths? I was really hoping to avoid including |
same here as @jamieomaguire "exports": {
".": "./dist/index.js",
"./react/24/solid": {
"types": "./dist/react/24/solid/index.d.ts",
"import": "./dist/react/24/solid/index.js"
}
}, import { ActivitiesIcon } from 'icons/react/24/solid';
// ^^^^^^^^^^^^^^^^^^^^
// Cannot find module 'icons/react/24/solid' or
// its corresponding type declarations.ts(2307) |
I'm trying to publish a component library that Next.js users can use individual components from without having to worry about bundle size overhead. Since Next.js doesn't tree shake the root import, as a library author, I have to provide deep imports to the individual components as a workaround. But the build command fails for deep imports.
Verify canary release
Provide environment information
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 22.2.0: Fri Nov 11 02:03:51 PST 2022; root:xnu-8792.61.2~4/RELEASE_ARM64_T6000
Binaries:
Node: 18.13.0
npm: 8.19.3
Yarn: 1.22.19
pnpm: N/A
Relevant packages:
next: 13.1.7-canary.26
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)
App directory (appDir: true), CLI (create-next-app), Package manager (npm, pnpm, Yarn), TypeScript
Link to the code that reproduces this issue
https://codesandbox.io/p/sandbox/purple-dream-eum7fq
To Reproduce
app/page.tsx
, the editor recognises the import correctly.next dev
runs the application without errors.next build
throws an error:Type error: Cannot find module
In general, use a subpath import (e.g.
import { something } from "my-package/something";
) from an npm package that has defined this underexports
field in itspackage.json
:Describe the Bug
The
next build
command is not able to recognise subpath imports from a package that exports them using Node's wildcard exports syntax. This error only occurs on build, thenext dev
command throws no errors, neither does the IDE.Expected Behavior
next build
should not throw an error for subpath imports from an npm package exporting them using wildcard syntax.Which browser are you using? (if relevant)
Microsoft Edge Version 110.0.1587.50 (Official build) (arm64)
How are you deploying your application? (if relevant)
Vercel
The text was updated successfully, but these errors were encountered: