question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TS2304: Cannot find name 'Request'

See original GitHub issue

Summary When compiling a Typescript file that uses MeiliSearch for Node.js, the error TS2304: Cannot find name 'Request' causes the build to fail. This happens because this library uses the Request type from the DOM library, which is not available on Node.js environments.

Details Full log:

node_modules/meilisearch/dist/types/http-requests.d.ts:14:18 - error TS2304: Cannot find name 'Request'.
14         config?: Request;
                    ~~~~~~~

node_modules/meilisearch/dist/types/http-requests.d.ts:18:17 - error TS2304: Cannot find name 'Request'.
18     }, config?: Request): Promise<void>;
                   ~~~~~~~

node_modules/meilisearch/dist/types/http-requests.d.ts:21:17 - error TS2304: Cannot find name 'Request'.
21     }, config?: Request): Promise<T>;
                   ~~~~~~~

node_modules/meilisearch/dist/types/http-requests.d.ts:24:17 - error TS2304: Cannot find name 'Request'.
24     }, config?: Request): Promise<Types.IndexResponse>;
                   ~~~~~~~

node_modules/meilisearch/dist/types/http-requests.d.ts:27:17 - error TS2304: Cannot find name 'Request'.
27     }, config?: Request): Promise<R>;
                   ~~~~~~~

node_modules/meilisearch/dist/types/http-requests.d.ts:30:17 - error TS2304: Cannot find name 'Request'.
30     }, config?: Request): Promise<Types.IndexResponse>;
                   ~~~~~~~

node_modules/meilisearch/dist/types/http-requests.d.ts:33:17 - error TS2304: Cannot find name 'Request'.
33     }, config?: Request): Promise<R>;
                   ~~~~~~~

node_modules/meilisearch/dist/types/http-requests.d.ts:36:17 - error TS2304: Cannot find name 'Request'.
36     }, config?: Request): Promise<void>;
                   ~~~~~~~

node_modules/meilisearch/dist/types/http-requests.d.ts:39:17 - error TS2304: Cannot find name 'Request'.
39     }, config?: Request): Promise<T>;
                   ~~~~~~~

Found 9 errors.

My tsconfig:

{
    "compilerOptions": {
        "baseUrl": "",
        "strict": true,
        "lib": [
            "esnext"
        ],
        "module": "commonjs",
        "moduleResolution": "node",
        "outDir": "./dist",
        "target": "es2020",
        "typeRoots": [
            "node_modules/@types"
        ]
    },
    "files": [
        "index.ts"
    ]
}

Index.ts:

import MeiliSearch from 'meilisearch';

new MeiliSearch({
    apiKey: '123',
    host: 'http://some.url:7700',
});

Possible solutions

  • Wait for cross-fetch to do something with the issue and then use the Request type from cross-fetch.
  • Switch out cross-fetch for a module that does not require the DOM library to be enabled, and use its Request type. (axios?).
  • Define your own Request type manually.

Workarounds

  • Add skipLibCheck to the tsconfig.
    • Undesirable because this will disable all type checking against library declaration files.
  • Add DOM to lib in tsconfig.
    • Undesirable because this adds and overwrites globals which are not available in Node.js environments.

References

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:17 (10 by maintainers)

github_iconTop GitHub Comments

6reactions
bidoubiwacommented, Oct 27, 2020

Hey @Ionaru

A quick fix is to add dom in your lib options inside tsconfig.json.

{
   "lib": [
       "esnext",
       "dom"
   ],
}

I would like a better solution to this problem as I think meilisearch-js should ship the Request type. What is weird to me is that cross-fetch has a type file at its root.

/// <reference lib="dom" />

declare const _fetch: typeof fetch;
declare const _Request: typeof Request;
declare const _Response: typeof Response;
declare const _Headers: typeof Headers;

declare module "cross-fetch" {
  export const fetch: typeof _fetch;
  export const Request: typeof _Request;
  export const Response: typeof _Response;
  export const Headers: typeof _Headers;
  export default fetch;
}

I’m not sure why this /// <reference lib="dom" /> is added.

@emyann do you have an idea of why that problem occurs?

3reactions
roberttcommented, Jun 7, 2021

Hey @bidoubiwa - I figured out. It was because of a weird regression in how yarn resolves dependences in monorepos, resulting in this error. After I fixed the resolution issue, it worked perfectly. I meant to come back remove/edit my comment - so sorry! This can be closed.

On a side note, meilisearch has been a blessing - so easy to deploy & use. Great job!

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript getting error TS2304: cannot find name ' require'
I am getting the error "TS2304: Cannot find name 'require' " when I attempt to transpile a simple TypeScript Node.js page. I have...
Read more >
TypeScript error TS2304 cannot find name require - Edureka
The error that I'm getting is the "TS2304: Cannot find name 'require' " when I attempt to transpile a simple TypeScript Node.js page....
Read more >
Typescript getting error TS2304 cannot find name ' require'
When we try to get a TypeScript running, we get this error: "TS2304: Cannot find name 'require' " when we attempt to transpile...
Read more >
Fix the Cannot Find Name 'require' Error in TypeScript
To fix the “cannot find name 'require'” error in TypeScript, install the @types/node package into your project by running npm i -D @types/node...
Read more >
"TS2304:Cannot find name 'module'" after update ... - YouTrack
"TS2304:Cannot find name 'module'" after update to WebStorm 2016.3 ... What steps will reproduce the problem? ... @Component({ moduleId: module.id }) export class ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found