From a71dbdfa69bd3b222136bd4f4334af691221a916 Mon Sep 17 00:00:00 2001 From: Daniel Stockman <5605+evocateur@users.noreply.github.com> Date: Thu, 6 Jan 2022 11:44:04 -0800 Subject: [PATCH] fix: Resolve config modules with ESM createRequire() ES modules do not have require() available, we must construct it first. This fixes the CLI when passing `--config my-config-package`, broken in v12.0.0. The tests didn't catch this because Jest still doesn't support mocking ESM, and there's really no way to write a test for this right now. --- lib/loadConfig.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/loadConfig.js b/lib/loadConfig.js index 76493447d..3a17d76c0 100644 --- a/lib/loadConfig.js +++ b/lib/loadConfig.js @@ -1,5 +1,6 @@ /** @typedef {import('./index').Logger} Logger */ +import { createRequire } from 'module' import { pathToFileURL } from 'url' import debug from 'debug' @@ -8,6 +9,12 @@ import YAML from 'yaml' const debugLog = debug('lint-staged:loadConfig') +/** + * require() does not exist for ESM, so we must create it to use require.resolve(). + * @see https://nodejs.org/api/module.html#modulecreaterequirefilename + */ +const require = createRequire(import.meta.url) + /** * The list of files `lint-staged` will read configuration * from, in the declared order.