启动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.18 → 10.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.js
或vue.config.js
,确保vue-loader
的版本与vue-template-compiler
模块的版本兼容。如果以上方法都没有解决问题,那么可能是其它依赖模块的问题导致的错误,检查项目中的其他依赖模块是否与vue-template-compiler
模块的版本兼容