简体   繁体   中英

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

I am very very new to VSCode extension development. So this may be a trivial question or something that already been discussed. But I can't get to make it work. So I am seeking out help.

I have a very simple extension that I am building at the moment. With a command from the command pallet (Let's say: Light Me Up ) it will show a random quote as an information message.

This is what I want to do. I want to fetch a bunch of quotes from here and then store them in a variable and then each time the command is fired I want to select a random one and show it.

This is my code is looking like

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.'));
}

And this is my 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"
  }
}

when I try to run the extension it opens up the second VSCode window but then if I try to issue any command from here (Let's say the Hello World one) then it gives me the following error

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.

What do I do to fix this? I just want to see the json in the console. Wham am I missing here?

Judging from these two issues:

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

it looks like the upgrade from node-fetch v3 to v3.1 was "troublesome" and resulted in the error you are seeing for some.

A few users are downgrading to v2 (or you might try v3 rather than v3.1 which you are using).

Uninstall node-fetch and npm install -s node-fetch@2


As noted in the comments if you downgrade to v2 you may want to do

npm install -D @types/node-fetch@2 as well.


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM