Why do I get an error about import declarations?

I am running Ubuntu 24.04 with GNOME Shell 46.0 and gjs 1.80.2

The example below works fine in Workbench, but when I try to run it in the terminal using gjs main.js I get an error:

(gjs:26360): Gjs-CRITICAL **: 19:20:40.677: JS ERROR: SyntaxError: import declarations may only appear at top level of a module @ main.js:1:0

FWIW, I get that error with any script that I try to write that uses imports, even though the imports are at the top of the file

import Adw from "gi://Adw";
import Gio from "gi://Gio";
import Gtk from "gi://Gtk?version=4.0";

Gio._promisify(Adw.AlertDialog.prototype, "choose", "choose_finish");

const box = workbench.builder.get_object("panel");

const button = new Gtk.Button({
  label: "Press me",
  margin_top: 6,
  css_classes: ["suggested-action"],
});
button.connect("clicked", () => {
  greet().catch(console.error);
});
box.append(button);

console.log("Welcome to Workbench!");

async function greet() {
  const dialog = new Adw.AlertDialog({
    body: "Hello World!",
  });

  dialog.add_response("ok", "OK");

  const response = await dialog.choose(workbench.window, null);
  console.log(response);
}

Thank you

I see now that I needed to use the -m switch (or in its longer name --module), i.e.

gjs -m main.js
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.