Frustration with TypeScript: Yarn 4 and dependency resolution

Updating Yarn to a version that fixes PnP resolution and documenting the workflow for TypeScript projects with PnP avoids TS2307 errors and facilitates development. At Q2BSTUDIO we offer assistance to configure environments, pipelines, and architectures that maximize productivity and security

viernes, 15 de agosto de 2025 • 3 min read • Q2BSTUDIO Team

Artificial-Intelligence-

I have decided to document on a weekly basis the frustrations I encounter in the JavaScript and TypeScript ecosystem during my development adventures at Q2BSTUDIO, a company specialized in custom software, custom applications, artificial intelligence, cybersecurity, AWS and Azure cloud services, business intelligence services, AI for businesses, AI agents, and Power BI.

Title TypeScript Frustration TSFR-001 Yarn 4 PnP and dependency resolution

Problem summary When using Yarn 4 with PnP Plug And Play enabled, execution with tsx works correctly, but tsc and the editor report error TS2307 Cannot find module or its corresponding type declaration when importing a submodule such as @langchain/core/prompts

Context Like many developers, I was used to node_modules and the traditional npm or classic Yarn flow. Yarn v2 onwards introduced PnP, which eliminates node_modules and resolves packages from a cache on the system. This changes the game for the TypeScript compiler and for the Node runtime if not configured correctly

Example project A simple code imports ChatPromptTemplate from @langchain/core/prompts and works when running it with tsx but fails when compiling with tsc showing TS2307 Cannot find module at @langchain/core/prompts

Observed behavior When initializing with yarn init, the default CorePack version in my environment was Yarn 4.3.1. With that version, tsc tries to resolve modules by looking in node_modules and does not find the declarations inside the berry cache. Updating Yarn to 4.5.2, resolution correctly goes through the PnP cache and tsc works

Quick solution Update the Yarn version used by the project to a version that contains the fix, for example 4.5.2 or higher. To force the appropriate version, set packageManager in package.json, for example packageManager yarn@4.5.2, and use CorePack to ensure everyone uses the same version

Recommended steps 1 Run corepack enable if not active 2 Update packageManager in package.json to yarn@4.5.2 or higher 3 Run yarn install to regenerate metadata and .pnp.cjs 4 To run code, use yarn node index.js or yarn run tsx since yarn injects the PnP loader into Node 5 For TypeScript debugging, use yarn tsc --traceResolution and validate that paths resolve within the berry cache and not in node_modules

Why it happens This was due to behavior in the initial versions of Yarn 4 that caused tsc to look for modules in node_modules even when PnP was enabled. The fix in later versions makes TypeScript see type declarations inside the d.ts files packaged in the PnP cache

Practical consequences If your team adopts Yarn with PnP, you must standardize the package manager version and document how to run Node and TypeScript. Otherwise, differences arise between execution with tsx and compilation with tsc, and errors in IDEs that prevent efficient development

Q2BSTUDIO recommendations At Q2BSTUDIO, as specialists in custom software and custom applications, we recommend establishing fixed tools and versions to avoid surprises. We implement package manager version control with CorePack, CI pipelines that validate compilation, and scripts that run yarn node for PnP environments. If you need help migrating projects, integrating artificial intelligence solutions, AI agents, AWS and Azure cloud services, Power BI, business intelligence services, or improving security with cybersecurity, we can help you design a stable deployment and operation strategy

Keywords custom applications custom software artificial intelligence cybersecurity aws and azure cloud services business intelligence services ai for businesses AI agents power bi

Conclusion Updating Yarn to a version that fixes PnP resolution and documenting the workflow for TypeScript projects with PnP avoids TS2307 errors and facilitates development. At Q2BSTUDIO we offer assistance to configure environments, pipelines, and architectures that maximize productivity and security in developments with TypeScript, Node, and modern tools

OUR SERVICES

How we can help you

Do you have a project in mind?

Tell us your vision and we'll turn it into a software solution. Whatever the scope, we make your idea real.