From be4ec5697cccdbfc9e95f602f69fa39015b5d70e Mon Sep 17 00:00:00 2001 From: Richard Lau Date: Mon, 28 Feb 2022 17:45:02 +0000 Subject: [PATCH] fix: require is not defined error (#47) The `require()` function is not defined in ES module scope. Loading a JSON file via `import` is behind `--experimental-json-modules` on LTS versions of Node.js, so use `createRequire()` instead. Refs: https://github.com/nodejs/branch-diff/pull/43 --- branch-diff.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/branch-diff.js b/branch-diff.js index 3436f83..1a6cad0 100755 --- a/branch-diff.js +++ b/branch-diff.js @@ -1,6 +1,7 @@ #!/usr/bin/env node import fs from 'fs' +import { createRequire } from 'module' import path from 'path' import process from 'process' import { pipeline as _pipeline } from 'stream' @@ -15,6 +16,7 @@ import gitexec from 'gitexec' const pipeline = promisify(_pipeline) const pkgFile = path.join(process.cwd(), 'package.json') +const require = createRequire(import.meta.url) const pkgData = fs.existsSync(pkgFile) ? require(pkgFile) : {} const pkgId = pkgtoId(pkgData) const refcmd = 'git rev-list --max-count=1 {{ref}}'