-
Notifications
You must be signed in to change notification settings - Fork 214
Closed
Labels
Description
Since v2.2.0, the CLI command does not work if it's invoked in a directory without a package.json
file.
Snippet to Reproduce
package.json
{
"private": true,
"packageManager": "[email protected]+sha512.7c2ea089e1a6af306409c4fc8c4f0897bdac32b772016196c469d9428f1fe2d5a21daf8ad6512762654ac645b5d9136bb210ec9a00afa8dbc4677843ba362ecd",
"dependencies": {
"ts-json-schema-generator": "2.2.0"
}
}
tsconfig.json
def/waypoint.ts
export interface Waypoint {
lat: number;
lng: number;
}
Error Message
ubuntu@N0:~/tjsg$ cd def
ubuntu@N0:~/tjsg/def$ $(corepack pnpm bin)/ts-json-schema-generator --path waypoint.ts --type Waypoint --out waypoint.schema.json
node:fs:448
return binding.readFileUtf8(path, stringToFlags(options.flag));
^
Error: ENOENT: no such file or directory, open 'package.json'
at Object.readFileSync (node:fs:448:20)
at file:///home/ubuntu/tjsg/node_modules/.pnpm/[email protected]/node_modules/ts-json-schema-generator/dist/ts-json-schema-generator.js:9:27
at ModuleJob.run (node:internal/modules/esm/module_job:222:25)
at async ModuleLoader.import (node:internal/modules/esm/loader:316:24) {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: 'package.json'
}
Node.js v20.13.1
Cause Analysis
The offending line is:
const pkg = JSON.parse(fs.readFileSync("package.json", "utf8")); |
This was introduced in #1964.
It reads the content of package.json
into pkg
variable, which is then used to display the version number of ts-json-schema-generator package:
.version(pkg.version) |
For this purpose, the command should be reading path.join(import.meta.dirname, "package.json")
rather than package.json
in current working directory, because:
- If current working directory does not have
package.json
file, it leads to ENOENT error. - If current working directory has
package.json
file, the command would display the version number of the downstream package, instead of the version number of ts-json-schema-generator package.