nodejs / branch-diff

A tool to list print the commits on one git branch that are not on another using loose comparison
52 stars 20 forks source link

fix: require is not defined error #47

Closed richardlau closed 2 years ago

richardlau commented 2 years ago

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

Fixes this error when there is a package.json file in the current working directory:

$ nvm run 17 .
Running node v17.6.0 (npm v8.5.1)
file:///home/rlau/sandbox/github/branch-diff/branch-diff.js:18
const pkgData = fs.existsSync(pkgFile) ? require(pkgFile) : {}
                   ^

ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '/home/rlau/sandbox/github/branch-diff/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file:///home/rlau/sandbox/github/branch-diff/branch-diff.js:18:20
    at ModuleJob.run (node:internal/modules/esm/module_job:197:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:341:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)

Node.js v17.6.0
github-actions[bot] commented 2 years ago

:tada: This PR is included in version 2.0.2 :tada:

The release is available on:

Your semantic-release bot :package::rocket: