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.

Cannot consume axios typescript types using CommonJS

See original GitHub issue

Summary

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:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

12reactions
glenjamincommented, Jan 22, 2019

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.

diff --git a/node_modules/axios/index.d.ts b/typings/axios/index.d.ts
index 403fd1a..a9848a9 100644
--- a/node_modules/axios/index.d.ts
+++ b/typings/axios/index.d.ts
@@ -1,3 +1,5 @@
+declare namespace Axios {
+
 export interface AxiosTransformer {
   (data: any, headers?: any): any;
 }
@@ -126,6 +128,8 @@ export interface AxiosStatic extends AxiosInstance {
   spread<T, R>(callback: (...args: T[]) => R): (array: T[]) => R;
 }
 
-declare const Axios: AxiosStatic;
+}
+
+declare const Axios: Axios.AxiosStatic;
 
-export default Axios;
+export = Axios;

If this is desirable let me know and I’ll bundle it into a proper pull request.

3reactions
mohd-akramcommented, Jun 29, 2021

A workaround is to add the following to a declaration file, eg. declarations.d.ts:

declare module 'axios' {
  import axios from 'axios/index';
  export = axios;
}
Read more comments on GitHub >

github_iconTop 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 >

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