【问题标题】:Fetching in a VSCode extension: node-fetch and node:http issues在 VSCode 扩展中获取:node-fetch 和 node:http 问题
【发布时间】:2022-01-05 09:42:18
【问题描述】:

我对 VSCode 扩展开发非常陌生。所以这可能是一个微不足道的问题或已经讨论过的问题。但我无法让它工作。所以我正在寻求帮助。

我目前正在构建一个非常简单的扩展。使用来自命令托盘的命令(比如说:Light Me Up),它将显示一个随机引用作为信息消息。

这就是我想做的。我想从here 获取一堆引号,然后将它们存储在一个变量中,然后每次触发命令时我想选择一个随机的并显示它。

这是我的代码的样子

import * as vscode from 'vscode';
import fetch from 'node-fetch';
// const got = require('got');

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
    
    console.log('Congratulations, your extension "seecode" is now active!');

    let data;

    (async () => {
        const response = await fetch("https://zenquotes.io/api/quotes");
        data = await response.json();
        console.log(data);
    })();

    context.subscriptions.push(
        vscode.commands.registerCommand('seecode.helloWorld', () => {
            vscode.window.showInformationMessage('Hello from SeeCode! See you on the other side');
            }
        )
    );

    

    // context.subscriptions.push(vscode.commands.registerCommand('seecode.'));
}

这是我的 package.json

{
  "name": "seecode",
  "displayName": "SeeCode",
  "description": "Easy Visual DevOPs",
  "version": "0.0.1",
  "engines": {
    "vscode": "^1.63.0"
  },
  "categories": [
    "Other"
  ],
  "activationEvents": [
    "onCommand:seecode.helloWorld",
    "onCommand:seecode.lightMeUp"
  ],
  "main": "./dist/extension.js",
  "contributes": {
    "commands": [
      {
        "command": "seecode.helloWorld",
        "title": "Hello World"
      },
      {
        "command": "seecode.lightMeUp",
        "category": "SeeCode",
        "title": "Light Me Up"
      }
    ]
  },
  "scripts": {
    "vscode:prepublish": "npm run package",
    "compile": "webpack",
    "watch": "webpack --watch",
    "package": "webpack --mode production --devtool hidden-source-map",
    "compile-tests": "tsc -p . --outDir out",
    "watch-tests": "tsc -p . -w --outDir out",
    "pretest": "npm run compile-tests && npm run compile && npm run lint",
    "lint": "eslint src --ext ts",
    "test": "node ./out/test/runTest.js"
  },
  "devDependencies": {
    "@types/glob": "^7.1.4",
    "@types/mocha": "^9.0.0",
    "@types/node": "14.x",
    "@types/vscode": "^1.63.0",
    "@typescript-eslint/eslint-plugin": "^5.1.0",
    "@typescript-eslint/parser": "^5.1.0",
    "@vscode/test-electron": "^1.6.2",
    "eslint": "^8.1.0",
    "glob": "^7.1.7",
    "mocha": "^9.1.3",
    "ts-loader": "^9.2.5",
    "typescript": "^4.4.4",
    "webpack": "^5.52.1",
    "webpack-cli": "^4.8.0"
  },
  "dependencies": {
    "node-fetch": "^3.1.0"
  }
}

当我尝试运行扩展程序时,它会打开第二个 VSCode 窗口,但是如果我尝试从这里发出任何命令(假设是 Hello World 一个),那么它会给我以下错误

Activating extension 'undefined_publisher.seecode' failed: 
Cannot find module 'node:http' Require stack: 
- /home/shubhadeep/work/personal/vscode_extension/seecode/dist/extension.js
- /snap/code/85/usr/share/code/resources/app/out/vs/loader.js
- /snap/code/85/usr/share/code/resources/app/out/bootstrap-amd.js
- /snap/code/85/usr/share/code/resources/app/out/bootstrap-fork.js.

我该怎么做才能解决这个问题?我只想在控制台中查看 json。我在这里想念什么?

【问题讨论】:

  • node-fetch 缺少必需的模块:node-http
  • 我会这么认为,但我找不到逻辑。自己安装的时候不应该自动安装吗?
  • 如果您在与fetchconsole.log 相同的工作区中创建NodeJS 脚本(JavaScript 或TypeScript),它是否有效
  • 所以是的,当我在一个单独的 JS 文件中从node-fetch github 页面编写示例代码时,然后将 package.json 更改为添加 "type": "module" 然后像这样运行文件- node file_name.js 没有任何问题...
  • 也许npm install @types/node-fetch@2 更好?只是猜测。

标签: node.js typescript visual-studio-code vscode-extensions node-fetch


【解决方案1】:

从这两个问题来看:

Cannot find module 'node:http` on AWS Lambda v14
Problem with importing node-fetch v3.0.0 and node v16.5.0

看起来从 node-fetch v3 升级到 v3.1 很“麻烦”,并导致您看到某些错误。

一些用户正在降级到 v2(或者您可以尝试 v3 而不是您正在使用的 v3.1)。

卸载node-fetchnpm install -s node-fetch@2


如 cmets 中所述,如果您降级到 v2,您可能想要这样做

npm install -D @types/node-fetch@2 也是如此。


【讨论】:

  • 如上面评论中所述。有效。它在导入语句中向我显示了一个奇怪的错误。但它仍然可以。非常感谢。
猜你喜欢
  • 2018-04-17
  • 2021-10-05
  • 1970-01-01
  • 1970-01-01
  • 2018-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多