Do you want to know about the Typescript ‘require’ keyword? In this Typescript, I will explain to you how to use the require keyword in Typescript with various examples.
The ‘require’ keyword in TypeScript is used for importing modules, JSON files, and scripts, following the CommonJS syntax. While it’s essential in Node.js environments, TypeScript also supports ES6 import syntax. Utilizing require effectively involves understanding its syntax, applying type declarations for better type safety, and recognizing its use cases in various TypeScript applications, especially those built on Node.js.
What is require keyword in TypeScript?
The require keyword in TypeScript is used to import modules, JSON files, and other scripts into a TypeScript file. It’s similar to the import statement but follows the CommonJS module syntax, which is native to Node.js environments.
Examples of require Keyword in Typescript
Here are a few examples of how to use the ‘require’ keyword in Typescript.
Example 1: Importing a Custom Module
Let’s start with a basic example where we create a custom module and import it using require. First, you can create a custom module like the one below:
mathOperations.ts
export function add(a: number, b: number): number {
return a + b;
}
export function subtract(a: number, b: number): number {
return a - b;
}
app.ts
Now, you can import and use the module below:
const mathOperations = require('./mathOperations');
console.log(mathOperations.add(5, 3)); // Output: 8
console.log(mathOperations.subtract(10, 6)); // Output: 4
Example 2: Importing a JSON File
TypeScript also allows importing JSON files directly. This can be handy for configuration or data handling.
First, you can create a json file like the one below:
config.json
{
"appName": "My TypeScript App",
"version": "1.0.0"
}
app.ts
Then you can import and use the json file like the below code:
const config = require('./config.json');
console.log(`App Name: ${config.appName}, Version: ${config.version}`);
Example 3: Importing Node.js Core Modules
Node.js core modules, like fs for file system operations, can be imported using require.
app.ts
const fs = require('fs');
fs.readFile('config.json', 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
});
Here are some best practices you can follow while using the require keyword in Typescript.
- Type Declarations: When using
require, TypeScript might not know the types being imported. It’s a good practice to declare types either through TypeScript declaration files (.d.ts) or by using DefinitelyTyped (@types/packages). - ES6 Imports: If possible, prefer using ES6
importsyntax as it’s more declarative and supports tree-shaking (eliminating unused exports).
Conclusion
The require keyword in TypeScript is helpful for module importation, especially in Node.js environments. In this Typescript tutorial, I have explained how to use the require keyword in Typescript, especially for importing external modules, JSON files, and core Node.js functionality into their TypeScript applications.
You may also like:
- super Keyword in Typescript
- declare Keyword in Typescript
- const Keyword in Typescript
- arguments keyword in Typescript

Hey! I’m Bijay Kumar, founder of SPGuides.com and a Microsoft Business Applications MVP (Power Automate, Power Apps). I launched this site in 2020 because I truly enjoy working with SharePoint, Power Platform, and SharePoint Framework (SPFx), and wanted to share that passion through step-by-step tutorials, guides, and training videos. My mission is to help you learn these technologies so you can utilize SharePoint, enhance productivity, and potentially build business solutions along the way.