TS2304: Cannot find name 'Request'
See original GitHub issueSummary
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-fetchto do something with the issue and then use theRequesttype fromcross-fetch. - Switch out
cross-fetchfor a module that does not require the DOM library to be enabled, and use itsRequesttype. (axios?). - Define your own
Requesttype manually.
Workarounds
- Add
skipLibCheckto the tsconfig.- Undesirable because this will disable all type checking against library declaration files.
- Add
DOMtolibin tsconfig.- Undesirable because this adds and overwrites globals which are not available in Node.js environments.
References
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:17 (10 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Hey @Ionaru
A quick fix is to add
domin yourliboptions insidetsconfig.json.I would like a better solution to this problem as I think meilisearch-js should ship the
Requesttype. What is weird to me is thatcross-fetchhas a type file at its root.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!