'Cannot find module 'vscode' in language server extension

We made a copy of the lsp sample project, and in server.ts we are importing the @types/vscode package to access the Range interface when creating Diagnostic instances. However, when launching the client we get this error from the server:

Error: Cannot find module 'vscode'
Require stack:
- /Users/gvalenc/git/Nick-Steinhauser/vscode-lsp-fork/server/out/server.js
    at Function._resolveFilename (node:internal/modules/cjs/loader:987:15)
    at node:internal/modules/cjs/loader:832:27
    at Function._load (node:electron/js2c/asar_bundle:5:13343)
    at Module.require (node:internal/modules/cjs/loader:1059:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/Users/gvalenc/git/Nick-Steinhauser/vscode-lsp-fork/server/out/server.js:27:29)
    at Module._compile (node:internal/modules/cjs/loader:1163:14)
    at Object..js (node:internal/modules/cjs/loader:1216:10)
    at Module.load (node:internal/modules/cjs/loader:1035:32)
    at node:internal/modules/cjs/loader:876:12 {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/gvalenc/git/Nick-Steinhauser/vscode-lsp-fork/server/out/server.js'
  ]
}

Here is the package.json for the client. The others are very similar.

{
  "name": "test-lib",
  "description": "test-lib",
  "author": "me",
  "license": "MIT",
  "version": "0.0.1",
  "publisher": "vscode",
  "repository": {
    "type": "git",
    "url": ""
  },
  "engines": {
    "vscode": "^1.63.0"
  },
  "dependencies": {
    "@types/vscode": "^1.67.0",
    "vscode-languageclient": "^7.0.0",
    "yaml-language-server": "^1.7.0",
    "log4js": "^6.4.4"
  },
  "devDependencies": {
    "@tsconfig/node16": "^1.0.2",
    "@vscode/test-electron": "^2.1.2"
  }
}

Here is the client's tsconfig.json (the others are about the same).

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "extends": "@tsconfig/node16/tsconfig.json",
  "compilerOptions": {
    "esModuleInterop": true,
    "declaration": true,
    "declarationMap": true,
    "sourceMap": true,
    "outDir": "out",
    "rootDir": "src",
    "strict": true,
    "noUnusedLocals": true,
    "noImplicitReturns": true,
    "forceConsistentCasingInFileNames": true,
    "target": "esnext",
    "module": "CommonJS",
    "moduleResolution": "node",
    "types": ["node", "vscode"]
  },
  "include": ["src"],
  "exclude": ["node_modules", ".vscode-test"]
}

Tried deleting all the node_modules folders and reinstalling and recompiling. Am I missing something? Also tried different ways to import (with star and specifically just Range).

My versions:

~/g/N/vscode-lsp-fork [validation]{4} ⛈️ »  npm -v                                                                   {13:54:06}
8.3.1
@   api:cloud   R:us-south
~/g/N/vscode-lsp-fork [validation]{4} ⛈️ »  node -v                                                                  {13:55:10}
v16.14.0

VS Code:
Version: 1.67.0
Commit: 57fd6d0195bb9b9d1b49f6da5db789060795de47
Date: 2022-05-04T12:14:47.100Z
Electron: 17.4.1
Chromium: 98.0.4758.141
Node.js: 16.13.0
V8: 9.8.177.13-electron.0
OS: Darwin x64 21.4.0


Solution 1:[1]

Turns out the vscode package can't be used in the language server. Instead we now use Range.create and Position.create from the vscode-languageserver package to create the Diagnostic instances.

Solution 2:[2]

try to move "@types/vscode" to devDependencies

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 GabeV
Solution 2 siasty