You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu系统启动Gnome官方示例扩展时遇ReferenceError错误求助

解决GNOME扩展代码中ReferenceError: imports is not defined错误

问题原因

imports是GNOME Shell内置GJS环境特有的模块加载语法,不能直接用Node.js、浏览器或普通JS运行环境执行这段代码。你遇到的错误是因为代码没有在GNOME Shell的扩展运行环境中加载。

解决步骤

1. 确保扩展文件结构正确

把代码放在GNOME扩展的标准目录下:

  • 目录路径:~/.local/share/gnome-shell/extensions/你的扩展ID/
  • 目录内必须包含两个文件:
    • extension.js(你的代码)
    • metadata.json(扩展元数据,必填)

示例metadata.json内容(替换成你的信息):

{
  "uuid": "your-extension-id@example.com",
  "name": "My Test Extension",
  "description": "A test extension from the GJS Guide",
  "shell-version": ["45", "46"],
  "url": "https://example.com"
}

2. 安装并启用扩展

  • 方法1:用GNOME扩展管理器(Extension Manager)搜索你的扩展名称并启用
  • 方法2:用命令行:
    gnome-extensions enable your-extension-id@example.com
    
    执行后按Alt+F2输入r重启GNOME Shell,扩展会加载运行。

3. 验证与调试

  • 查看扩展日志确认是否正常运行:
    journalctl -f -o cat /usr/bin/gnome-shell
    
    启用扩展后应该能看到你代码里的initializing和enabling日志。

常见误区

  • 不要直接用node extension.js或浏览器打开执行代码,这些环境不支持GJS的imports模块系统
  • 必须保证metadata.json中的shell-version与你当前的GNOME Shell版本匹配(可通过gnome-shell --version查看)

内容的提问来源于stack exchange,提问作者0CT0 PUCE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 16:05:04