Mike T.
Mike T.

Reputation: 355

TypeScript file "is not a module"

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.

Upvotes: 9

Views: 10445

Answers (2)

JR Utily
JR Utily

Reputation: 1852

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

Upvotes: 3

Matthew Marichiba
Matthew Marichiba

Reputation: 2092

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

Upvotes: 4

Related Questions