“In VS Code extensions, your
package.json
isn’t just metadata — it’s the DNA that tells the editor what your extension can do.”
If you’ve ever wondered how VS Code knows:
- what commands your extension adds,
- when it should activate,
- what contributes to the sidebar or menus,
…it’s all because of one file — package.json
.
Let’s demystify it step-by-step, from scaffolding your extension using Yo Code, to wiring commands, contributions, and activation events.
🧙 Step 1: Generating Your Extension with Yo Code
The fastest way to start a VS Code extension is with the Yeoman generator called Yo Code.
🧩 Install the generator:
npm install -g yo generator-code
🪄 Generate a new extension:
yo code
You’ll be asked questions like:
- What type of extension do you want to create? (TypeScript / JavaScript)
- What’s the name of your extension?
- Description?
- Do you want to include tests?
After a few seconds, you’ll get a folder like this:
my-extension/
├── .vscode/
│ ├── launch.json
├── src/
│ ├── extension.ts
├── package.json
├── tsconfig.json
└── README.md
💡 Step 2: The Magic of package.json
Here’s a simplified look at what package.json
in a VS Code extension does (not just dependencies like a normal Node.js app):
{
"name": "my-awesome-extension",
"displayName": "My Awesome Extension",
"description": "A VS Code extension that says hello",
"version": "0.0.1",
"publisher": "your-name",
"engines": {
"vscode": "^1.80.0"
},
"categories": ["Other"],
"activationEvents": ["onCommand:extension.sayHello"],
"main": "./dist/extension.js",
"contributes": {
"commands": [
{
"command": "extension.sayHello",
"title": "Say Hello"
}
]
},
"scripts": {
"vscode:prepublish": "npm run compile",
"compile": "tsc -p ./"
},
"devDependencies": {
"@types/vscode": "^1.80.0",
"typescript": "^5.0.0"
}
}
🧩 Step 3: Understanding the Key Sections
Let’s break this down.
🧱 1. Basic Info
Defines the identity and compatibility of your extension.
{
"name": "my-awesome-extension",
"displayName": "My Awesome Extension",
"version": "0.0.1",
"publisher": "your-name",
"engines": {
"vscode": "^1.80.0"
}
}
-
publisher
: Your unique handle on the VS Code Marketplace. -
engines.vscode
: Ensures compatibility with a VS Code version.
⚙️ 2. Activation Events
Controls when your extension becomes active.
"activationEvents": [
"onCommand:extension.sayHello"
]
This means your code won’t even run until that command is triggered.
Other examples include:
"activationEvents": [
"onStartupFinished",
"onLanguage:python",
"onFileSystem:sftp",
"workspaceContains:package.json"
]
💡 Pro Tip:
Use precise activation events — it keeps your extension lightweight and faster to load.
🔧 3. Contributions — Declaring What Your Extension Adds
This section is where the magic happens.
It’s how your extension extends VS Code.
Example:
"contributes": {
"commands": [
{
"command": "extension.sayHello",
"title": "Say Hello"
}
],
"menus": {
"commandPalette": [
{
"command": "extension.sayHello",
"when": "editorTextFocus"
}
]
}
}
Here’s what’s happening:
-
commands
→ Declares what your extension can do. -
menus
→ Decides where these commands show up (e.g., Command Palette, right-click menu, etc.).
🧠 4. The “Main” Entry — Where Code Meets Config
This tells VS Code which file to execute when activating the extension.
"main": "./dist/extension.js"
And inside that file, you’ll find something like:
import * as vscode from "vscode";
export function activate(context: vscode.ExtensionContext) {
let disposable = vscode.commands.registerCommand(
"extension.sayHello",
() => {
vscode.window.showInformationMessage("Hello from your extension!");
}
);
context.subscriptions.push(disposable);
}
This code registers the extension.sayHello
command we declared in package.json
.
Run it inside VS Code (F5
to start the extension host), open the command palette, type Say Hello, and boom 💥 — your first extension is live.
🧰 Step 4: Building More Complex Extensions
As your extension grows, you can extend your package.json
with more contribution points:
Feature | Contribution Key | Example |
---|---|---|
Custom sidebar |
viewsContainers / views
|
Add panels like Git or Explorer |
Snippets | snippets |
Add code snippets for languages |
Themes | themes |
Define custom editor themes |
Keybindings | keybindings |
Add keyboard shortcuts |
Languages | languages |
Define custom syntax highlighting |
Example for snippets:
"contributes": {
"snippets": [
{
"language": "javascript",
"path": "./snippets/js-snippets.json"
}
]
}
🧪 Step 5: Running and Debugging Your Extension
- Press F5 in VS Code → opens a new Extension Development Host window.
- Use
console.log
orvscode.window.showInformationMessage()
to debug.
Debugging Tips:
- Set breakpoints directly in your
.ts
or.js
files. - Check the “Debug Console” for logs.
- Reload extension host quickly with Cmd + R (Mac) or Ctrl + R (Windows).
🚀 Step 6: Packaging and Publishing
When you’re ready to share your extension:
- Install VSCE (VS Code Extension CLI):
npm install -g vsce
- Package your extension:
vsce package
- Publish to the Marketplace:
vsce publish
✅ You’ll need to create a Personal Access Token on Azure DevOps (free).
💎 Advanced Patterns & Pro Tips
Trick | Description |
---|---|
when clauses |
Control visibility of menus, keybindings, etc. |
workspaceContains activation |
Load extensions only if a project contains specific files |
Use TypeScript | Great for type-safe VS Code APIs |
Extension Host Profiling | Check activation time using Developer: Show Running Extensions
|
Local commands | Use context keys to show/hide commands dynamically |
🌍 Example Scenarios
- Custom Developer Tools
- A React dev might create an extension that auto-generates components with prop templates.
- Team Productivity Extensions
- Add commands for quick branch switching, issue creation, or lint running.
- Custom Theming or UI
- Add a theme extension to unify colors across an entire workspace.
- Language Tools
- Add snippets, hover tips, or autocomplete for internal company DSLs.
🔗 Useful Resources
Resource | Link |
---|---|
Official VS Code Extension Docs | https://code.visualstudio.com/api |
VSCE (Publishing Tool) | https://github.com/microsoft/vsce |
Yeoman Generator for Code | https://github.com/microsoft/vscode-generator-code |
Extension Examples Repo | https://github.com/microsoft/vscode-extension-samples |
VS Code API Reference | https://code.visualstudio.com/api/references/vscode-api |
🧭 Final Thoughts
The package.json
in a VS Code extension isn’t just a dependency file — it’s the map that connects your JavaScript/TypeScript logic to the VS Code universe.
Once you understand it, you realize that:
- activation events define when,
- contributions define what,
- and your code defines how.
With these three working together, you can turn your ideas into powerful tools that enhance how developers code every day.
Top comments (0)