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.
$ 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
The
require()
function is not defined in ES module scope. Loading a JSON file viaimport
is behind--experimental-json-modules
on LTS versions of Node.js, so usecreateRequire()
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: