meilisearch-js: TS2304: Cannot find name 'Request'

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

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 2
  • Comments: 17 (10 by maintainers)

Commits related to this issue

Most upvoted comments

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?

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!

Hey @Ionaru The problem you have is one of the reasons we want to go for named export. Default exports as we have today makes it really ugly to use in node js.

export default MeiliSearch

This is commonly known as the Common js interop with ES modules. What happens is that when the default export is transpiled into Common js (node js) the default export is not transpiled like this

module.exports = MeiliSearch

But like this:

module.exports.default = MeiliSearch

To make your typescript build compatible with nodeJs you need to add the following parameter in your tsconfig file:

{
   "esModuleInterop": true
}

This will automatically change your code like this:

// dist/index.js
const x = new meilisearch_1.default({
    apiKey: '123',
    host: 'http://some.url:7700',
});

You can see now that typescript add default after MeiliSearch because it asks for the default key.

You could have similar issues with other default export libraries in a node typescript environment if you do not add this configuration. So I don’t think this is linked to a problem inside MeiliSearch.

Either way, this should not be an issue anymore if we go for a named exports. If you’d like to you can give your opinion on this here: https://github.com/meilisearch/instant-meilisearch/issues/122

more details about this problem: https://stackoverflow.com/questions/56238356/understanding-esmoduleinterop-in-tsconfig-file

I’ve put possible solutions in the original post.

@bidoubiwa I think your diagnostic is right, the reason why the project is not compiling is because the dom lib is not present in the tsconfig.json. The fact that skipLibCheck is not used, the Typescript compiler won’t only check the code used against Meilisearch types but the entire library as well as the transitive dependencies. skipLibCheck is not necessarily bad if you’re using several libraries and they are not compatible together, sometimes you don’t even have the choice. In this is case I would go with the fix you suggested: adding dom in the lib config of the tsconfig.json.