Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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   } } 
like image 344
Zerumi Avatar asked Oct 07 '19 16:10

Zerumi


People also ask

Can't use import statement outside a module TypeScript?

To solve the error "Cannot use import statement outside a module" in TypeScript, set the module option to commonjs in your tsconfig. json file and make sure to compile your TypeScript files (e.g. with ts-node ), and not to run them directly with node .

How do you fix Cannot use import statement outside a module error?

The "SyntaxError: Cannot use import statement outside a module" occurs when we use the ES6 Modules syntax in a script that was not loaded as a module. To solve the error, set the type attribute to module when loading a script, or in your package. json for Node.

Can not use import statement outside a module?

I stumbled on this error: Uncaught SyntaxError: cannot use import statement outside a module while importing a function from a JavaScript file. This error occurs for one reason: you're trying to use import and you're not inside an ES module. It can happen in a Node. js environment, or in the browser.


1 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.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).

like image 68
Zwiers Avatar answered Oct 04 '22 02:10

Zwiers