Javascript 中缺少模块,但 Typescript 中没有

Missing module in Javascript but not in Typescript

我正在按照指南编写 vscode 扩展程序,但在我的 server.ts 文件中遇到了导入问题。这是文件树:

test
 ┣ client
 ┃ ┣ out
 ┃ ┣ src
 ┃ ┃ ┗ index.ts
 ┃ ┣ package-lock.json
 ┃ ┣ package.json
 ┃ ┗ tsconfig.json
 ┣ server
 ┃ ┣ out
 ┃ ┣ src
 ┃ ┃ ┣ completions.ts
 ┃ ┃ ┣ parser.ts
 ┃ ┃ ┗ server.ts
 ┃ ┣ package-lock.json
 ┃ ┣ package.json
 ┃ ┣ tsconfig.json
 ┃ ┗ yarn.lock
 ┣ package.json
 ┗ tsconfig.json

与扩展示例 provided by Microsoft.

的结构相同

但是,在 server.ts 中,我试图从 vscode 模块中导入一些元素:

//server.ts

import { workspace as Workspace } from 'vscode';

let sm_home: string = Workspace.getConfiguration("sourcepawnLanguageServer").get("sourcemod_home");
...

在我的 package.json 中,我有:

  "dependencies": {
    "@types/glob": "^5.0.30",
    "@types/uuid": "^3.4.0",
    "@types/vscode": "^1.14.0",
    "glob": "^7.1.2",
    "uuid": "^3.1.0",
    "vscode-languageserver": "^7.0.0",
    "vscode-languageserver-textdocument": "^1.0.1",
    "vscode-test": "1.5.1",
    "vscode-uri": "^3.0.2"
  },

整个项目编译正常,但是当我启动扩展时,出现以下错误:

Error: Cannot find module 'vscode'
Require stack:
- c:\Users\Charles\CloudStation\Documents\Perso\Dev\sourcepawn-vscode\server\out\server.js
    at Function._resolveFilename (internal/modules/cjs/loader.js:1019:15)
    at internal/modules/cjs/loader.js:895:27
    at Function._load (electron/js2c/asar_bundle.js:5:12738)
    at Module.require (internal/modules/cjs/loader.js:1079:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (c:\Users\Charles\CloudStation\Documents\Perso\Dev\sourcepawn-vscode\server\out\server.js:5:18)
    at Module._compile (internal/modules/cjs/loader.js:1199:30)
    at Object..js (internal/modules/cjs/loader.js:1220:10)
    at Module.load (internal/modules/cjs/loader.js:1039:32)
    at internal/modules/cjs/loader.js:932:14 {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'c:\Users\Charles\CloudStation\Documents\Perso\Dev\sourcepawn-vscode\server\out\server.js'
  ]
}

如果我在 server.ts 文件中删除导入和使用导入的行,错误就会消失。 此导入在 index.ts` Could this be due to the fact that server.tsis called inside ofindex.ts` 中工作正常 ?

import { ExtensionContext, workspace as Workspace, window, commands } from 'vscode';
import { LanguageClient, LanguageClientOptions, ServerOptions, TransportKind } from 'vscode-languageclient/node';
import * as glob from 'glob';
import * as path from 'path';

export const sm_home: string = 'Workspace.getConfiguration("sourcepawnLanguageServer").get("sourcemod_home")';

export function activate(context: ExtensionContext) {
    let serverModule = context.asAbsolutePath(
        path.join('server', 'out', 'server.js')
...

这个好像不麻烦the C/C++ extension.

在此先感谢您的帮助!

vscode package has been deprecated. It has now been split up into two packages which are @types/vscode and vscode-test. The reason for this refactoring can be found here.

您包含了 @types/vscode 依赖项但未包含 vscode-test

添加 vscode-test 依赖项应该可以解决您的问题,即

npm i vscode-test

还要确保您拥有:

"engines": { "vscode": "^1.32.0" }

在您的 package.json 文件中定义。

一段时间后发现情况:

在 vscode 扩展中的语言服务器实例中使用 vscode 不起作用,因为(据我所知)服务器没有 运行 和 vscode 就像扩展一样。

如果您需要设置、工作区的 uris 等,您应该使用 connection.workspace...,但请记住请求将是异步的。

查看如何创建 connection 对象的示例 here