nx icon indicating copy to clipboard operation
nx copied to clipboard

Failed to resolve entry for package when use react + vite

Open dfiai opened this issue 2 years ago • 5 comments
trafficstars

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

  1. create 2 packages using nx/react/vite app and lib image

  2. add import lib in the app image

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

dfiai avatar Aug 31 '23 09:08 dfiai

Related to https://github.com/nrwl/nx/issues/18492#issuecomment-1691942830

steven10172 avatar Sep 19 '23 02:09 steven10172

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! 🙏

github-actions[bot] avatar Mar 18 '24 00:03 github-actions[bot]

I am running into the exact same issue

dbehmoaras avatar Mar 30 '24 13:03 dbehmoaras

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)

MBelniak avatar Apr 03 '24 15:04 MBelniak

Hi! Could you please provide a repo with a minimal reproduction so that I can investigate this further?

Coly010 avatar May 17 '24 10:05 Coly010

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! 🙏

github-actions[bot] avatar May 25 '24 00:05 github-actions[bot]

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

Wurielle avatar Jun 28 '24 08:06 Wurielle

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?

kristianmandrup avatar Jul 02 '24 03:07 kristianmandrup

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.

github-actions[bot] avatar Aug 02 '24 00:08 github-actions[bot]