I’m having trouble creating a new vscode-extension inside an NX workspace. For some reason it doesn’t seem to run. The repository with the code has been pushed here. These are the commands I ran to create the workspace:
npx create-nx-workspace
> vscodetest
> Node
> None
> Integrated monorepo
> vscodeextension
> No Dockerfile
> Do it later - CICD
> No remote caching
cd vscodetest
npm i vscode
code .
main.ts
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
console.log('Congratulations, your extension "mintplayer-vidyano-extension" is now active!');
// The commandId parameter must match the command field in package.json
const disposable = vscode.commands.registerCommand('vscode-extension.helloWorld', () => {
vscode.window.showInformationMessage('Hello World from mintplayer-vidyano-extension!');
});
context.subscriptions.push(disposable);
}
export function deactivate() {}
// Run: npx nx build
// F5
// ctrl+P
// > Hello world
.vscode/tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "watch",
"problemMatcher": "$tsc-watch",
"isBackground": true,
"presentation": {
"reveal": "never"
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
.vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Run Extension In Dev Mode",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}/dist/packages/vidyano-extension"
],
"trace": false,
"internalConsoleOptions": "openOnFirstSessionStart",
"outFiles": [
"${workspaceFolder}/dist/packages/vidyano-extension"
],
"preLaunchTask": "${defaultBuildTask}"
}
]
}
nx.json
{
...,
"defaultProject": "vscodeextension"
}
Build and run the application:
npx nx build
npx nx serve vscodeextension
And actually you would also need a package.json inside the project
{
"name": "vscodeextension",
"displayName": "vscode-extension",
"description": "Automatically build applications",
"version": "0.0.1",
"engines": {
"vscode": "^1.93.0"
},
"categories": [
"Other"
],
"activationEvents": [],
"main": "./main.js",
"contributes": {
"commands": [
{
"command": "vscode-extension.helloWorld",
"title": "Hello World"
}
]
}
}
Now you should be able to hit F5 and this should launch a new VSCode window in debug mode.
However I’m getting the following error
Error: Cannot find module ‘vscode’
Call stack
Error: Cannot find module 'vscode' Require stack:
C:reposvidyano-extensiondistpackagesvidyano-extensionpackagesvidyano-extensionsrcmain.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1143:15)
at Function.Module._resolveFilename (C:reposvidyano-extensiontmpvidyano-extensionmain-with-require-overrides.js:41:36)
at Function.Module._load (node:internal/modules/cjs/loader:984:27)
at Function.Module._load (C:reposvidyano-extensionnode_modules@nxjssrcexecutorsnodenode-with-require-overrides.js:18:31)
at Module.require (node:internal/modules/cjs/loader:1231:19)
at require (node:internal/modules/helpers:179:18)
at Object.<anonymous> (C:reposvidyano-extensionpackagesvidyano-extensionsrcmain.ts:3:25)
at Module._compile (node:internal/modules/cjs/loader:1369:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1427:10)
at Module.load (node:internal/modules/cjs/loader:1206:32)
I checked, and the vscode module is actually in the node_modules folder.
How can I fix this?
Edit: Turns out NX Console is a VSCode extension inside an nx workspace too
Edit: I tried from scratch using this walkthrough (repository), but after launching (F5) I still cannot see my custom command. What I do see however, is that the wrong package.json ends up in the /dist/apps/exte folder