Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript file "is not a module"

Tags:

I have a really basic Typescript file being imported into a Register.tsx file. I keep getting the following error when building with npm.

test.ts' is not a module

test.ts

export class Tester {
    test(): string {
        return "test";
    }
}

Register.tsx

import { Tester } from "./../test"

I've read several different solutions but I can't see anything I'm doing wrong or different. Am I missing something glaringly obvious? I'm new to Typescript / npm / Webpack.

like image 325
Mike T. Avatar asked Jul 27 '18 16:07

Mike T.


2 Answers

Does test.ts export anything else or access the module object? My experience is that TypeScript throws the 'not a module' warning whenever if it sees the NodeJS module object, which NodeJS uses for exports instead of the TS export notation.

My team has an environment in which we include plain JavaScript files into certain TypeScript projects, and we get the same 'not a module' warning whenever importing anything with lines like: module.exports = X or module.exports.thing = X (which happens to be all of the plain-Javascript files.)

like image 170
Matthew Marichiba Avatar answered Sep 28 '22 19:09

Matthew Marichiba


When this happened to me, restarting my yarn dev did solved the issue. It seems tsc is not restarted completely when watch reload, and that it does not discover new files properly...

like image 21
JR Utily Avatar answered Sep 28 '22 17:09

JR Utily