打包组件报错:Error: Cannot find module ‘vue/compiler-sfc‘

解决Vue组件库Webpack打包错误:找不到'module/compiler-sfc'
在搭建Vue组件库并使用Webpack打包时遇到错误,问题源于'vue/compiler-sfc'模块未找到。错误出现在'webpack.config.js'文件中,尝试引入VueLoaderPlugin时出错。检查发现'vue-loader'版本为17.0.0,而该版本可能不包含所需模块。解决方案是回退到低版本的'vue-loader',如15.0.0,通过'npm install vue-loader@15-D'进行安装。执行此操作后,问题得到解决,成功打包组件库。

vue组件库搭建过程中使用webpack打包组件时报错,报错内容如下:

Error: Cannot find module 'vue/compiler-sfc'
Require stack:
- D:\vue2\moon-ui\node_modules\vue-loader\dist\index.js
- D:\vue2\moon-ui\webpack.config.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (D:\vue2\moon-ui\node_modules\vue-loader\dist\index.js:8:24)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'D:\\vue2\\moon-ui\\node_modules\\vue-loader\\dist\\index.js',
    'D:\\vue2\\moon-ui\\webpack.config.js'
  ]
}

发现报错文件:webpack.config.js

const path = require("path");
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const glob = require("glob");
const list = {};
​
async function makeList(dirPath, list) {
    const files = glob.sync(`${dirPath}/**/index.js`)
    for (const file of files) {
        const component = file.split(/[/.]/)[2];
        list[component] = `./${file}`;
    }
}
makeList("components/lib", list)
module.exports = {
    entry: list,
    output: {
        filename: '[name].umd.js',
        path: path.resolve(__dirname, "dist"),
        library: 'mui',
        libraryTarget: 'umd'
    },
    plugins: [
        new VueLoaderPlugin(),
    ],
    module: {
        rules: [
            {
                test: /\.vue$/,
                use: [
                    { loader: 'vue-loader' },
                ]
            }
        ]
    }
};

通过执行下面代码,测试当前文件是否可执行:

node webpack.config.js

发现当引入VueLoaderPligin时报错

const VueLoaderPlugin = require('vue-loader/lib/plugin');

查询发现package.json中vue-loader版本是17.0.0:

{
  ...
  "devDependencies": {
    ...
    "vue-loader": "^17.0.0",
    ...
  }
}

解决方法

npm i vue-loader@15 -D

安装低版本的vue-loader

重新尝试打包,解决问题!

{ "name": "vue-antd-jeecg", "version": "2.4.3", "private": true, "scripts": { "pre": "cnpm install || yarn --registry https://registry.npmmirror.com || npm install --registry https://registry.npmmirror.com ", "serve": "vue-cli-service serve", "build:test": "vue-cli-service build --mode test", "build": "vue-cli-service build", "lint": "vue-cli-service lint", "analyzer": "set analyzer=true && vue-cli-service build " }, "dependencies": { "@antv/data-set": "^0.11.8", "@jeecg/antd-onine": "^1.0.1", "@jeecg/antd-online-mini": "2.4.3-RC", "@tinymce/tinymce-vue": "^2.1.0", "@toast-ui/editor": "^2.5.4", "ant-design-vue": "^1.7.8", "area-data": "^5.0.6", "axios": "^0.28.0", "clipboard": "^2.0.11", "codemirror": "^5.65.5", "cssnano-preset-default": "^7.0.2", "dayjs": "^1.11.3", "dom-align": "1.12.0", "echarts": "^5.5.0", "enquire.js": "^2.1.6", "html2canvas": "^1.4.1", "jquery": "^3.5.0", "js-cookie": "^2.2.0", "jsonp": "^0.2.1", "jspdf": "^2.5.1", "lodash.get": "^4.4.2", "lodash.pick": "^4.4.0", "md5": "^2.3.0", "nprogress": "^0.2.0", "tinymce": "^7.0.0", "viser-vue": "^2.4.8", "vue": "^2.6.14", "vue-area-linkage": "^5.1.0", "vue-cookies": "^1.8.1", "vue-cropper": "^0.5.8", "vue-easy-printer": "^1.0.6", "vue-i18n": "^8.27.1", "vue-loader": "^16.8.3", "vue-ls": "^3.2.2", "vue-photo-preview": "^1.1.3", "vue-print-nb-jeecg": "^1.0.11", "vue-router": "^3.5.4", "vue-splitpane": "^1.0.4", "vue-to-pdf": "^1.0.0", "vuedraggable": "^2.24.3", "vuex": "^3.6.2", "vxe-table": "3.7.10", "vxe-table-plugin-antd": "1.8.10", "xe-utils": "2.4.8", "yarn": "^1.22.19" }, "devDependencies": { "@babel/polyfill": "^7.12.1", "@vue/cli-plugin-babel": "^3.3.0", "@vue/cli-plugin-eslint": "^3.3.0", "@vue/cli-service": "^3.3.0", "@vue/eslint-config-standard": "^4.0.0", "babel-eslint": "7.2.3", "compression-webpack-plugin": "^3.1.0", "eslint": "^5.16.0", "eslint-plugin-vue": "^5.1.0", "html-webpack-plugin": "^4.5.2", "less": "^3.13.1", "less-loader": "^4.1.0", "node-sass": "^4.11.0", "sass-loader": "^7.0.1", "vue-template-compiler": "^2.6.14", "webpack": "^4.46.0", "webpack-bundle-analyzer": "^4.5.0" }, "eslintConfig": { "root": true, "env": { "node": true }, "extends": [ "plugin:vue/strongly-recommended", "@vue/standard" ], "parserOptions": { "parser": "babel-eslint" }, "rules": { "generator-star-spacing": "off", "no-mixed-operators": 0, "vue/max-attributes-per-line": [ 2, { "singleline": 5, "multiline": { "max": 1, "allowFirstLine": false } } ], "vue/attribute-hyphenation": 0, "vue/html-self-closing": 0, "vue/component-name-in-template-casing": 0, "vue/html-closing-bracket-spacing": 0, "vue/singleline-html-element-content-newline": 0, "vue/no-unused-components": 0, "vue/multiline-html-element-content-newline": 0, "vue/no-use-v-if-with-v-for": 0, "vue/html-closing-bracket-newline": 0, "vue/no-parsing-error": 0, "no-tabs": 0, "indent": [ "off", 2 ], "no-console": 0, "space-before-function-paren": 0 } }, "postcss": { "plugins": { "autoprefixer": {} } }, "browserslist": [ "> 1%", "last 2 versions", "not ie <= 10" ] } 帮我设置版本合理,使依赖之间不会冲突,使用的使vue2
09-09
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xuelong-ming

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值