{"record":{"id":"31ea8b686f28554b","repo":"neoclide/coc.nvim","slug":"err-require-esm","errorCode":"ERR_REQUIRE_ESM","errorMessage":"require() of ES Module ${url} not supported. Use dynamic import() instead.","messagePattern":"require\\(\\) of ES Module (.+?) not supported\\. Use dynamic import\\(\\) instead\\.","errorType":"error_code","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/extension/loader.ts","lineNumber":241,"sourceCode":"\n  /**\n   * Node-compatible `require.resolve.paths` for a request from a parent\n   * module. Returns null for builtins, like Node does.\n   */\n  public resolvePaths(request: string, parent: ExtensionCommonJSModule): string[] | null {\n    return Module._resolveLookupPaths(request, this.parentModule(parent))\n  }\n\n  /**\n   * Extension-local require: API injection, builtins, then modules in this\n   * runtime.\n   */\n  public require(request: string, parent: ExtensionCommonJSModule): unknown {\n    if (request === 'coc.nvim') return this.runtime.api\n    if (this.isBuiltin(request)) return this.loadBuiltin(request)\n    let resolved = resolveExtensionModule(this.runtime, request, parent.filename, 'require')\n    if (resolved.type === 'file' && resolved.format === 'module') {\n      throw requireESMError(resolved.filename)\n    }\n    // Builtins and coc.nvim are handled above, so resolution always yields a\n    // file module here.\n    return this.load((resolved as any).filename, parent)\n  }\n\n  public load(filename: string, parent?: ExtensionCommonJSModule, isMain = false): unknown {\n    const cacheKey = this.normalizeFilename(filename)\n    const ext = path.extname(cacheKey).toLowerCase()\n    if (ext === '.json') return this.loadJson(cacheKey, parent)\n    if (ext === '.node') return this.loadNative(cacheKey, parent)\n    if (ext === '.js' || ext === '.cjs' || ext === '') return this.loadJavaScript(cacheKey, parent)\n    if (ext === '.mjs') throw requireESMError(cacheKey)\n    throw new Error(`Unsupported module type \"${ext}\" for ${cacheKey}`)\n  }\n\n  /**\n   * Load a native addon outside the VM. The addon is dlopen'd by Node and its","sourceCodeStart":223,"sourceCodeEnd":259,"githubUrl":"https://github.com/neoclide/coc.nvim/blob/50e974d9692461a69147d5cab146a8d3e439abe4/src/extension/loader.ts#L223-L259","documentation":"Extension require() refuses to load an ES module via CommonJS require. When module resolution finds a file whose package.json declares \"type\": \"module\" (an ESM .js or .mjs), the loader throws ERR_REQUIRE_ESM telling the extension author to use dynamic import().","triggerScenarios":"Extension code calls require('some-pkg') where the resolved package's package.json has \"type\": \"module\", or requires a .mjs file directly.","commonSituations":"A dependency upgraded to an ESM-only major version (e.g. chalk 5+, got 12+, node-fetch 3+) while extension code still uses require(); loading a .mjs file; mixing require of a dual package from a CommonJS extension.","solutions":["Use await import('some-pkg') instead of require in the extension code.","Pin/downgrade the dependency to the last CommonJS-compatible version.","Switch the extension itself to ESM if the loader configuration supports it.","Use a dual-format build (e.g. tsup/unbuild) so a CJS entry exists to require."],"exampleFix":"// before\nconst chalk = require('chalk')\n// after\nconst { default: chalk } = await import('chalk')","handlingStrategy":"fallback","validationCode":"const pkg = JSON.parse(fs.readFileSync(require.resolve('some-pkg/package.json'), 'utf8'))\nif (pkg.type === 'module') throw new Error('ESM-only dependency; use import()')","typeGuard":null,"tryCatchPattern":"let mod\ntry { mod = require('some-pkg') } catch (e) { if (e.code === 'ERR_REQUIRE_ESM') mod = await import('some-pkg').then(m => m.default ?? m); else throw e }","preventionTips":["Check package.json \"type\" of new dependencies before requiring them","Pin CJS-compatible versions of popular ESM-only packages (chalk@4, got@11, node-fetch@2)","Prefer dynamic import() for anything possibly ESM","Keep extensions building dual CJS/ESM output"],"tags":["esm","commonjs","module-loading","extensions"],"backgroundTag":"require-of-es-module","analyzedSha":"50e974d9692461a69147d5cab146a8d3e439abe4","analyzedAt":"2026-08-31T11:17:23.966Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}