[vite] Pre-transform error: Cannot find package pnpm路径过长导致运行报错

时间:2025-01-22 10:23:41

下了套vue3的代码,执行pnpm install初始化,使用vite启动,启动后访问就会报错

报错信息

 ERROR  16:40:53 [vite] Pre-transform error: Cannot find package 'E:\work\VSCodeProjectWork\jeecg\xxxxxxxxx-next\xxxxxxxxx-next-jeecgBoot-vue3\node_modules\.pnpm\@vitejs+plugin-vue-jsx@https++++@vitejs+plugin-vue-jsx+-+plugin-vue-jsx_wpfjsfrcyfhppiaqhs2qki2ffu\node_modules\@babel\plugin-transform-typescript\' imported from E:\work\VSCodeProjectWork\jeecg\xxxxxxxxx-next\xxxxxxxxx-next-jeecgBoot-vue3\node_modules\.pnpm\@vitejs+plugin-vue-jsx@https++++@vitejs+plugin-vue-jsx+-+plugin-vue-jsx_wpfjsfrcyfhppiaqhs2qki2ffu\node_modules\@vitejs\plugin-vue-jsx\dist\
Did you mean to import @babel+plugin-transform-typescript@https++++@babel+plugin-transform-typ_t6dvr774uyzoiatluosu5niumu/node_modules/@babel/plugin-transform-typescript/lib/?

 ERROR  16:41:02 [vite] Internal server error: Cannot find package 'E:\work\VSCodeProjectWork\jeecg\xxxxxxxxx-next\xxxxxxxxx-next-jeecgBoot-vue3\node_modules\.pnpm\@vitejs+plugin-vue-jsx@https++++@vitejs+plugin-vue-jsx+-+plugin-vue-jsx_wpfjsfrcyfhppiaqhs2qki2ffu\node_modules\@babel\plugin-transform-typescript\' imported from E:\work\VSCodeProjectWork\jeecg\xxxxxxxxx-next\xxxxxxxxx-next-jeecgBoot-vue3\node_modules\.pnpm\@vitejs+plugin-vue-jsx@https++++@vitejs+plugin-vue-jsx+-+plugin-vue-jsx_wpfjsfrcyfhppiaqhs2qki2ffu\node_modules\@vitejs\plugin-vue-jsx\dist\
Did you mean to import @babel+plugin-transform-typescript@https++++@babel+plugin-transform-typ_t6dvr774uyzoiatluosu5niumu/node_modules/@babel/plugin-transform-typescript/lib/?
  Plugin: vite:vue-jsx
  File: E:/work/VSCodeProjectWork/jeecg/xxxxxxxxx-next/xxxxxxxxx-next-jeecgBoot-vue3/src/utils/factory/
      at legacyMainResolve (node:internal/modules/esm/resolve:214:26)
      at packageResolve (node:internal/modules/esm/resolve:840:14)
      at moduleResolve (node:internal/modules/esm/resolve:910:20)
      at defaultResolve (node:internal/modules/esm/resolve:1130:11)
      at  (node:internal/modules/esm/loader:396:12)
      at  (node:internal/modules/esm/loader:365:25)
      at  (node:internal/modules/esm/loader:240:38)
      at  (node:internal/modules/esm/loader:328:34)
      at importModuleDynamically (node:internal/modules/cjs/loader:1261:33)
      at importModuleDynamicallyWrapper (node:internal/vm/module:430:21)
      at importModuleDynamicallyCallback (node:internal/modules/esm/utils:186:14)
      at  (E:\work\VSCodeProjectWork\jeecg\xxxxxxxxx-next\xxxxxxxxx-next-jeecgBoot-vue3\node_modules\.pnpm\@vitejs+plugin-vue-jsx@https++++@vitejs+plugin-vue-jsx+-+plugin-vue-jsx_wpfjsfrcyfhppiaqhs2qki2ffu\node_modules\@vitejs\plugin-vue-jsx\dist\:84:13)
      at  (file:///E:/work/VSCodeProjectWork/jeecg/xxxxxxxxx-next/xxxxxxxxx-next-jeecgBoot-vue3/node_modules/.pnpm/vite@5.2.13_@types+node@20.14.2_less@4.2.0/node_modules/vite/dist/node/chunks/:52318:62)
      at async loadAndTransform (file:///E:/work/VSCodeProjectWork/jeecg/xxxxxxxxx-next/xxxxxxxxx-next-jeecgBoot-vue3/node_modules/.pnpm/vite@5.2.13_@types+node@20.14.2_less@4.2.0/node_modules/vite/dist/node/chunks/:55073:29)
      at async viteTransformMiddleware (file:///E:/work/VSCodeProjectWork/jeecg/xxxxxxxxx-next/xxxxxxxxx-next-jeecgBoot-vue3/node_modules/.pnpm/vite@5.2.13_@types+node@20.14.2_less@4.2.0/node_modules/vite/dist/node/chunks/:64920:32)

同样的代码再同事电脑里就可以正常运行,
找到了个文章,解决方案是换个文件夹,原因的特殊字符,
jenkins目录下的vue3项目——pnpm install后运行报错——奇葩问题解决

我尝试了一下,将_改为了-,不好使,
E:\work\VSCodeProjectWork\jeecg\xxxxxxxxx-next\xxxxxxxxx-next-jeecgBoot-vue3
同级换个文件夹,不好使
E:\work\VSCodeProjectWork\jeecg\xxxxxxxxx_next\xxxxxxxxx-next-vue3
放在根目录下的A文件夹里,,好使,,
E:\A
按照原文件夹的名字拼接在一起变成了个巨长的文件夹名,不好使
E:\workVSCodeProjectWorkjeecgxxxxxxxxx_nextxxxxxxxxx-next-jeecgBoot-vue3
更改稍短点的路径,好使
E:\work\VSCodeProjectWork\jeecg\xxxxxxxxx_next\vue3

由此得出的pnpm不能在特别长的文件夹下使用

它的机制产生巨长的路径E:\work\VSCodeProjectWork\jeecg\xxxxxxxxx-next\xxxxxxxxx-next-jeecgBoot-vue3\node_modules\.pnpm\@vitejs+plugin-vue-jsx@https++++@vitejs+plugin-vue-jsx+-+plugin-vue-jsx_wpfjsfrcyfhppiaqhs2qki2ffu\node_modules\@babel\plugin-transform-typescript\' imported from E:\work\VSCodeProjectWork\jeecg\xxxxxxxxx-next\xxxxxxxxx-next-jeecgBoot-vue3\node_modules\.pnpm\@vitejs+plugin-vue-jsx@https++++@vitejs+plugin-vue-jsx+-+plugin-vue-jsx_wpfjsfrcyfhppiaqhs2qki2ffu\node_modules\@vitejs\plugin-vue-jsx\dist\实测已经561个字符了,与好使的路径只差26个字符,猜测是要控制在512以内,去除\ 536,536-26=510,也许就是答案吧,,

解除路径限制即可破局

找到了个解决方案【解除Windows系统中文件名和目录路径的最大长度限制】,此锅是微软的,不改路径长度也可以用

注册表路径为HKEY_LOCAL_MACHINE\System\CurrentControlSet\Policies
在右侧窗格中,右键点击空白处,选择“新建” > DWORD(32位)值
将新值命名为LongPathsEnabled(如果已存在则直接修改)。
双击该值,将其数据设置为1,然后点击“确定”保存设置。杀掉进程,然后运行即可,或者直接重启。