1

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'?

1

1 Answer 1

2

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';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.