nrwl/nx

"Cannot find module" error when library has @myorgname in package.json's name

amiranvarov opened this issue · 5 comments

Current Behavior

when you create a new library with @myorg prefix in it's name, NX is not able to sreve it and generates an error

  1. Create a new NX project and any app (ex: myapp), for example NodeJS app.
  2. create any library, for example @nx/node library with a name "my-aweasome-service", and importPath name "@myorgname/my-aweasome-service". library with packge.json gets generated with a name "@myorgname/my-aweasome-service"
  3. Import anything from that library into myapp. For example "import { sayHi } from "@myorgname/my-aweasome-service". at this stage, typescript is seeing sayHi and it doesn't complain.
  4. Run nx serve myapp
    you will see error:

Error: Cannot find module '/Users/aanvarov/projects/neira/dist/libs/my-aweasome-service'
Require stack:

  • /Users/aanvarov/projects/nx-porject/dist/apps/myapp/apps/myappsrc/main.js
  • .....
    ...

But if i do remove @myprgname part from package.json's name, and leave just "my-aweasome-service" then NX serves my app fine.

Expected Behavior

NX should serve the app fine, without any error whether package.json contains the @myorg part in it's name or not

GitHub Repo

No response

Steps to Reproduce

  1. Create a new NX project and any app (ex: myapp), for example NodeJS app.
  2. create any library, for example @nx/node library with a name "my-aweasome-service", and importPath name "@myorgname/my-aweasome-service". library with packge.json gets generated with a name "@myorgname/my-aweasome-service"
  3. Import anything from that library into myapp. For example "import { sayHi } from "@myorgname/my-aweasome-service". at this stage, typescript is seeing sayHi and it doesn't complain.
  4. Run nx serve myapp
    you will see error:

Nx Report

I did try with latest NX (17.2) as well. Same issue i'm having

>  NX  Falling back to ts-node for local typescript execution. This may be a little slower.
  - To fix this, ensure @swc-node/register and @swc/core have been installed

 >  NX   Report complete - copy this into the issue template

   Node   : 20.6.1
   OS     : darwin-arm64
   yarn   : 1.22.21
   
   nx                 : 16.8.1
   @nx/js             : 16.8.1
   @nx/jest           : 16.8.1
   @nx/linter         : 16.8.1
   @nx/workspace      : 16.8.1
   @nx/devkit         : 16.8.1
   @nx/esbuild        : 16.8.1
   @nx/eslint-plugin  : 16.8.1
   @nx/node           : 16.8.1
   @nrwl/tao          : 16.8.1
   typescript         : 5.1.6

Failure Logs

No response

Package Manager Version

No response

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

No response

Just realized that this issue is related to mine too: #21030
Can you confirm you're using vite as bundler and nxViteTsPaths inside the viteconfig file?

Indeed there is a conflict when the name of the package (inside package.json) match the alias in the paths props (inside the tsconfig.base.json file). I explained my solution in detail inside that issue report; it seems that since npm creates a symbolic link inside the node_modules directory it generates a conflict when serving the app, my guess is that nxViteTsPaths is not working well (when Nx used vite-tsconfig-paths everything worked fine).

hey @AgentEnder!

nope, i'm not using vite at all. Any ideas what else may be an issue?