encore icon indicating copy to clipboard operation
encore copied to clipboard

Encore doesnt allow imports from workspace packages in a pnpm monorepo

Open firstaxel opened this issue 9 months ago • 5 comments

Hello please i tried to initiate encore on pnpm monorepo and tried to import packages from my packages folder but it gives me an error message stating module no found error until i install the packages found in the packages/* folder for example packages/database contains drizzle-orm in my package.json file. Every time i run encore it tell me install drizzle-orm in the encore api folder before it can work and run encore run

Please is there a way to bypass and stop encore for telling me to install the package before i can make api run @fredr @marcuskohlberg

firstaxel avatar Mar 19 '25 08:03 firstaxel

Hi, could you create a git repository with a minimal reproduction so I can test it out? I have a hard time understanding your project structure, and there are so many ways of setting these things up.

Two things to consider, first make sure you are running the latest version of encore (v1.46.10) as we did some improvements for monorepos in that version. Secondly, when using pnpm, make sure that you set the packageManager field in your package.json (you can set it in the root package.json or the encore app package.json) to "pnpm@<version>" and replace <version> with whatever pnpm --version gives you.

fredr avatar Mar 19 '25 08:03 fredr

Github encore-monorepo this is the repo, encore is in the apps/apiw folder on the process of migration from nextjs api to encore api ts runtime

the issue is noticed that encore bundles everything into a single mainjs file so if the imported file contains npm packages from external pnpm workspace packages, encore wants the npm package to be in the apiw folder so it will not run until you install it meaning if the npm packages is installed in the packages/* folder encore doesnt recongnize it @fredr

firstaxel avatar Mar 19 '25 09:03 firstaxel

I was also having the same issues with a similar folder structure that @firstaxel mentioned.

Although this can be workaround by:

  1. Put all backend-related libs inside apps/encore-backend/libs/some-backend-lib. This way pnpm/nx/(any monorepo workspace tools) still be able to recognized this as an individual lib package, just have to add apps/encore-backend/libs/* into your workspace setting
  2. Put encore.app at root, and define the backend code in a folder (let's say api), while keeping other packages in separate folder. (With this way, we need to specify multiple tsconfig files for TS to work properly). One potential drawback is that the docker image size will be big since there's no ignore config file for encore yet...

Welp eventually I hope encore can work well with all the major monorepo tools without having to worry about these kinds of directory structure...

Looking forward for the updates..

darekaze avatar Apr 09 '25 16:04 darekaze

Similar issue, I have a relatively standard pnpm monorepo setup.

~/apps/* and ~/packages/*

I have my encore app stored at ~/apps/encore/* and a shared packaged at ~/packages/shared-config/*

and my package.json in my encore app @etx/shared-config": "workspace:*" but during runtime i get import errors

node:internal/modules/esm/resolve:265
    throw new ERR_MODULE_NOT_FOUND

etxdev avatar Apr 29 '25 11:04 etxdev

Linking to my own experience - I got Encore working with Turborepo with two simple steps:

  1. Build all workspace dependencies within your encore-app package postinstall script
  2. Declare packageManager within your encore-app package.json as well as your root

Context https://github.com/encoredev/encore/issues/2021#issuecomment-3135686720

jeff-computers avatar Jul 30 '25 10:07 jeff-computers