0

Im looking for help with basic lib dependency in an app. not a custom solution

Nx monorepo: Internal library imports not found after production build

I'm working in an Nx monorepo (v20.4.4) with a Node.js app called pulse. The app builds and runs fine in development (nx serve pulse), but in production, it fails with Cannot find module errors for internal libraries.

Folder structure (simplified):

apps/
  pulse/
    src/
      main.ts
libs/
  shared/
    mongo-connect/
      src/
        index.ts

Problem:

When I build the app:

nx build pulse

And try to run it:

node dist/apps/pulse/main.js

I get this error:

Error: Cannot find module '/.../node_modules/@readyfastcode/mongo-connect/src/index.js'.
Please verify that the package.json has a valid "main" entry

The output JS includes lines like: (code from dist that is failing)

module.exports = require("@readyfastcode/mongo-connect");

But @readyfastcode/mongo-connect is an internal lib — it's not published and doesn’t exist in node_modules. If I change the path to a relative one (like ../../libs/shared/mongo-connect/src/index.js), it works, but that’s not maintainable.

i saw others using transform paths but why? this is supposed to work without

i see suggestions to update apps/pulse/project.json with dependsOn or implicitDependencies. from what i tried that doesnt change anythin.


My environment:

  • Nx: 20.4.4
  • @nx/node: 20.4.4
  • @nx/js: 20.8.1
  • Node: 20.9

Question:

Im not looking for a custom solution. just looking for basic NX functionality. The relation ship between apps and libs is the whole point of nx. Im looking for a lesson or documentation or a debug route

Is there a clean way to either bundle the app or resolve internal lib paths like @readyfastcode/mongo-connect at runtime, without publishing them or rewriting import paths?


Any help would be appreciated!

5
  • Use npmrc docs.npmjs.com/cli/v9/configuring-npm/npmrc I dont remember the correct setup, but It gives you the option to choose a path for projects which u can host locally or on a remote. Commented May 1 at 21:46
  • medium.com/@debshish.pal/… Commented May 1 at 21:47
  • I will try these. but first... why? Nx is supposed to link to libs by default why do i need custom solution? Commented May 1 at 21:50
  • I am not NX expert, This was the solution which came to my mind. seems nx failed at its job. Commented May 1 at 21:53
  • I did something wrong. cant figure it out. Commented May 1 at 21:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.