Skip to content

Imports error with CommonJS + Typescript #5008

Closed

Description

@ivanpepelko

Describe the bug

When using commonjs + typescript (in nest.js project, also reproduced standalone), imports are not consistent:

  • default import: tsc passes, fails at runtime when trying to access axios.default
  • star import: typescript check does not pass, produces working javascript
  • require: workaround for imports

To Reproduce

See https://github.com/ivanpepelko/axios-import-require-issue
Attached:

  • typescript sources (import-default.ts, import-star.ts, require.ts)
  • resulting javascript files
  • tsconfig
  • package.json

Expected behavior

axios.default to be exported as per documentation.

Environment

  • Axios Version 1.0.0
  • Adapter: default for node16
  • Node.js Version 16
  • OS: Linux 5.19.12-arch1-1 x86_64
  • Additional Library Versions: within nest.js framework and standalone as in attached repo

Activity

  1. bartschriever commented on Oct 5, 2022

    @bartschriever

    I am experiencing the exact same problem.

  2. arthurfiorette commented on Oct 5, 2022

    @arthurfiorette
    Contributor

    Probably related to #5000.

  3. VladimirMikulic commented on Oct 5, 2022

    @VladimirMikulic

    Same issue as well.

  4. jasonsaayman commented on Oct 6, 2022

    @jasonsaayman
    Member

    See #5030 merged should fix this.

  5. SharjeelSafdar commented on Oct 26, 2022

    @SharjeelSafdar

    I'm also facing the same problem.

  6. BrettHoutz commented on Oct 27, 2022

    @BrettHoutz

    I still have this issue with axios v1.1.3.

    TS import:

    import axios from 'axios';

    JS code transpiled by tsc:

    const axios_1 = require('axios');
    // ...
    const { data } = await axios_1.default.get('http://example.com');

    This throws the exception:

    TypeError: Cannot read properties of undefined (reading 'get')
    
  7. SharjeelSafdar commented on Oct 28, 2022

    @SharjeelSafdar

    I solved this problem with this workaround:

    import { AxiosStatic } from "axios";
    const Axios = require("axios") as AxiosStatic;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions