#Node failing to resolve packages in a pnpm monorepo compiled from typescript

11 messages · Page 1 of 1 (latest)

spiral jasper
#

Hi all! Maybe this isn't entirely a ts question? But this feels like the most appropriate place I have access to that may be able to help.

So, I've been converting my project to a monorepo. I also switched from npm to pnpm. I have most everything set up. My packages are building fine. My code is compiling properly. However, in the last step, for some reason I am now unable to run node dist/index.js. It fails at the module resolver trying to resolve any of my workspace packages.

Here's the specific error:

  const resolvedOption = FSLegacyMainResolve(packageJsonUrlString, packageConfig.main, baseStringified);
                         ^

Error: Cannot find package '/Users/user/Documents/kobold/apps/client/node_modules/pf2etools-data/package.json' imported from /Users/user/Documents/kobold/apps/client/dist/src/index.js
    at legacyMainResolve (node:internal/modules/esm/resolve:213:26)
    at packageResolve (node:internal/modules/esm/resolve:831:14)
    at moduleResolve (node:internal/modules/esm/resolve:901:20)
    at defaultResolve (node:internal/modules/esm/resolve:1131:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:390:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:359:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:234:38)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:85:39)
    at link (node:internal/modules/esm/module_job:84:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}

Node.js v21.6.2```

I can run:
`user@laptop client % ls /Users/user/Documents/kobold/apps/client/node_modules/pf2etools-data/package.json
/Users/user/Documents/kobold/apps/client/node_modules/pf2etools-data/package.json`
So obviously the symlink is there. But node doesn't see it for some reason.

In the app/client/package.json, I have that module specified like this: ``"pf2etools-data": "workspace:*"`. And it's identified by pnpm, installed, and tsc compiles it, vscode uses its types, and typescript compilers can run it. But I just can't get the actual node executable to run it.

Weirdly enough, I can use node with the ts-node loader, and that works. Is there something I'm doing wrong here? Something weird with symlinks? Is there any way I can run that with the regular node loader?
earnest kite
#

why is src inside dist?

it's probably not quite the issue, but feels like dist and src not being on the same level could cause some indirect problems

spiral jasper
earnest kite
#

you could have a separate different tsconfig for those, which you should anyways if they use a different environment

spiral jasper
#

I just switched that back to a root of /src and a outdir of dist, and the issue persists

earnest kite
#

rip

spiral jasper
#

It's really strange, since loader=ts-node/esm causes it to work, but that's obviously a big performance decline for prod lol

spiral jasper
#

!helper

spiral jasper
#

Huh... figured it out. It turns out that this happened because I didn't define an exports field in that package's package.json. Very weird error for that, though.

earnest kite
#

huh... i shouldve asked what the import was, that wouldve probably been a pretty helpful clue.

hindsight is 20/20

spiral jasper
#

Yeah. I figured this out by just making a small ts test file to compile and run with node that imported each of my packages. Then I saw if any of them did work (which some did), and from there I did line comparisons on the package jsons and config files