VS Code扩展发布后报错:无法找到glob模块及命令缺失
问题:VS Code扩展发布后激活失败,找不到'glob'模块及命令
我开发的VS Code扩展在本地测试完全正常,但发布到应用市场后出现两个问题:
- 激活扩展失败,提示无法找到模块'glob'
- 弹出提示“Command 'Ex:Action One' resulted in an error,command 'ex:actionone' not found”
已尝试执行npm install glob、删除重建node_modules目录、卸载重装npm等操作,问题仍未解决。
报错详情
mainThreadExtensionService.ts:112 Activating extension 'Massdriver.vscode-massdriver-tools' failed: Cannot find module 'glob' Require stack: - /home/michael/.vscode-server/extensions/test.vscode-ex-tools-0.0.4/out/terraform/deploy.js - /home/michael/.vscode-server/extensions/test.vscode-ex-tools-0.0.4/out/extension.js - /home/michael/.vscode-server/bin/7f329fe6c66b0f86ae1574c2911b681ad5a45d63/out/vs/loader.js - /home/michael/.vscode-server/bin/7f329fe6c66b0f86ae1574c2911b681ad5a45d63/out/bootstrap-amd.js - /home/michael/.vscode-server/bin/7f329fe6c66b0f86ae1574c2911b681ad5a45d63/out/bootstrap-fork.js.
package.json配置
{ "scripts": { "vscode:prepublish": "npm run compile", "compile": "tsc -p ./", "watch": "tsc -watch -p ./", "pretest": "npm run compile && npm run lint", "lint": "eslint src --ext ts", "test": "node ./out/test/runTest.js" }, "dependencies": { "fs": "^0.0.1-security", "path": "^0.12.7", "semver": "^7.3.8", "glob": "^10.0.0" }, "devDependencies": { "@types/glob": "^8.0.0", "@types/mocha": "^10.0.0", "@types/node": "16.x", "@types/vscode": "^1.72.0", "@typescript-eslint/eslint-plugin": "^5.38.1", "@typescript-eslint/parser": "^5.38.1", "@vscode/test-electron": "^2.1.5", "eslint": "^8.26.0", "glob": "^10.0.0", "typescript": "^4.8.4" } }
tsconfig.json配置
{ "compilerOptions": { "module": "commonjs", "target": "ES2020", "outDir": "out", "lib": [ "ES2020" ], "sourceMap": true, "rootDir": "src", "strict": true, "esModuleInterop": true, "skipLibCheck": true /* enable all strict type-checking options */ /* Additional Checks */ // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ // "noUnusedParameters": true, /* Report errors on unused parameters. */ }, "exclude": [ "node_modules", ".vscode-test" ] }
解决方案
1. 修复'glob'模块缺失问题
- 清理重复依赖:移除
devDependencies中的glob,只保留dependencies里的版本,避免打包时混淆。 - 用Webpack打包依赖:VS Code扩展默认不会打包
node_modules,本地依赖在发布后不存在,需通过Webpack将依赖打包进输出文件:- 安装Webpack相关工具:
npm install webpack webpack-cli ts-loader --save-dev - 创建
webpack.config.js:const path = require('path'); module.exports = { target: 'node', entry: './src/extension.ts', output: { path: path.resolve(__dirname, 'out'), filename: 'extension.js', libraryTarget: 'commonjs2', devtoolModuleFilenameTemplate: '../[resource-path]' }, devtool: 'source-map', externals: { vscode: 'commonjs vscode' }, resolve: { extensions: ['.ts', '.js'] }, module: { rules: [ { test: /\.ts$/, exclude: /node_modules/, use: [{ loader: 'ts-loader' }] } ] } }; - 修改package.json的scripts:
"scripts": { "vscode:prepublish": "npm run package", "compile": "webpack --mode development", "watch": "webpack --mode development --watch", "package": "webpack --mode production", // 保留原有pretest、lint、test脚本 } - 执行
npm run package打包,检查out/extension.js是否包含glob代码。
- 安装Webpack相关工具:
2. 修复命令未找到问题
命令找不到是因为扩展激活失败导致命令未注册,解决模块问题后扩展正常激活,命令即可恢复。若仍有问题:
- 检查package.json的
contributes.commands配置,确保命令IDex:actionone与代码中registerCommand的ID完全一致(注意大小写)。 - 确认激活逻辑中没有提前退出,确保命令注册代码被执行。
3. 额外验证
- 用
vsce package生成.vsix文件,手动安装到VS Code测试,避免发布后才发现问题。 - 确认package.json的
main字段正确指向out/extension.js,且发布包包含整个out目录。
内容的提问来源于stack exchange,提问作者Ithius
相关产品推荐
相关产品推荐

