VS Code Extension Compile Errors: Troubleshooting Dependencies and Activation
This article explains how to troubleshoot common errors in TypeScript-based VS Code extensions, from compile-time red squiggles to extension activation failures. It covers the `package.json` fields that affect loading, `@types/vscode` and lockfile version alignment, Extension Host logs, workspace trust, and a practical troubleshooting checklist.
Start by Identifying the Framework You Are Using
This article uses a VS Code extension as its example. The development language is TypeScript, and the build tools are npm and tsc. Other IDE plugins follow similar loading principles, but their configuration file names, APIs, and error messages differ. Before continuing, you should be comfortable running npm commands in a terminal and reading package.json.
1. Distinguish Compile-Time from Runtime Errors
Red errors in extension development usually come from two stages, and each requires a different troubleshooting approach.
Compile-time errors are produced by the TypeScript compiler or the language service in the editor. They indicate problems with types or import statements, and the code has not run yet. Typical sources include missing type declaration packages, mismatched dependency versions, or incorrect module paths.
Runtime errors occur when the extension activates. After VS Code reads package.json, it calls the extension’s activate function at the appropriate time. If activate throws an exception, the extension fails to activate. The editor usually does not show a prominent pop-up; the error information is written only to the Extension Host log in the Output panel.
Determining which category an error belongs to first can prevent you from wasting time changing application code when the real issue is a dependency.
2. Key Fields in package.json
The entry point for a VS Code extension is determined by package.json. The following fields most often affect loading:
main: The compiled entry file, such as./out/extension.js. If the path is wrong, the extension cannot load.engines.vscode: The minimum supported VS Code version, such as^1.85.0.contributes: Static contribution points such as commands, menus, and configuration settings. If a command name is declared only here but is not registered in code, users will see a “command not found” error when they trigger it.activationEvents: The events that trigger activation. Newer versions of VS Code infer most activation events fromcontributes, so this field can often be omitted.
The extension lifecycle has four stages: reading the manifest, activation, running, and deactivation. Deactivation occurs when the editor closes or disables the extension. At that point, resources registered in context.subscriptions are automatically disposed.
3. Common Compile Errors and How to Verify Them
The table below lists four common symptoms. Each one includes a runnable verification command so you can reproduce the issue locally.
| Symptom | Possible Cause | Verification Command | Fix |
|---|---|---|---|
TS2307: Cannot find module 'vscode' |
@types/vscode is not installed, or the type configuration in tsconfig is incorrect. |
npm ls @types/vscode |
Run npm install -D @types/vscode@<version> and keep the installed version no higher than the version implied by engines.vscode. |
TS2339: Property 'xxx' does not exist on type 'typeof vscode' |
The type package version is lower than the API version used in the code. | Compare npm view @types/vscode version with npm ls @types/vscode. |
Upgrade the type package and update engines.vscode to the same major version. |
The editor reports an error, but npx tsc --noEmit passes. |
The language service is still using an old index cache. | npx tsc --noEmit |
Run TypeScript: Restart TS Server from the Command Palette. If that does not work, run Developer: Reload Window. |
Cannot find module './util' after moving files |
A relative path or path alias is broken. | Check the baseUrl and paths settings in tsconfig.json. |
Correct the import path or update the paths mapping. |
Before clearing caches or reinstalling dependencies, commit your code or back up package-lock.json, tsconfig.json, and the .vscode directory. This lets you roll back to the original state if something goes wrong.
4. Dependency Resolution: Version Ranges and Lockfiles
The way versions are written in package.json affects dependency resolution. For example:
"devDependencies": {"@types/vscode": "^1.85.0"}
^1.85.0 allows any version in the 1.x range that is not lower than 1.85.0. If team members do not commit package-lock.json, different machines may resolve different versions of the type package. This leads to situations where “it works on my machine” but reports errors on another.
When troubleshooting dependencies, follow this order:
- Run
npm ls @types/vscodeto see the version that is actually installed. Example output:
my-extension@0.0.1 /path/to/my-extension└── @types/vscode@1.85.0
- If the version does not match the team convention, run
npm ci. This command deletesnode_modulesand reinstalls dependencies strictly according topackage-lock.json; it does not update the lockfile. - If the lockfile does not exist or is damaged, run
npm installto generate a new lockfile, then commit it to version control.
There is also one constraint to keep in mind: the version of @types/vscode should not be higher than engines.vscode. Otherwise, the code may call APIs that do not exist in the user’s editor. Even if the local build passes, the extension can fail after the user installs it.
5. Runtime Problems: Activation Failures and Workspace Trust
Extensions run in a separate Extension Host process, apart from the editor UI. Extensions cannot directly manipulate page elements; they interact with the editor only through APIs provided by the vscode module. Here is a minimal activation example:
import * as vscode from 'vscode';export function activate(context: vscode.ExtensionContext) {const disposable = vscode.commands.registerCommand('demo.hello', () => {vscode.window.showInformationMessage('Extension activated');});context.subscriptions.push(disposable);}export function deactivate() {}
There are two key points in this code. First, the command must be registered inside activate, and its name must match the declaration in contributes. Second, the object returned by registration should be added to context.subscriptions; when the extension is deactivated, the editor automatically releases these resources, so no manual cleanup is needed.
To troubleshoot runtime issues:
- Open the Output panel and select
Extension Hostfrom the dropdown list to inspect the stack trace. - Run
Developer: Show Running Extensionsto check the extension’s activation state and timing. - Use F5 to launch the Extension Development Host window. Projects generated by the scaffold usually already have a launch configuration, and the window loads the extension that is currently being developed.
Workspace trust also affects extension behavior. In an untrusted workspace, an extension may have access only to limited functionality. Extension code can use vscode.workspace.isTrusted to determine whether the current workspace is trusted. If the extension needs to read scripts in the workspace or execute external commands, it should check this value first. In an untrusted workspace, show a prompt or skip the relevant functionality rather than failing directly.
6. Troubleshooting Checklist
Confirm the following items in order. They cover most compile errors and activation failures:
- Run
code --versionand record your local VS Code version. - Confirm that
engines.vscodeand@types/vscodeuse the same major version, and that the@types/vscodeversion is not higher than the version specified byengines.vscode. - Back up or commit
package-lock.jsonandtsconfig.json. - Run
npx tsc --noEmitto determine whether the error comes from compilation or the editor cache. - Run
npm cito reinstall dependencies according to the lockfile. - Run
TypeScript: Restart TS Serverand see whether the red markers disappear. - Check that command names in
contributesmatch the names passed toregisterCommandin the code. - Press F5 to launch the Extension Development Host and inspect the stack trace in the
Extension Hostoutput. - After each investigation, record the original error text, version numbers, and fix so the information can be reused.
7. Three Next Steps
- Use the official scaffold
yo codeto generate a TypeScript template. First get a minimal extension running to confirm that the environment works. - Intentionally write an incorrect command name or type name, observe the error text, and compare it with the table in Section 3 to practice moving from symptom to cause.
- Open the official VS Code Extension API documentation, switch to the version matching
engines.vscode, and use the property name in the error to find the relevant interface documentation.
