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

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.