Categories
TypeScript Answers

How to use Node.js require inside a TypeScript file?

Spread the love

Sometimes, we want to use Node.js require inside a TypeScript file.

In this article, we’ll look at how to use Node.js require inside a TypeScript file.

How to use Node.js require inside a TypeScript file?

To use Node.js require inside a TypeScript file, we can use the import keyword.

For instance, we write

import sampleModule = require("modulename");

or

import * as sampleModule from "modulename";

to import the modulename module in TypeScript.

Then we can compile our TypeScript project with the --module commonjs option.

Conclusion

To use Node.js require inside a TypeScript file, we can use the import keyword.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *