Skip to content

Commit

Permalink
fix: require is not defined error (#47)
Browse files Browse the repository at this point in the history
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: #43
  • Loading branch information
richardlau authored Feb 28, 2022
1 parent 6d78f4a commit be4ec56
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions branch-diff.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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}}'
Expand Down

0 comments on commit be4ec56

Please sign in to comment.