TypeScript is compiling into this failing JS file when importing a module. Why?

user1869935

I have a pretty basic TS file that looks like this

import axios from 'axios';

const url = 'https://jsonplaceholder.typicode.com/todos/1';

axios.get(url).then(res => {
  console.log(res.data);
});

That compiles into this (added some linebreaks for clarity)

"use strict";

exports.__esModule = true;

var axios_1 = require("axios");
var url = 'https://jsonplaceholder.typicode.com/todos/1';

axios_1["default"].get(url).then(function (res) {
    console.log(res.data);
});

When I try running it with node I get this error

TypeError: Cannot read properties of undefined (reading 'get')
    at Object.<anonymous> (/Users/john/programation/javascript/typescript_2/fetchjson/index.js:5:20)

True enough when I console.log(axios_1) it doesn't have any "default" key.

I thought the TS compiler would handle this basic stuff. What can I do?

Dimava

Looks like you have an old CommonJs-module version of 'axios', which has no esModuleInterop flag

Update your axios and try again


Or you may import it like

import * as axios from 'axios'

(axios page says default export is the same as the whole module)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Importing a JS module in a Typescript file

From Dev

Error "Cannot find module" when importing a remote JSON file in TypeScript

From Dev

Typescript: Ignore implicitly any type when importing js module

From Dev

Why is this Typescript module importing and then reexporting another module?

From Dev

Importing a typescript module to a JS module in a React app

From Dev

typescript error when compiling node module

From Dev

Webpack Module not found when importing Typescript

From Dev

Why ModuleNotFound when importing a module in a same directory?

From Dev

Make file error when compiling kernel module

From Dev

Importing module in JS script inside HTML file

From Dev

Compiling Stylus with Webpack 4 without importing it into a JS file?

From Dev

Typescript: Empty output file when compiling

From Javascript

Typescript compiler error when importing json file

From Dev

jest with typescript throws an error when importing a file

From Dev

TypeScript: Imported Module Not Compiling

From Dev

Why do I need to add the .js extension when importing files in typescript but not in Angular imports?

From Dev

TypeScript Module Importing & WebPack

From Dev

Is there a way to avoid adding (///<reference path="..."/>) when compiling to single '*.js' file with TypeScript 1.8 compiler

From Dev

Failed to load module script when importing a .jpg file in a javascript file

From Dev

Why do script tags need to be left empty when importing a JS file?

From Dev

Why is importing module not working when running the code for the second time

From Dev

Why importing the Vue.js source works, but not the module?

From Dev

Error when importing a module

From Dev

vscode not compiling typescript file

From

Importing json file in TypeScript

From Dev

Importing a JSON file with Typescript

From Python

(Solved) Docstring not showing up when importing .py file as a module?

From

Flow “Required module not found” when importing a CSS file

From Dev

"Cannot find module" when importing .csv file in Create React App

Related Related

  1. 1

    Importing a JS module in a Typescript file

  2. 2

    Error "Cannot find module" when importing a remote JSON file in TypeScript

  3. 3

    Typescript: Ignore implicitly any type when importing js module

  4. 4

    Why is this Typescript module importing and then reexporting another module?

  5. 5

    Importing a typescript module to a JS module in a React app

  6. 6

    typescript error when compiling node module

  7. 7

    Webpack Module not found when importing Typescript

  8. 8

    Why ModuleNotFound when importing a module in a same directory?

  9. 9

    Make file error when compiling kernel module

  10. 10

    Importing module in JS script inside HTML file

  11. 11

    Compiling Stylus with Webpack 4 without importing it into a JS file?

  12. 12

    Typescript: Empty output file when compiling

  13. 13

    Typescript compiler error when importing json file

  14. 14

    jest with typescript throws an error when importing a file

  15. 15

    TypeScript: Imported Module Not Compiling

  16. 16

    Why do I need to add the .js extension when importing files in typescript but not in Angular imports?

  17. 17

    TypeScript Module Importing & WebPack

  18. 18

    Is there a way to avoid adding (///<reference path="..."/>) when compiling to single '*.js' file with TypeScript 1.8 compiler

  19. 19

    Failed to load module script when importing a .jpg file in a javascript file

  20. 20

    Why do script tags need to be left empty when importing a JS file?

  21. 21

    Why is importing module not working when running the code for the second time

  22. 22

    Why importing the Vue.js source works, but not the module?

  23. 23

    Error when importing a module

  24. 24

    vscode not compiling typescript file

  25. 25

    Importing json file in TypeScript

  26. 26

    Importing a JSON file with Typescript

  27. 27

    (Solved) Docstring not showing up when importing .py file as a module?

  28. 28

    Flow “Required module not found” when importing a CSS file

  29. 29

    "Cannot find module" when importing .csv file in Create React App

HotTag

Archive