[英]import statement gives a syntax error saying “Cannot use import statement outside a module”
[英]Syntax error: Cannot use import statement outside a module
当我尝试在 VSC 中运行调试器时,我不断收到此错误消息。 有人可以帮忙吗? 这是一个屏幕截图:
我对编程和学习课程非常陌生,请尽可能保持基本的解释..
这是JS文件的代码。 我已经使用 Yo Code 和 NPM 生成了一个基本的 Visual Studio 代码扩展。
// 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 代码扩展在 Node 环境中运行,该环境本身不支持模块(因此没有import
和export
)。
yo code
仅在您创建 TypeScript 扩展时使用import
。 对于 js 扩展, yo code
改为使用require
:
const vscode = require('vscode');
要在 VS 代码扩展中使用import
,您必须使用 TypeScript 或 webpack 等工具将代码编译到目标节点
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.