Debugging Local Packages with Pnpm and Vite: Common Issues and Solutions

Debugging Local Packages with Pnpm and Vite: Common Issues and Solutions

3月 3, 2024 · 3 分钟阅读时长 · 530 字 · -阅读 -评论

When developing with Pnpm + Vite, during local package debugging, Vite may still load the old version of the package. This article analyzes and resolves this issue.

Local Package Debugging Steps

Since the package manager is pnpm, the corresponding debugging package mounting steps are as follows:

  1. Execute pnpm link --global in the local package
  2. Execute pnpm link pkgname --global in the target project

After performing these steps, accessing the web browser will reproduce the issue of incorrect package versions.

Let’s first analyze the situation in vite/pnpm

.vite

When Vite builds a project, it creates a .vite folder under node_modules

https://static.1991421.cn/2024/2024-03-03-233708.jpeg

The .vite cache requires changes in any of the following conditions:

  • Package manager lockfile content, e.g. package-lock.json, yarn.lock, pnpm-lock.yaml or bun.lockb.
  • Patches folder modification time.
  • Relevant fields in your vite.config.js, if present.
  • NODE_ENV value.

To force a refresh in Vite, you need to use --force or delete the .vite directory. For more details, see the official documentation

When executing pnpm link, you’ll notice that the lock file doesn’t change, so the .vite cache won’t be updated.

.pnpm Directory

Packages in node_modules are soft-linked to the .pnpm folder. When executing pnpm link:

  1. The lock file won’t change
  2. The corresponding package in node_modules will be soft-linked to the globally linked package

Problem Analysis

In summary, the issue is that after pnpm link, the lock file doesn’t get updated. Although the package dependency links change (meaning the content changes), Vite is unaware of this. Therefore, the Vite cache needs to be manually refreshed. Manually deleting the .vite directory and restarting Vite will show that the correct content is loaded.

SourceMap

SourceMap is designed to facilitate debugging with source code, such as being able to search for source files in Chrome Network. The reason it can be retrieved is that when the browser has Network open, it attempts to load the corresponding map file based on the sourcemap address noted in the code file. The map file records the location of the source files, and then the source code files are loaded. If you can’t retrieve source code files in Chrome, you can check whether the map files and source files are being loaded successfully with the correct paths.

SourceMap Not Working When Debugging Webpack Packages in Vite

The reason is that the sourcemap generated by Webpack uses the webpack protocol for source files, which Vite doesn’t support. One solution is to switch to Vite for building.

![image-20240305231617186](/Users/alanhe/Library/Mobile Documents/comappleCloudDocs/Typora/image-20240305231617186.png)

{"version":3,"file":"index.js","mappings":"AAAA","sources":["webpack://laputarenderer/webpack/universalModuleDefinition"],"sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory(require(\"react\"));\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([\"react\"], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"laputarenderer\"] = factory(require(\"react\"));\n\telse\n\t\troot[\"laputarenderer\"] = factory(root[\"React\"]);\n})(self, (__WEBPACK_EXTERNAL_MODULE__12__) => {\nreturn "],"names":[],"sourceRoot":""}

Final Thoughts

Debugging local packages with Pnpm and Vite can be challenging due to caching mechanisms. The key insight is understanding that Vite’s dependency pre-bundling relies on lockfile changes to invalidate its cache. When using pnpm link, the lockfile remains unchanged, causing Vite to continue using cached versions of packages. The solution is to manually clear the Vite cache by deleting the .vite directory or using the --force flag when restarting the development server. This approach ensures that your local package changes are properly reflected during development.

Alan H
Authors
开发者,数码产品爱好者,喜欢折腾,喜欢分享,喜欢开源