Sentry-javascript: [@sentry/node] "Cannot find name" TypeScript build errors

Created on 14 Sep 2018  Â·  7Comments  Â·  Source: getsentry/sentry-javascript

Package + Version

  • [ ] @sentry/browser
  • [X] @sentry/node
  • [ ] raven-js
  • [ ] raven-node _(raven for node)_
  • [ ] other:

Version:

4.0.0-rc.2

Description

Perhaps I've not correctly configured my TypeScript environment or something, but I cannot seem to successfully run tsc --build with @sentry/node, getting the following errors:

node_modules/@sentry/node/dist/handlers.d.ts:2:49 - error TS2304: Cannot find name 'Request'.

2 export declare function requestHandler(): (req: Request, res: Response, next: () => void) => void;
                                                  ~~~~~~~

node_modules/@sentry/node/dist/handlers.d.ts:2:63 - error TS2304: Cannot find name 'Response'.

2 export declare function requestHandler(): (req: Request, res: Response, next: () => void) => void;
                                                                ~~~~~~~~

node_modules/@sentry/node/dist/handlers.d.ts:13:71 - error TS2304: Cannot find name 'Request'.

13 export declare function errorHandler(): (error: MiddlewareError, req: Request, res: Response, next: (error: MiddlewareError) => void) => void;
                                                                         ~~~~~~~

node_modules/@sentry/node/dist/handlers.d.ts:13:85 - error TS2304: Cannot find name 'Response'.

13 export declare function errorHandler(): (error: MiddlewareError, req: Request, res: Response, next: (error: MiddlewareError) => void) => void;
                                                                                       ~~~~~~~~

node_modules/@sentry/node/dist/transports/base.d.ts:8:53 - error TS2304: Cannot find name 'URL'.

8     request(options: http.RequestOptions | string | URL, callback?: (res: http.IncomingMessage) => void): http.ClientRequest;
                                                      ~~~

In case it is my TypeScript configuration, the contents of tsconfig.json is:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es2017",
        "lib": [
            "es2017"
        ],
        "moduleResolution": "node",
        "rootDir": "./",
        "sourceMap": true,
        "allowJs": true,
        "noImplicitAny": true,
        "noUnusedLocals": true,
        "noImplicitThis": true,
        "strictNullChecks": true,
        "noImplicitReturns": true,
        "preserveConstEnums": true,
        "suppressImplicitAnyIndexErrors": true,
        "forceConsistentCasingInFileNames": true,
        "strict": true
    },
    "exclude": [
        "node_modules",
        "build",
        "webpack"
    ],
    "types": [
        "typePatches"
    ]
}

And a simple use case:

import { init } from "@sentry/node";

init({
    dsn: "..."
});

Most helpful comment

@Aquilosion you have to add node typings to your project.

npm install @types/node

....

"compilerOptions": {
    "types": [
      "node"
    ],
}

Sorry for the inconvinience, it'll be included in the docs. Cheers!

All 7 comments

@Aquilosion you have to add node typings to your project.

npm install @types/node

....

"compilerOptions": {
    "types": [
      "node"
    ],
}

Sorry for the inconvinience, it'll be included in the docs. Cheers!

@kamilogorek Thanks, though I'm not sure that's it… I already had @types/node as a dev dependency (as I was using it for things like process.env). I added the types key as parameter directly to compilerOptions, but I'm still getting the same errors.

My tsconfig now looks like:

{
    "compilerOptions": {
        "types": [
            "node"
        ],
        "module": "commonjs",
        "target": "es2017",
        "lib": [
            "es2017"
        ],
        "moduleResolution": "node",
        "rootDir": "./",
        "sourceMap": true,
        "allowJs": true,
        "noImplicitAny": true,
        "noUnusedLocals": true,
        "noImplicitThis": true,
        "strictNullChecks": true,
        "noImplicitReturns": true,
        "preserveConstEnums": true,
        "suppressImplicitAnyIndexErrors": true,
        "forceConsistentCasingInFileNames": true,
        "strict": true
    },
    "exclude": [
        "node_modules",
        "build",
        "webpack"
    ]
}

And for completeness, the dependencies in my package.json:

    "dependencies": {
        "@sentry/node": "^4.0.0-rc.2"
    },
    "devDependencies": {
        "@types/node": "^10.9.4"
    }

@Aquilosion I'll double-check it today and get back to you.

Thanks! I do have a basic test project if you're unable to reproduce: https://github.com/Aquilosion/TestSentryTypeScript

@Aquilosion

{ 
  "compilerOptions": {
    "lib": ["dom"]
  }
}

dom typings contain those APIs, forgot about that. Tested with your project, it works just fine. We'll document this :)

Perfect, thanks @kamilogorek!

@kamilogorek we shouldn't have to add the dom typings for node application, this should be fixed.

Was this page helpful?
0 / 5 - 0 ratings