Typescript: Cannot use import statement outside a module

I have a .ts file in node js (latest version of node.js for 07.10.19) app with importing node-module without default export. I use this construction: import { Class } from 'abc'; When i run the code, i have this error: Cannot use import statement outside a module.

In the network i see many solutions for this problem (for .js), but it not helps to me, maybe because i have typescript file. Here's my code:

import { Class } from 'abc';
module.exports = { ...
    execute(a : Class ,args : Array<string>){ ...

Here's my tsconfig.json:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",

    "strict": true
  }
}

Adding "type": "module" to package.json will tell Node you are using ES2015 modules, which should get rid of the error, but then you will need to tell Typescript to generate this type of module by setting "module": "es2015" instead of "commonjs" in tsconfig.json.

This however causes a problem with the current code because although you are using an ES6 import {} statement you are exporting using the commonJS module.exports = {} syntax, and Node’s ES module loader will have an issue with it. There are two ways to deal with it:

  1. Keep the module.exports but tell Node to interpret this file as commonJS by giving it a .cjs extension.
  2. Change the export statement to ES2015 syntax: export function execute(…)..

The first option could get a bit tricky because the compiler will output .js files and you’d have to change it to .cjs all the time (as far as I know). With the second option you should be able to run the file with Node (including the --experimental-modules flag for versions < 13.8).

If you absolutely need to use commonJS, perhaps it is better to install the type definitions for Node: @types/node and change the import to commonJS format: require('abc') and keep the rest of the settings as they are (though you can add "type": "commonjs" to package.json to be explicit).

SyntaxError: Cannot use import statement outside a module, with , SyntaxError: Cannot use import statement outside a module, with TypeScript and ES Modules #9860. Open. dandv opened this issue on Apr 22 � 12 comments. SyntaxError: Cannot use import statement outside a module. I have read some similar Stack Overflow questions and GitHub issues but I didn't find a solution for my own application. Finally I found Mocha documentation on GitHub regarding ES modules but it didn't work:


install package ts-node and change your package.json

"scripts": {
    "dev": "ts-node index.ts"
  }

Typescript: Cannot use import statement outside a module, 3 Answers. Adding “type”: “module” to package. json will tell Node you are using ES2015 modules, which should get rid of the error, but then you will need to tell Typescript to generate this type of module by setting “module”: “es2015” instead of “commonjs” in tsconfig. SyntaxError: Cannot use import statement outside a module in my js file [duplicate]


There can be a lot of of possibilities for getting,

SyntaxError: Cannot use import statement outside a module

while running typescript.

In my setup, I was importing a module called, AbModule (AbModule has class AbClassName) from my script testab.ts.

testab.ts had import stmt,

import {AbClassName} = 'testAdapterDir/AbModule.po.ts';

However, AbModule had all *.ts files and there were *.js files were not present. fix is,

You may get errors in running the below code but you can safely ignore them.

cd AbModule path
tsc *.ts

Now AbModule should contain compiled files all *.js too. Now run testab.ts and noticed that the mentioned error does not exist anymore,

SyntaxError: Cannot use import statement outside a module, add this line into your package.json. “SyntaxError: Cannot use import statement outside a module” is published by Fredric Cliver. /// -ing a module # A common mistake is to try to use the /// syntax to refer to a module file, rather than using an import statement. To use the File System module in TypeScript, we first need to import it. You can nest them one inside another.


Handbook - Modules, I was trying to load some JS files and ran into this strange "Cannot use import statement outside a module" error and was unable to move on� js means transpiling the TypeScript code to JavaScript in a form that is useful on Node. The import statement cannot be used in embedded scripts unless such script has a type="module". The first argument of the writeFile function is the path of the file and the second one is the content of it.


Fix Cannot use import statement outside a module Issue , “SyntaxError: Cannot use import statement outside a module” Code import outside of module � typescript Uncaught SyntaxError: Cannot use� Cannot Use Import Statement Outside A Module Node Typescript json for your code to work. It will come with freebies like parsing and route organization. easiest way such file. /// -ing a module # A common mistake is to try to use the /// syntax to refer to a module file, rather than using an import statement.


SyntaxError: Cannot use import statement outside a module Code , I am starting to work with vue.js and have been trying to set up the app js file. When I do npm run dev and load a page, I get this error Cannot use i Webdriverio can not use import statement outside a module Sagar S. With most things Angular, there is a lot of magic happening when it comes to dependency injection (DI). If you're using TypeScript, you can configure the baseUrl setting inside the compilerOptions of your project's tsconfig. When I try to import react from 'react', nothing import.