简体   繁体   English

安装扩展后找不到模块

[英]Can't find the module after extension is installed

I'm trying to create VS Code extension.我正在尝试创建VS Code扩展。 It works when fine when I develop, however when I create the package and install it to VS Code it is failing with following error:当我开发时它工作正常,但是当我创建包并将其安装到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)

my code:我的代码:

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}`));
                    }
                );

            }
        }
    });

}

dependencies from package.json来自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"
}

root folder:根文件夹:

.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

For me the solution was to run npm install <package_name> (notice no "-g") from the extension's code root folder.对我来说,解决方案是从扩展的代码根文件夹运行npm install <package_name> (注意没有“-g”)。 Vscode puts the extension in its [extension folder][1], navigate there to do npm install . Vscode 将扩展放在其 [扩展文件夹][1] 中,导航到那里执行npm install

Example: for linux/mac示例:对于 linux/mac

cd ~/.vscode/extensions
cd your.extension
npm install

This automatically added it to the devDependencies as well, and the extension worked perfectly from there on.这也自动将它添加到 devDependencies,并且扩展从那里完美运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM