PnP module resolution inside a yarn workspace

Using yarn 1.19.1, on node v10.16.3 on macOS Catalina.

I’m trying to get workspaces + pnp to work, ultimately with the goal to get my CI system working faster thanks to PnP.

I’ve made a simple test repo to try it out. https://github.com/philipnilsson/yarn-pnp-ws-throwaway

In my real project I will need to run different webpack builds in my individual workspaces that resolve files in dependent workspaces. I’ve approximated this by having pkg1 in my repo depend on pkg2. I’ve then created an index.js simply with the contents

const { foo } = require('pkg2');
console.log('foo');

I’m attempting to run this as yarn node index.js while inside the pkg1 folder. This results in a module resolution failure as follows

12:35 $ yarn node ./index.js
yarn node v1.19.1
internal/modules/cjs/loader.js:638
    throw err;
    ^

Error: Cannot find module 'pkg2'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/Users/philipnilsson/src/test/pkg1/index.js:1:17)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
error Command failed.
Exit code: 1
Command: /usr/local/bin/node
Arguments: ./index.js
Directory: /Users/philipnilsson/src/test/pkg1
Output:

info Visit https://yarnpkg.com/en/docs/cli/node for documentation about this command.

However, running this from the root folder works just fine.

12:36 $ yarn node pkg1/index.js
yarn node v1.19.1
foo
✨  Done in 0.12s.

Am I doing something wrong here? I’d be happy to contribute some kind of documentation if this is sorted out – but I’ve been unable to find anything relevant. I’ve never had any similar issues when not using pnp as the workspace projects are just linked into the node_modules folder – so resolution works without any issues.

Author: Fantashit

1 thought on “PnP module resolution inside a yarn workspace

  1. Even less sure how one is supposed to do this on yarn 2, but this is what I tried. Appears to be the same.

    image

    yarn node pkg1/index.js still works on v2-rc12.

    Can’t seem to get yarn pnp to resolve sibling workspaces at all while I’m inside a terminal in a particular workspace. I might be missing something fundamental. Is there any info on how to use either yarn pnp or berry in a monorepo with multiple workspace projects that potentially have dependencies between them?

Comments are closed.