当前位置:   article > 正文

vue3项目打包错误_vue3 打包报错 [commonjs--resolver] failed to resolve e

vue3 打包报错 [commonjs--resolver] failed to resolve entry for package

错误1 包冲突

在这里插入图片描述
解决方法两种:
(1)在安装包的时候忽略冲突,安装命令后面加 --force or --legacy-peer-deps 例如:

npm install vue-router@next -S --legacy-peer-deps
  • 1

(2)更新所有的包到,最新,再来解决冲突
npm install -g npm-check-updates // 安装
ncu -u // 检测
npm install // 更新

2

vite v2.9.13 building for production...9 modules transformed.
[vite:resolve] Missing "./preload-helper" export in "vite" package
error during build:
Error: Missing "./preload-helper" export in "vite" package
    at bail (C:\Users\wumeng\AppData\Roaming\npm\node_modules\vite\dist\node\chunks\dep-80fe9c6b.js:38572:8)
    at resolve (C:\Users\wumeng\AppData\Roaming\npm\node_modules\vite\dist\node\chunks\dep-80fe9c6b.js:38649:10)
    at resolveExports (C:\Users\wumeng\AppData\Roaming\npm\node_modules\vite\dist\node\chunks\dep-80fe9c6b.js:40957:12)
    at resolveDeepImport (C:\Users\wumeng\AppData\Roaming\npm\node_modules\vite\dist\node\chunks\dep-80fe9c6b.js:40975:31)
    at tryNodeResolve (C:\Users\wumeng\AppData\Roaming\npm\node_modules\vite\dist\node\chunks\dep-80fe9c6b.js:40748:20)
    at Object.resolveId (C:\Users\wumeng\AppData\Roaming\npm\node_modules\vite\dist\node\chunks\dep-80fe9c6b.js:40556:28)
    at C:\Users\wumeng\AppData\Roaming\npm\node_modules\vite\node_modules\rollup\dist\shared\rollup.js:22870:37
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

第一个报错js,右键点进去,搜索 vite/preload-helper 替换为 \0vite/preload-helper 如图
在这里插入图片描述

3 terser not found. Since Vite v3, terser has become an optional dependency. You need to install it.

terser not found. Since Vite v3, terser has become an optional dependency. You need to install it.
  • 1
npm i  terser --legacy-peer-deps 
  • 1

安装这个包

4 vite3报错

TypeError: Cannot destructure property 'renderBuiltUrl' of 'config.experimental' as it is undefined.
    at toOutputFilePathInHtml (E:\BaiduSyncdisk\PHPproject\JavaScript\mksz542\node_modules\@vitejs\plugin-legacy\dist\index.cjs:25:11)
    at toAssetPathFromHtml (E:\BaiduSyncdisk\PHPproject\JavaScript\mksz542\node_modules\@vitejs\plugin-legacy\dist\index.cjs:62:10)
    at transformIndexHtml (E:\BaiduSyncdisk\PHPproject\JavaScript\mksz542\node_modules\@vitejs\plugin-legacy\dist\index.cjs:343:18)
    at applyHtmlTransforms (C:\Users\wumeng\AppData\Roaming\npm\node_modules\vite\dist\node\chunks\dep-80fe9c6b.js:36977:27)
    at Object.generateBundle (C:\Users\wumeng\AppData\Roaming\npm\node_modules\vite\dist\node\chunks\dep-80fe9c6b.js:36928:32)
    at C:\Users\wumeng\AppData\Roaming\npm\node_modules\vite\node_modules\rollup\dist\shared\rollup.js:22870:37
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

这个错误似乎是 plugin-legacy 2.0需要 vite 3.0才行
在这里插入图片描述
但是我是这样的也报错
在这里插入图片描述
这里的解决办法目前只能关闭兼容性

// 3.兼容处理
// import legacy from '@vitejs/plugin-legacy'

 // 兼容性设置
    // legacy({
    //   targets: ['defaults', 'not IE 11']
    // }),

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

5 node的中使用import引入模块导致错误

import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
  • 1
  • 2
  • 3

如下错误:

import fs from 'node:fs'
^^^^^^

SyntaxError: Cannot use import statement outside a module
  • 1
  • 2
  • 3
  • 4

解决方法:
package中添加type:module

"type": "module",
  "scripts": {
    "preview": "vite preview --port 4173",
    "lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs --fix --ignore-path .gitignore",
    "dev": "node server",
    "build": "npm run build:client && npm run build:server",
    "build:noExternal": "npm run build:client && npm run build:server:noExternal",
    "build:client": "vite build --ssrManifest --outDir dist/client",
    "build:server": "vite build --ssr src/entry-server.js --outDir dist/server",
    "build:server:noExternal": "vite build --config vite.config.noexternal.js --ssr src/entry-server.js --outDir dist/server",
    "generate": "vite build --ssrManifest --outDir dist/static && npm run build:server && node prerender",
    "serve": "cross-env NODE_ENV=production node server",
    "debug": "node --inspect-brk server",
    "start": "node server.js"
  },
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/338698?site
推荐阅读
相关标签
  

闽ICP备14008679号