Skip to content

Commit

Permalink
fix: require is not defined error
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.
  • Loading branch information
richardlau committed Feb 26, 2022
1 parent a28abae commit 1a73cdb
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 1a73cdb

Please sign in to comment.