Skip to content

Commit

Permalink
fix: Resolve config modules with ESM createRequire()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
evocateur committed Jan 6, 2022
1 parent c7ea359 commit a71dbdf
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/loadConfig.js
@@ -1,5 +1,6 @@
/** @typedef {import('./index').Logger} Logger */

import { createRequire } from 'module'
import { pathToFileURL } from 'url'

import debug from 'debug'
Expand All @@ -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.
Expand Down

0 comments on commit a71dbdf

Please sign in to comment.