skate_23
skate_23

Reputation: 487

SyntaxError: Cannot use import statement outside a module Typescript Error

I have multiple .ts files and I am getting some typescript errors in them regarding importing some modules/scripts.

Consider the following example:


Sample1.ts
--------

const axios = require('axios');

function sample1() {
...
...

}

Sample2.ts
--------

const axios = require('axios');

function sample2() {
...
...

}


These two .ts files are in one folder. I get an error in file sample2.ts saying

Cannot redeclare block-scoped variable 'axios'.ts(2451)

To resolve this error I changed this line const axios = require('axios'); to import axios from 'axios'; to an import statement. But for the import statement, I get the error SyntaxError: Cannot use import statement outside a module

My module and target in tsconfig file is esnext and es6 respectively.

Please can someone help me with this problem. I ma trying to remove these errors for a very long time.

Upvotes: 0

Views: 238

Answers (1)

mk23
mk23

Reputation: 1403

In the package.json file, add the top-level "type" field with a value of "module".

// package.json
{
  "type": "module"
}

Upvotes: 0

Related Questions