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

按文档安装OpenLayers ESLint配置后仍遇'ol未定义'错误,如何解决?

Fixing the 'ol' is not defined ESLint Error with OpenLayers

I’ve run into this exact issue before when combining eslint-config-openlayers with stricter configs like airbnb-base. The problem boils down to either config order overriding the OpenLayers global definitions, or the stricter no-undef rules from Airbnb not picking up the implicit globals from the OpenLayers config. Here are two reliable fixes:

1. Explicitly declare ol as a global variable

Add a globals section to your .eslintrc.json to tell ESLint that ol is a read-only global variable. This bypasses any conflicts between the two configs:

{
  "extends": [
    "openlayers",
    "airbnb-base"
  ],
  "env": {
    "browser": true,
    "commonjs": true,
    "es6": true,
    "jquery": true
  },
  "globals": {
    "ol": "readonly"
  }
}

2. Adjust the extends order

ESLint applies configuration rules in the order they’re listed—later configs override earlier ones. If you move openlayers after airbnb-base, its global variable rules will take precedence over Airbnb’s stricter defaults:

{
  "extends": [
    "airbnb-base",
    "openlayers"
  ],
  "env": {
    "browser": true,
    "commonjs": true,
    "es6": true,
    "jquery": true
  }
}

Bonus: Verify package compatibility

Occasionally, version mismatches between eslint and eslint-config-openlayers can prevent the config from loading correctly. Check the peer dependency requirements in the eslint-config-openlayers package to ensure you’re using a supported ESLint version. You can do this by running:

npm ls eslint eslint-config-openlayers

This will show you the installed versions and any compatibility warnings.

Either of the first two solutions should resolve the 'ol' is not defined error immediately. The explicit global declaration is my go-to fix because it leaves no ambiguity for ESLint.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:48:28