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

sigma.js报错:全局作用域已存在名为sigma的对象

Fixing "Uncaught An object called sigma is already in the global scope" in Sigma.js

Hey there! No need to apologize—we all hit these little roadblocks when learning a new library, and this is a super common issue with Sigma.js. Let's break down what's going on and fix it step by step.

What's Causing the Error?

The main culprit here is duplicate Sigma.js script imports in your <head> section. You've included both sigma.js (the uncompressed full version) and sigma.min.js (the compressed production version) — these are the exact same library, just formatted differently. Loading both means the global sigma object gets defined twice, which triggers that error.

There's also a minor issue with your global g variable (it's not declared with const/let, which can lead to unintended side effects), but let's tackle the error first.

Step-by-Step Fixes

  • Remove duplicate script references: Delete either sigma.js or sigma.min.js from your <head> — you only need one. For most cases, the minified version (sigma.min.js) is better for performance.
  • Clean up unnecessary dependencies: Depending on the version of Sigma.js you're using, many utility modules like sigma.utils.js are already included in the main library. You can test removing these extra scripts first, and add them back only if you get specific missing functionality errors later.
  • Declare your graph variable properly: Use const to declare your g graph object to avoid polluting the global scope.
  • Optional: Remove the redundant refresh() call: Modern versions of Sigma.js automatically render the graph when you initialize the sigma instance, so calling s.refresh() right after creation isn't necessary (though it won't break anything if you leave it in).

Modified Working Code

Here's your updated code with all these fixes applied:

<html>
<head>
  <!-- Keep only one Sigma.js script, remove the duplicate -->
  <script src='js_use/sigma.min.js'></script>
  <!-- Only include additional scripts if you know you need them -->
  <script src='js_use/sigma.parsers.json.min.js'></script>
</head>
<body>
  <div id="container"></div>
  <script>
    // Declare graph with const to avoid global scope pollution
    const g = {
      "nodes": [
        { "id": "node_apple", "label": "Apple_Inc.", "x": 10, "y": 8, "size": 5 },
        { "id": "node_steve_jobs", "label": "Steve_Jobs", "x": 14, "y": 8, "size": 5 },
        { "id": "node_public_company", "label": "Public_Company", "x": 7, "y": 5, "size": 2 }
      ],
      "edges": [
        { "id": "e0", "source": "node_apple", "target": "node_steve_jobs" },
        { "id": "e1", "source": "node_apple", "target": "node_public_company" },
        { "id": "e2", "source": "node_steve_jobs", "target": "node_apple" }
      ]
    };

    // Initialize Sigma instance
    const s = new sigma({ graph: g, container: 'container' });
    // s.refresh(); // Optional, usually not needed
  </script>
</body>
</html>

Additional Tips

  • Make sure all your Sigma.js scripts are from the same version — mixing versions can cause unexpected bugs.
  • If you're still having issues, check your browser's dev tools (Console tab) for more detailed error messages, which can point to missing dependencies or other setup issues.

内容的提问来源于stack exchange,提问作者Aneri Sheth

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 07:12:39