Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Imports error with CommonJS + Typescript #5008

Closed
ivanpepelko opened this issue Oct 5, 2022 · 7 comments
Closed

Imports error with CommonJS + Typescript #5008

ivanpepelko opened this issue Oct 5, 2022 · 7 comments

Comments

@ivanpepelko
Copy link
Contributor

ivanpepelko commented Oct 5, 2022

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
@bartschriever
Copy link

I am experiencing the exact same problem.

@arthurfiorette
Copy link
Contributor

Probably related to #5000.

@VladimirMikulic
Copy link

Same issue as well.

@jasonsaayman
Copy link
Member

See #5030 merged should fix this.

@SharjeelSafdar
Copy link

I'm also facing the same problem.

@BrettHoutz
Copy link

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')

@SharjeelSafdar
Copy link

I solved this problem with this workaround:

import { AxiosStatic } from "axios";
const Axios = require("axios") as AxiosStatic;

squaresmile added a commit to atlasacademy/apps that referenced this issue Dec 22, 2022
- tsc esm output has incomplete imports (missing ".js"). This can be resolved by adding ".js" (not ".ts") to the ts files' imports but that feels icky so it's probably best to stick with cjs.
- axios@1 has broken exports axios/axios#5008 and feels overall unstable so it's probably best to stay at @0 for the foreseeable future.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants