Skip to main content

There can be n numbera lot of of possibilities for getting, "SyntaxError: Cannot use import statement outside a module "

SyntaxError: Cannot use import statement outside a module

while running typescript.

In my setup, I was importing a module called, AbModule AbModule (AbModuleAbModule has class AbClassNameAbClassName) from my script testabtestab.ts.ts

testab.tstestab.ts had import stmt, import {AbClassName} = 'testAdapterDir/AbModule.po.ts';import stmt,

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

However, AbModule hadAbModule had all *.ts*.ts files and there were *.js*.js files were not present. fix is, cd AbModule path tsc *.ts (you may get errors but ignore them)

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

cd AbModule path
tsc *.ts

now AbModuleNow AbModule should comtaincontain compiled files all *.js*.js too. Now run testab.tstestab.ts and noticed that SyntaxError: Cannot use import statement outside a module"the mentioned error does not existsexist anymore,

There can be n number 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, cd AbModule path tsc *.ts (you may get errors but ignore them)

now AbModule should comtain compiled files all *.js too. Now run testab.ts and noticed that SyntaxError: Cannot use import statement outside a module" error does not exists anymore,

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,

Source Link

There can be n number 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, cd AbModule path tsc *.ts (you may get errors but ignore them)

now AbModule should comtain compiled files all *.js too. Now run testab.ts and noticed that SyntaxError: Cannot use import statement outside a module" error does not exists anymore,