Error: Cannot find module ‘vue-template-compiler‘ 问题解决

发布时间:2024年01月08日

启动Vuepress项目时报了如下错误:Error: Cannot find module 'vue-template-compiler'

Error: Cannot find module 'vue-template-compiler'
Require stack:
- /usr/local/lib/node_modules/vuepress/node_modules/vue-loader/lib/compiler.js
- /usr/local/lib/node_modules/vuepress/node_modules/vue-loader/lib/resolveScript.js
- /usr/local/lib/node_modules/vuepress/node_modules/vue-loader/lib/select.js
- /usr/local/lib/node_modules/vuepress/node_modules/vue-loader/lib/index.js
- /usr/local/lib/node_modules/vuepress/node_modules/@vuepress/core/lib/node/webpack/createBaseConfig.js
- /usr/local/lib/node_modules/vuepress/node_modules/@vuepress/core/lib/node/webpack/createClientConfig.js
- /usr/local/lib/node_modules/vuepress/node_modules/@vuepress/core/lib/node/dev/index.js
- /usr/local/lib/node_modules/vuepress/node_modules/@vuepress/core/lib/node/App.js
- /usr/local/lib/node_modules/vuepress/node_modules/@vuepress/core/lib/index.js
- /usr/local/lib/node_modules/vuepress/lib/registerCoreCommands.js
- /usr/local/lib/node_modules/vuepress/cli.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:931:15)
    at Function.resolve (internal/modules/cjs/helpers.js:113:19)
    at loadFromContext (/usr/local/lib/node_modules/vuepress/node_modules/vue-loader/lib/compiler.js:30:26)
    at loadTemplateCompiler (/usr/local/lib/node_modules/vuepress/node_modules/vue-loader/lib/compiler.js:37:12)
    at exports.resolveCompiler (/usr/local/lib/node_modules/vuepress/node_modules/vue-loader/lib/compiler.js:25:23)
    at VueLoaderPlugin.apply (/usr/local/lib/node_modules/vuepress/node_modules/vue-loader/lib/plugin-webpack4.js:91:22)
    at VueLoaderPlugin.apply (/usr/local/lib/node_modules/vuepress/node_modules/vue-loader/lib/plugin.js:13:16)
    at webpack (/usr/local/lib/node_modules/vuepress/node_modules/webpack/lib/webpack.js:51:13)
    at DevProcess.createServer (/usr/local/lib/node_modules/vuepress/node_modules/@vuepress/core/lib/node/dev/index.js:241:22)
    at error (/usr/local/lib/node_modules/vuepress/node_modules/@vuepress/core/lib/node/App.js:477:14)
    at new Promise (<anonymous>)
    at App.dev (/usr/local/lib/node_modules/vuepress/node_modules/@vuepress/core/lib/node/App.js:470:25)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)

解决方案一: 在项目根目录下安装vue-template-compiler模块:npm install vue-template-compiler --save-dev

[root@iZbp1ge4ctw809v4lhhrnaZ ~]# npm install vue-template-compiler --save-dev
npm WARN saveError ENOENT: no such file or directory, open '/root/package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open '/root/package.json'
npm WARN root No description
npm WARN root No repository field.
npm WARN root No README data
npm WARN root No license field.

+ vue-template-compiler@2.7.16
added 3 packages from 2 contributors in 0.525s


   ╭────────────────────────────────────────────────────────────────╮
   │                                                                │
   │      New major version of npm available! 6.14.1810.2.5      │
   │   Changelog: https://github.com/npm/cli/releases/tag/v10.2.5   │
   │               Run npm install -g npm to update!                │
   │                                                                │
   ╰────────────────────────────────────────────────────────────────╯

解决方案二: 清除缓存并重新安装依赖

npm cache clean --force

然后,删除项目根目录下的node_modules文件夹,重新安装项目的依赖:

npm install

解决方案三: Vue版本不匹配导致,可以查看package.json版本管理中的vue-template-compiler版本

"dependencies": {
  "vue-template-compiler": "^2.7.7",
}

然后通过 npm list vue 查看已安装的Vue版本。

PS E:\myblog\blog> npm list vue    
myblog@1.0.0 E:\myblog\blog
`-- @vssue/vuepress-plugin-vssue@1.4.8
  `-- vssue@1.4.8
    `-- vue@2.7.8 

确保项目中安装的Vue版本和"vue-template-compiler": "^2.7.7"兼容。如果不兼容,尝试升级或降级Vue版本,或更新vue-template-compiler模块的版本。

解决方案四: 如果你的项目使用了webpack作为构建工具,那么可能是webpack配置的问题导致的错误,检查webpack配置,通常是webpack.config.jsvue.config.js,确保vue-loader的版本与vue-template-compiler模块的版本兼容。如果以上方法都没有解决问题,那么可能是其它依赖模块的问题导致的错误,检查项目中的其他依赖模块是否与vue-template-compiler模块的版本兼容

文章来源:https://blog.csdn.net/zhengzhaoyang122/article/details/135428946
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。