Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript exports is not defined

I'm trying to use export and import but it not working I get an error

Here is my code HTML :

<!DOCTYPE html> <html> <head>     <meta charset="utf-8" />     <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body>     @RenderBody()      <script src="~/scripts/user.js"></script>     <script src="~/scripts/main.js"></script> </body> </html> 

User.ts :

export class User {     firstName: string;     lastName: string; } 

main.ts

import { User } from "./user"; 

Here is also screenshot of exception :

enter image description here

like image 449
Iris Avatar asked Apr 18 '17 12:04

Iris


People also ask

How do I export TypeScript?

Use a named export to export a type in TypeScript, e.g. export type Person = {} . The exported type can be imported by using a named import as import {Person} from './another-file' . You can have as many named exports as necessary in a single file.

How do you fix require is not defined?

To solve the "ReferenceError require is not defined" error, remove the type property if it's set to module in your package. json file and rename any files that have a . mjs extension to have a . js extension.

What is export declare TypeScript?

In TypeScript, just as in ECMAScript 2015, any file containing a top-level import or export is considered a module. Conversely, a file without any top-level import or export declarations is treated as a script whose contents are available in the global scope (and therefore to modules as well).

What is default export in TypeScript?

It's just JavaScript Modules and their associated keyword like import , export , and export default are JavaScript constructs, not typescript. However typescript added the exporting and importing of interfaces and type aliases to it.


1 Answers

You have a couple of options:

Option 1: Use a module loader like Webpack, Browserify, etc.

Option 2: If you just want to compile *.ts to *.js without any module imports or exports, set compilerOptions.module to "none" in your tsconfig.json. Note that you won't be able to export/import modules when you set compilerOptions.module to "none".

like image 174
Saravana Avatar answered Oct 29 '22 16:10

Saravana