How to dynamically query my VSCode extension version from the extension code?

Viewed 1368

I'm writing an extension for VSCode. I would like to get the runtime version of the extension from its own code. Is there a way to do that?

I found VSCode.extensions.getExtension('myExtensionId').packageJSON but I don't know what to do with that.

3 Answers

You can get the version in the activate function:

context.extension.packageJSON.version

where context is the ExtensionContext parameter from the activate function

When the extension is activated, the current extension context is passed as argument. You may use the context.extensionPath instead of hardcoded extension id string 'publisher.myExtensionId'.

import * as Path from 'path';
import * as fs from 'fs';

export function activate(context: vscode.ExtensionContext) {
    var extensionPath = Path.join(context.extensionPath, "package.json");
    var packageFile = JSON.parse(fs.readFileSync(extensionPath, 'utf8'));

    if (packageFile) {
        console.log(packageFile.version);
    }

//......... rest

}
Related