在语言服务器扩展中找不到模块 'vscode'
Cannot find module 'vscode' in language server extension
我们复制了 lsp 示例项目,在 server.ts 中,我们正在导入 @types/vscode
包以在创建 Diagnostic
实例时访问 Range
接口。但是,启动客户端时,我们从服务器收到此错误:
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'
]
}
这是给客户的package.json
。其他的都差不多。
{
"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"
}
}
这里是客户的tsconfig.json
(其他都差不多)
{
"$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"]
}
尝试删除所有 node_modules
文件夹并重新安装和重新编译。我错过了什么吗?还尝试了不同的导入方式(使用星标,特别是 Range
)。
我的版本:
~/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
尝试将“@types/vscode”移动到 devDependencies
原来 vscode
包不能在语言服务器中使用。相反,我们现在使用 vscode-languageserver
包中的 Range.create
和 Position.create
来创建 Diagnostic
个实例。
我们复制了 lsp 示例项目,在 server.ts 中,我们正在导入 @types/vscode
包以在创建 Diagnostic
实例时访问 Range
接口。但是,启动客户端时,我们从服务器收到此错误:
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'
]
}
这是给客户的package.json
。其他的都差不多。
{
"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"
}
}
这里是客户的tsconfig.json
(其他都差不多)
{
"$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"]
}
尝试删除所有 node_modules
文件夹并重新安装和重新编译。我错过了什么吗?还尝试了不同的导入方式(使用星标,特别是 Range
)。
我的版本:
~/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
尝试将“@types/vscode”移动到 devDependencies
原来 vscode
包不能在语言服务器中使用。相反,我们现在使用 vscode-languageserver
包中的 Range.create
和 Position.create
来创建 Diagnostic
个实例。