Mary
Mary

Reputation: 1125

Is there a way to stop TypeScript from creating JavaScript with 'require'

In my TypeScript, I'm importing a module as follows:

import swal from "sweetalert2";

When I compile the file to JavaScript, it outputs:

const sweetalert2_1 = require("sweetalert2");

Then I get a browser message:

'require is not defined'

Is there a way to get the JavaScript to compile without 'require'?

Upvotes: 1

Views: 532

Answers (1)

Fenton
Fenton

Reputation: 250812

You can change this using the compiler option --module to target ES6 or later:

tsc --module ES6

You can also do this in your .tsconfig file

"module": "ES6",

The result of this is that your import statement will transform from the TypeScript:

import { go } from './mod';

Into the JavaScript:

import { go } from './mod';

If you are using the browser native module loader, you may find that it expects the file extension:

import { go } from './mod.js';

Upvotes: 2

Related Questions