title: How can I properly configure Yarn 2 PnP on a monorepo with TS References configured?
body: <p>Short: <strong>For the project described below, how can I best configure things so that the two applications (client+server) run?</strong></p>
  <p>I am working on a longer running project that over time evolved into a mono repo. I cannot share the full code as the project is private, but the project has roughly the following structure, labelled with numbers so I can reference specific files easily in this example:</p>
  <pre>
   &lt;repo-root&gt;/
  1  package.json
  2  tsconfig.json
     client/
  3    package.json
  4    tsconfig.json
       pages/
         index.tsx
     global/
  5    package.json
  6    tsconfig.json
       src/
  9      index.ts
       build/
         index.js
         index.d.ts
     server/
  7    package.json
  8    tsconfig.json
       src/
         index.ts
       bin/
         index.js
  </pre>
  <p>Specifically, the client contains a <a href="https://nextjs.org/" rel="nofollow noreferrer">NextJS</a> project, the server contains a NodeJS <a href="https://www.npmjs.com/package/express" rel="nofollow noreferrer">Express</a> server, and the global directory contains some shared functions and interfaces that both the client and the server utilize.</p>
  <p>The client and server are able to access global via typescript project references, as well as using a typescript alias to map <code>@global/</code> to the global/src directory on the client, and to the global/build directory on the server, as seen in the tsconfig files below. The reason for there being a build directory in global at all is because we have a compiled environment in the server; the server application builds with <code>tsc</code> and runs with <code>node server.js</code> (roughly). Here are the numbered files, put in pastebin so the post isn't huge:</p>
  <ol>
  <li><a href="https://pastebin.com/hS41dCLg" rel="nofollow noreferrer">/package.json</a></li>
  <li><a href="https://pastebin.com/2sRFXUSh" rel="nofollow noreferrer">/tsconfig.json</a></li>
  <li><a href="https://pastebin.com/KZvvWm1t" rel="nofollow noreferrer">/client/package.json</a></li>
  <li><a href="https://pastebin.com/umjyReVA" rel="nofollow noreferrer">/client/tsconfig.json</a></li>
  <li><a href="https://pastebin.com/piLaLPV6" rel="nofollow noreferrer">/global/package.json</a></li>
  <li><a href="https://pastebin.com/hVj2thxV" rel="nofollow noreferrer">/global/tsconfig.json</a></li>
  <li><a href="https://pastebin.com/neUjyrnY" rel="nofollow noreferrer">/server/package.json</a></li>
  </ol>
  <p>8 is a code snippet because I ran out of guest pastes for the day:</p>
  <pre class="lang-json prettyprint-override"><code>{
      &quot;compilerOptions&quot;: {
          &quot;module&quot;: &quot;commonjs&quot;,
          &quot;removeComments&quot;: true,
          &quot;lib&quot;: [&quot;es2015&quot;, &quot;dom&quot;],
          &quot;sourceMap&quot;: true,
          &quot;esModuleInterop&quot;: true,
          &quot;target&quot;: &quot;es5&quot;,
          &quot;declaration&quot;: false,
          &quot;forceConsistentCasingInFileNames&quot;: true,
          &quot;isolatedModules&quot;: false,
          &quot;strict&quot;: false,
  
          &quot;noImplicitAny&quot;: false,
          &quot;noImplicitThis&quot;: false,
          &quot;baseUrl&quot;: &quot;./src&quot;,
          &quot;rootDir&quot;: &quot;./src&quot;,
          &quot;types&quot;: [&quot;node&quot;, &quot;jest&quot;],
          &quot;paths&quot;: {
              &quot;global/*&quot;: [&quot;../../global/build/*&quot;]
          },
          &quot;outDir&quot;: &quot;bin&quot;,
          &quot;plugins&quot;: [
              { &quot;transform&quot;: &quot;typescript-transform-paths&quot; },
  
              { &quot;transform&quot;: &quot;typescript-transform-paths&quot;, &quot;afterDeclarations&quot;: true }
          ]
      },
      &quot;include&quot;: [&quot;./src/**/*.ts&quot;],
      &quot;exclude&quot;: [&quot;*.d.ts&quot;, &quot;**/*.spec.ts&quot;, &quot;../global/**/*.ts&quot;],
      &quot;references&quot;: [{ &quot;path&quot;: &quot;../global&quot; }]
  }
  </code></pre>
  <p>This is the best we could do for setup. However, there are several issues remaining before our code could work again.</p>
  <p>We decided to migrate our mono repo to yarn pnp, because we ran into <a href="https://github.com/facebook/draft-js/issues/1763" rel="nofollow noreferrer">this issue</a> at run time, after I included a shared dependency between client and global (<code>draft-js</code>). The linked medium article in that github comment noted several possible solutions, but we were not using lerna, nor was our whole app using webpack (only client, as NextJS internally uses it). So our solution seemed to be in the footnote, yarn pnp.</p>
  <p>I could not find any documentation however on how to properly convert project references from ts into yarn PnP dependencies, or at least how to make everything work again in the yarn pnp environment.</p>
  <p>With this set up, we were first running into the issue that global doesn't build. It prints the error, &quot;Cannot find module 'firebase' or it's corresponding type declarations&quot;, even though firebase is clearly listed as a dependency in file 5, and I can see the zip file in the .yarn/cache directory for the right version of firebase. I've also verified that it is using the .pnp.cjs file, and I am running <code>yarn build</code> there (<code>yarn tsc --build</code>), and <code>yarn -v</code> prints that I am using the v3 canary version as expected; I cannot understand why somehow it can still not find the firebase module.</p>
  <p>After that issue though, the other problem is that the typescript aliases seem that they should now be replaced with <code>portal:</code> or <code>link:</code> dependencies as described in their docs <a href="https://yarnpkg.com/features/protocols#why-is-the-link-protocol-recommended-over-aliases-for-path-mapping" rel="nofollow noreferrer">here</a>. This did not work for us, it could not resolve the dependencies when trying to build the server specifically. Next, we tried using workspaces instead as that also seemed to promise to allow the client and server to access each-other. This is the configuration you can see in the pastes now. However, now when trying to compile global with this workspace configuration, I get an error similar to the <a href="https://github.com/yarnpkg/berry/issues/2470" rel="nofollow noreferrer">second snippet here</a>, about &quot;Unable to locate pnpapi&quot;, saying that global/.pnp.cjs is being controlled by the root .pnp.cjs</p>
  <p>Can anyone please advise, <strong>how could I best go about setting up this kind of project?</strong> What sort of refactoring is required, so that I can have one repository containing a NextJS TS client, a NodeJS server, and a shared global directory for shared TS interfaces and some functions (thus also some shared dependencies to get types / functions from, such as draft-js and firebase)?</p>
  <p>If I can provide more specific details or format this question better, please also comment about that.</p>
  <p>Thanks!</p>