nx
nx copied to clipboard
Failed to resolve entry for package when use react + vite
Current Behavior
I create 2 packages app and lib using react/vite, I got build app error when import lib in to the app.
Expected Behavior
Lib should be referenced in the app without errors.
GitHub Repo
No response
Steps to Reproduce
-
create 2 packages using nx/react/vite app and lib
-
add import lib in the app
-
npx nx build my-app: [commonjs--resolver] Failed to resolve entry for package "my-lib". The package may have incorrect main/module/exports specified in its package.json.
It was reproducible in ubuntu and windows , magically after renaming one of the package it started to work but after npm i, it stopped again
Nx Report
Node : 16.16.0
OS : win32-x64
npm : 8.11.0
nx : 16.7.4
@nx/js : 16.7.4
@nx/jest : 16.7.4
@nx/linter : 16.7.4
@nx/workspace : 16.7.4
@nx/devkit : 16.7.4
@nx/eslint-plugin : 16.7.4
@nx/react : 16.7.4
@nrwl/tao : 16.7.4
@nx/vite : 16.7.4
@nx/web : 16.7.4
typescript : 5.1.6
Failure Logs
No response
Package Manager Version
8.11.0
Operating System
- [ ] macOS
- [X] Linux
- [X] Windows
- [ ] Other (Please specify)
Additional Information
No response
Related to https://github.com/nrwl/nx/issues/18492#issuecomment-1691942830
This issue has been automatically marked as stale because it hasn't had any recent activity. It will be closed in 14 days if no further activity occurs. If we missed this issue please reply to keep it active. Thanks for being a part of the Nx community! 🙏
I am running into the exact same issue
Has anyone tried enabling this option? https://vitejs.dev/config/shared-options#resolve-preservesymlinks It helped in my case (the path to main was being referenced from resolved location instead of path in node_modules)
Hi! Could you please provide a repo with a minimal reproduction so that I can investigate this further?
This issue has been automatically marked as stale because no reproduction was provided within 7 days. Please help us help you. Providing a repository exhibiting the issue helps us diagnose and fix the issue. Any time that we spend reproducing this issue is time taken away from addressing this issue and other issues. This issue will be closed in 21 days if a reproduction is not provided. If a reproduction has been provided, please reply to keep it active. Thanks for being a part of the Nx community! 🙏
Has anyone tried enabling this option? https://vitejs.dev/config/shared-options#resolve-preservesymlinks It helped in my case (the path to main was being referenced from resolved location instead of path in node_modules)
I just tried this but it didn't work for me although I found what's causing the issue in the process.
The issue only appeared when I added workspaces in my root package.json file.
Something about Vite aliases generated by nxViteTsPaths + the fact that the packages are already present within node_modules (due to workspaces) breaks the module resolution for Vite.
I haven't found a solution for my problem except by manually setting resolve.alias in my vite.config.ts for each package.
Setting resolve.preserveSymlinks = true doesn't change anything for me.
Edit:
After more fiddling I noticed resolveId from the nxViteTsPaths plugin is never called probably because Vite already resolved it
https://github.com/nrwl/nx/blob/336d371ceb4deccd0cf77cd8521ad4a128884617/packages/vite/plugins/nx-tsconfig-paths.plugin.ts#L155
As suggested in the issue under the link, adding enforce: 'pre' in node_modules/@nx/vite/plugins/nx-tsconfig-paths.plugin.js after name: 'nx-vite-ts-paths', solves the issue for me.
I'm using npx patch-package @nx/vite for now with "postinstall": "npx patch-package" under my scripts in my root package.json if anyone is looking for a solid fix on their version of nx
Hi @Wurielle do you have a gist or repo to look at which demonstrates this configuration?
Would I just need to insert "postinstall": "npx patch-package" in my root package.json?
This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.