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

build fails in "npm i" - tsc errors: 'err' is of type 'unknown'. #245

Open
DavidBiesack opened this issue Oct 18, 2024 · 2 comments
Open

Comments

@DavidBiesack
Copy link

I cloned this repo and tried to build locally (to try to debug/fix an issue I reported).

The npm i command failed in tsc

$ npm i

> [email protected] prepublish
> npm run build


> [email protected] build
> rimraf dist && tsc

src/commands/ajv.ts:89:11 - error TS18046: 'err' is of type 'unknown'.

89       if (err.code === "MODULE_NOT_FOUND") {
             ~~~

src/commands/ajv.ts:91:110 - error TS18046: 'err' is of type 'unknown'.

91           `'ts-node' is required for the TypeScript configuration files. Make sure it is installed\nError: ${err.message}`
                                                                                                                ~~~

src/commands/util.ts:55:25 - error TS18046: 'err' is of type 'unknown'.

55     const msg: string = err.message
                           ~~~

src/commands/util.ts:85:29 - error TS18046: 'err' is of type 'unknown'.

85     console.error(`error: ${err.message}`)
                               ~~~


Found 4 errors in 2 files.

Errors  Files
     2  src/commands/ajv.ts:89
     2  src/commands/util.ts:55
npm ERR! code 2
npm ERR! path ~/dev/tools/ajv-cli
npm ERR! command failed
npm ERR! command sh -c npm run build

Opening the project in VSCode, I also see (other) errors such as

Cannot find module 'json5'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option? ts(2792)

What version of Node.js is required to build? I tried Node 18 and 20

Please add a CONTRIBUTING file or build instructions to the README since this project does not build "out of the box".

(i.e. should I increment the package.json version when submitting a PR, or do maintainers do that when making a release, etc.)

@DavidBiesack
Copy link
Author

I can wrap some of those err.message references with

  if (err instanceof Error) { ... }

but that does not address the reference to err.code === "MODULE_NOT_FOUND" in util.js .
Apparently, .code is defined in SystemError which Node does not export (see nodejs/node#46869 ); that takes more code to get past tsc compile warnings.
The following works, but it's not as elegant as an instanceof SystemError check:

    try {
      registerer = require("ts-node").register()
    } catch (err) {
      if (err instanceof Error) {
        if ((err as NodeJS.ErrnoException).code === "MODULE_NOT_FOUND") {
          throw new Error(
            `'ts-node' is required for the TypeScript configuration files. Make sure it is installed\nError: ${err.message}`
          )
        }
      }
      throw err
    }

With those code changes, the package compiles with tsc (via npm run build).

@DavidBiesack
Copy link
Author

PR #242 addresses this in a different way (use (err: any) ).
I'll let maintainers decide whether this repo allows additional use of any type (there are some other places where any is used)

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

No branches or pull requests

1 participant