Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI - ENOENT: no such file or directory, open 'package.json' #1976

Closed
yoursunny opened this issue May 28, 2024 · 3 comments · Fixed by #1979
Closed

CLI - ENOENT: no such file or directory, open 'package.json' #1976

yoursunny opened this issue May 28, 2024 · 3 comments · Fixed by #1979
Assignees
Labels

Comments

@yoursunny
Copy link

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": "pnpm@9.1.3+sha512.7c2ea089e1a6af306409c4fc8c4f0897bdac32b772016196c469d9428f1fe2d5a21daf8ad6512762654ac645b5d9136bb210ec9a00afa8dbc4677843ba362ecd",
  "dependencies": {
    "ts-json-schema-generator": "2.2.0"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "allowImportingTsExtensions": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "importHelpers": true,
    "module": "es2022",
    "moduleResolution": "Node",
    "noEmit": true,
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noUncheckedIndexedAccess": true,
    "noUnusedLocals": true,
    "resolveJsonModule": true,
    "strict": true,
    "target": "ES2022"
  },
  "include": [
    "def/*.ts"
  ]
}

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/ts-json-schema-generator@2.2.0/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:

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.
@arthurfiorette
Copy link
Collaborator

#1975 might solved this issue already, could you try with v2.3.0-next.2?

@arthurfiorette arthurfiorette self-assigned this May 29, 2024
@yoursunny
Copy link
Author

I already tried v2.3.0-next.2 and it also suffers from this issue.

@arthurfiorette
Copy link
Collaborator

v2.3.0-next.3 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants