Javascript 语法错误:无法在模块外部使用import语句
当我尝试在VSC中运行调试器时,我一直收到此错误消息。有人能帮忙吗? 以下是一个屏幕截图: 我对编程非常陌生,并且正在学习一门课程,请尽可能保持基本的解释 下面是JS文件的代码。我已经使用Yo代码和NPM生成了一个基本的visualstudio代码扩展Javascript 语法错误:无法在模块外部使用import语句,javascript,visual-studio-code,vscode-debugger,Javascript,Visual Studio Code,Vscode Debugger,当我尝试在VSC中运行调试器时,我一直收到此错误消息。有人能帮忙吗? 以下是一个屏幕截图: 我对编程非常陌生,并且正在学习一门课程,请尽可能保持基本的解释 下面是JS文件的代码。我已经使用Yo代码和NPM生成了一个基本的visualstudio代码扩展 // The module 'vscode' contains the VS Code extensibility API // Import the module and reference it with the alias vsc
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import { commands, window } from 'vscode';
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is
activated
console.log('Congratulations, your extension "content-helper" is now
active!');
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
let disposable = commands.registerCommand('extension.helloWorld', function () {
// The code you place here will be executed every time your command is executed
// Display a message box to the user
window.showInformationMessage('Hello World!');
});
context.subscriptions.push(disposable);
}
const _activate = activate;
export { _activate as activate };
// this method is called when your extension is deactivated
function deactivate() {}
export default {
activate,
deactivate
}
VS代码扩展在本机不支持模块的节点环境中运行(因此没有
导入
和导出
)
yo code
仅在创建TypeScript扩展时使用import
。对于js扩展,yo code
改为使用require
:
const vscode=require('vscode');
要在VS代码扩展中使用
import
,必须使用TypeScript或webpack等工具将代码向下编译到目标节点。请向我们显示导致此错误的代码。当然,我基本上使用了“yo代码”和NPM来创建一个扩展。您能为lanch.json
添加更多信息吗?谢谢。我用什么来代替导入和导出?需要
和导出
:或者使用我提到的编译器Hi Matt,你能详细说明一下“将代码编译到目标节点”吗?@laike9m Typescript需要编译成Javascript(然后由Node.JS运行)才能运行。