安装扩展后找不到模块

时间:2019-04-20 08:11:16

标签: javascript node.js typescript visual-studio-code vscode-extensions

我正在尝试创建VS Code扩展名。在我进行开发时,它可以正常工作,但是当我创建软件包并将其安装到VS Code时,它会失败,并出现以下错误:

  ERR Cannot find module 'request': Error: Cannot find module 'request'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:602:15)
    at Function.Module._load (internal/modules/cjs/loader.js:528:25)
    at Function.t._load (c:\Users\USER\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\services\extensions\node\extensionHostProcess.js:729:537)
    at Function.t.getExtensionPathIndex.then.a._load (c:\Users\USER\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\services\extensions\node\extensionHostProcess.js:691:639)
    at Function.t.getExtensionPathIndex.then.r._load (c:\Users\USER\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\services\extensions\node\extensionHostProcess.js:655:197)
    at Module.require (internal/modules/cjs/loader.js:658:17)
    at n (c:\Users\USER\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\loader.js:15:874)
    at openBambooPlanUrlInBrowser.GIT.getGitBranchFromFileName (C:\Users\USER\.vscode\extensions\dUSER.markdown-table-of-contents-0.0.1\out\extension.js:397:41)
    at getGitBranchFromFileName.exec (C:\Users\USER\.vscode\extensions\dUSER.markdown-table-of-contents-0.0.1\out\extension.js:383:17)
    at ChildProcess.exithandler (child_process.js:294:7)
    at ChildProcess.emit (events.js:182:13)
    at maybeClose (internal/child_process.js:961:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:248:5)

我的代码:

async openBambooPlanUrlInBrowser(fileName: string) {
    new GIT().getGitBranchFromFileName(fileName, (branch: string) => {
        var config: any = vscode.workspace.getConfiguration('markdown-table-of-contents').get('bitbucketRepositories');
        for (var setting of config) {

            if (fileName.toLowerCase().startsWith(setting.folder.toLowerCase())) {
                branch = branch.replace('/', '-');
                let bambooHost = vscode.workspace.getConfiguration('markdown-table-of-contents').get('atlassianBambooHost');
                const request = require('request');

                request(
                    {
                        url: `${bambooHost}/rest/api/latest/plan/${setting.bambooPlanKey}/branch/${branch}.json`,
                        headers: {
                            "Authorization": 'Basic ' + vscode.workspace.getConfiguration('markdown-table-of-contents').get('atlassianAuthHash')
                        }
                    },
                    (error: string, response: string, body: string) => {
                        let planKey = JSON.parse(body).key;
                        vscode.env.openExternal(vscode.Uri.parse(`${bambooHost}/browse/${planKey}`));
                    }
                );

            }
        }
    });

}

package.json的依赖

"dependencies": {
    "child_process": "^1.0.2",
    "clipboardy": "^1.2.3",
    "fs": "0.0.1-security",
    "iconv-lite": "^0.4.24",
    "path": "^0.12.7",
    "request": "^2.88.0",
    "util": "^0.11.1",
    "xml2js": "^0.4.19",
    "xmldom": "^0.1.27"
}

根文件夹:

.gitignore
.vscode
.vscodeignore
depl.bat
markdown-table-of-contents-0.0.1.vsix
node_modules
out
package-lock.json
package.json
src
tsconfig.json
tslint.json

1 个答案:

答案 0 :(得分:0)

对我来说,解决方案是从扩展程序的代码根文件夹运行npm install <package_name>(注意,没有“ -g”)。

这也会自动将其添加到devDependencies中,并且扩展从那时起就可以正常工作。