Cannot consume axios typescript types using CommonJS
See original GitHub issueSummary
There’s a chance I’m doing something wrong here, but this seemed to be the best place to post.
I’m using the following typescript config to typecheck my NodeJS code in .js
files without transforming it (and also to typecheck my browser code with .ts
and transforms).
{
"compilerOptions": {
"strict": true,
"jsx": "preserve",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"noUnusedLocals": true,
"target": "ES2017",
"module": "CommonJS",
"allowJs": true,
"checkJs": true,
"noEmit": true,
"baseUrl": ".",
"paths": {
"*": ["node_modules/*", "typings/*"]
}
}
}
Given the following code in app.js
const axios = require("axios");
const client = axios.create({
timeout: 1000
});
I get this type error:
server/app.js:10:22 - error TS2339: Property 'create' does not exist on type 'typeof import(".../node_modules/axios/index")'.
10 const client = axios.create({
~~~~~~
However with this code (which is wrong), I get no type errors
const axios = require("axios").default;
const client = axios.create({
baseURL: config.stravaBaseURL,
timeout: 1000
});
Context
- axios version: 0.18.0
- Environment: Node 10.13.0, Typescript 3.2.4
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:8 (1 by maintainers)
Top Results From Across the Web
I'm not able to use Axios in TypeScript - Stack Overflow
I'm trying to use Axios to get some data from a fake json api but the problem is that my IDE isn't able...
Read more >CommonJS modules | Node.js v19.3.0 Documentation
json file contains a top-level field "type" with a value of "module" , those files will be recognized as CommonJS modules only if...
Read more >TypeScript: JavaScript With Syntax For Types.
TypeScript is JavaScript with syntax for types. TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at ......
Read more >A Complete Guide to Using TypeScript in Node.js - Better Stack
An example is axios whose types are included in the main repository so that it is downloaded alongside its NPM package and automatically...
Read more >axios - npm
TypeScript icon, indicating that this package has built-in type ... while using CommonJS imports with require() , use the following approach ...
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 FreeTop 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
Top GitHub Comments
Based on https://github.com/Microsoft/TypeScript/issues/2719 the solution appears to be to modify the typescript definition using
export =
syntax so that typescript knows it’s really a commonjs module.If this is desirable let me know and I’ll bundle it into a proper pull request.
A workaround is to add the following to a declaration file, eg.
declarations.d.ts
: