Skip to content

Commit 17ab6fe

Browse files
committed
Fix "require is not defined" caused by dist/package.json type:module
ncc injects {"type":"module"} into dist/package.json when the source project is ESM, but the bundle itself is CJS format. Node then tries to run it as ESM and fails. Add a postbuild hook that overwrites dist/package.json with {} to establish a CJS package boundary in dist/ without walking up to the root package.json.
1 parent 8d0e47c commit 17ab6fe

2 files changed

Lines changed: 2 additions & 4 deletions

File tree

dist/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
{
2-
"type": "module"
3-
}
1+
{}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"scripts": {
99
"lint": "eslint --fix *.js",
1010
"build": "ncc build index.js -o dist",
11-
"build-legacy": "NODE_OPTIONS=--openssl-legacy-provider ncc build index.js -o dist",
11+
"postbuild": "node --input-type=commonjs -e \"require('fs').writeFileSync('dist/package.json', '{}')\"",
1212
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js src --config=jest.config.cjs",
1313
"test-debug": "node --experimental-vm-modules --inspect-brk node_modules/jest/bin/jest.js --runInBand src --config=jest.config.cjs",
1414
"deploy-wip": "rimraf dist/index.js && npm run build && git ac wip && git push",

0 commit comments

Comments
 (0)