question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Building with yarn pnp (yarn 2+) ignores the 'browser' element of the package.json

See original GitHub issue

Describe the bug

The title pretty much says it. Unfortunately I don’t have the time to provide a reproduction. I can however provide a fix (see below) that I have tested in my environment.

The problem is with yarn pnp the importer ends up having a /* appended to it in some cases (I don’t fully understand why). This causes the check of the idToPkgMap in the resolver to fail since it misses, so the browser-specific resolution from the package.json is never found. Solution is to add an entry with the /* to the map in the yarn pnp case.

This shows up clearly when using the aws-sdk for example in a browser app (it will fail at runtime trying to reference a nodejs module).

Index: packages/vite/src/node/plugins/resolve.ts
<+>UTF-8
===================================================================
diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts
--- a/packages/vite/src/node/plugins/resolve.ts	(revision a4895ed9d71531c71243ffcb3aab465ecd5694a0)
+++ b/packages/vite/src/node/plugins/resolve.ts	(date 1643617573205)
@@ -10,6 +10,7 @@
   OPTIMIZABLE_ENTRY_RE
 } from '../constants'
 import {
+  isRunningWithYarnPnp,
   isBuiltin,
   bareImportRE,
   createDebugger,
@@ -557,6 +558,10 @@
 
   // link id to pkg for browser field mapping check
   idToPkgMap.set(resolved, pkg)
+  if (isRunningWithYarnPnp) {
+    // Corresponds to yarn pnp resolve in esBuildDepPlugin
+    idToPkgMap.set(path.dirname(resolved) + '/*', pkg)
+  }
   if (isBuild) {
     // Resolve package side effects for build so that rollup can better
     // perform tree-shaking

Reproduction

none

System Info

System:
    OS: macOS 12.1
    CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
    Memory: 19.67 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.17.3 - ~/.nvm/versions/node/v14.17.3/bin/node
    Yarn: 3.1.1 - /usr/local/bin/yarn
    npm: 8.1.0 - ~/.nvm/versions/node/v14.17.3/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman

Used Package Manager

yarn

Logs

No response

Validations

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
swandircommented, Jan 31, 2022

As a temporary workaround you may apply commits from the PR on top of a Vite version you need, pack a tarball and use it via file: protocol (example)

0reactions
francisucommented, Feb 1, 2022

As a temporary workaround you may apply commits from the PR on top of a Vite version you need, pack a tarball and use it via file: protocol (example)

I have done that and confirm that your PR fixes my issue, AWS works correctly with yarn pnp in the browser. Thanks for the work on this and I hope they merge it soon!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Plug'n'Play | Yarn - Package Manager
An overview of Plug'n'Play, a powerful and innovative installation strategy for Node.
Read more >
Advanced package manager features for npm, Yarn, and pnpm
It seems that styled-components does not list all of its dependencies in its package.json . There is a typical solution for such a...
Read more >
Migration - Parcel
Let's walk through a couple basic steps to upgrade from Parcel 1 to Parcel 2. Package name. #. The first thing to note...
Read more >
Configuring Jest
It is recommended to define the configuration in a dedicated JavaScript, TypeScript or JSON file. The file will be discovered automatically, if ...
Read more >
Introducing Yarn 2 ! - DEV Community ‍ ‍
yarn up <name> will upgrade a package in all workspaces at once; yarn add -i <name> will offer to reuse the same version...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found