Skip to content

Commit

Permalink
fix: require.resolve doesn't work with ESM
Browse files Browse the repository at this point in the history
Replace it with `createRequire` from `node:module`.
  • Loading branch information
sapegin committed Jun 9, 2024
1 parent 3f23692 commit 3adf257
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'fs';
import { createRequire } from 'node:module';
import stripJsonComments from 'strip-json-comments';
import { matchCasing } from 'match-casing';
import type {
Expand Down Expand Up @@ -32,8 +33,10 @@ const MARK_GROUPS = [
/**
* Load JSON file, strip comments.
*/
function loadJson(filepath: string) {
const json = fs.readFileSync(require.resolve(filepath), 'utf8');
function loadJson(modulePath: string) {
const require = createRequire(import.meta.url);
const resolvedModule = require.resolve(modulePath);
const json = fs.readFileSync(resolvedModule, 'utf8');
return JSON.parse(stripJsonComments(json)) as string[];
}

Expand Down

0 comments on commit 3adf257

Please sign in to comment.