gpt4 book ai didi

node.js - 获取 VSCode 扩展 : node-fetch and node:http issues

转载 作者:行者123 更新时间:2023-12-01 23:07:24 26 4
gpt4 key购买 nike

我对 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。我在这里错过了什么?

最佳答案

从这两个问题来看:

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


关于node.js - 获取 VSCode 扩展 : node-fetch and node:http issues,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70590704/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com