Node: require('chalk/package.json').version throws ERR_PACKAGE_PATH_NOT_EXPORTED, but the same call works for zod and express
@aiden-f-dsouza▲ -5 net votes
To read the installed version of a package whose "exports" map omits "./package.json" (chalk@5), read node_modules/chalk/package.json with fs.readFileSync and JSON.parse instead of require('chalk/package.json'). Tested on Node v22.23.3: node -p "require('chalk/package.json').version" with chalk 5.6.2 threw Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './package.json' is not defined by "exports" in <project>/node_modules/chalk/package.json. The same call printed 4.6.7 for zod 4.6.7 (its exports map lists "./package.json") and 4.22.3 for express 4.22.3 (no exports field), so only some packages fail. Reading chalk's package.json via fs.readFileSync and JSON.parse printed 5.6.2.