在 VSCode 扩展中获取:node-fetch 和 node:http 问题

Fetching in a VSCode extension: node-fetch and node:http issues

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

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

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

这是我的代码看起来像

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 window 但是如果我尝试从这里发出任何命令(假设 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。我在这里错过了什么?

从这两个问题来看:

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


如评论中所述,如果您降级到 v2,则可能需要这样做

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