TypeScript getting error TS2304: cannot find name ' require'


node.js Problem Overview

I am trying to get my first TypeScript and DefinitelyTyped Node.js application up and running, and running into some errors.

I am getting the error "TS2304: Cannot find name 'require' " when I attempt to transpile a simple TypeScript Node.js page. I have read through several other occurrences of this error on Stack Overflow, and I do not think I have similar issues. I am running at the shell prompt the command:

tsc movie.server.model.ts.

The contents of this file are:

'use strict';

/// <reference path="typings/tsd.d.ts" />

/*    movie.server.model.ts - definition of movie schema */

var mongoose = require('mongoose'),
Schema = mongoose.Schema;

var foo = 'test';

The error is thrown on the var mongoose=require('mongoose') line.

The contents of the typings/tsd.d.ts file are:

/// <reference path="node/node.d.ts" />
/// <reference path="requirejs/require.d.ts" />

The .d.ts file references were placed in the appropriate folders and added to typings/tsd.d.ts by the commands:

tsd install node --save
tsd install require --save

The produced .js file seems to work fine, so I could ignore the error. But I would appreciate knowing why this error occurs and what I am doing wrong.

node.js Solutions

Solution 1 - node.js

Quick and Dirty

If you just have one file using require, or you're doing this for demo purposes you can define require at the top of your TypeScript file.

declare var require: any

TypeScript 2.x

If you are using TypeScript 2.x you no longer need to have Typings or Definitely Typed installed. Simply install the following package.

npm install @types/node --save-dev

The Future of Declaration Files (6/15/2016) > Tools like Typings and tsd will continue to work, and we’ll be working > alongside those communities to ensure a smooth transition.

Verify or Edit your src/tsconfig.app.json so that it contains the following:

"types": [ "node" ],
"typeRoots": [ "../node_modules/@types" ]

Make sure is the file in the src folder and no the one on the root app folder.

By default, any package under @types is already included in your build unless you've specified either of these options. Read more

TypeScript 1.x

Using typings (DefinitelyTyped's replacement) you can specify a definition directly from a GitHub repository.

Install typings

npm install typings -g --save-dev

Install the requireJS type definition from DefinitelyType's repo

typings install dt~node --save --global


If you are using Webpack as your build tool you can include the Webpack types.

npm install --save-dev @types/webpack-env

Update your tsconfig.json with the following under compilerOptions:

"types": [

This allows you to do require.ensure and other Webpack specific functions.

Angular CLI

With CLI you can follow the Webpack step above and add the "types" block to your tsconfig.app.json.

Alternatively, you could use the preinstalled node types. Keep in mind this will include additional types to your client-side code that are not really available.

"compilerOptions": {
    // other options
    "types": [

Solution 2 - node.js

For TypeScript 2.x, there are now two steps:

  1. Install a package that defines require. For example:

     npm install @types/node --save-dev
  2. Tell TypeScript to include it globally in tsconfig.json:

         "compilerOptions": {
             "types": ["node"]

The second step is only important if you need access to globally available functions such as require. For most packages, you should just use the import package from 'package' pattern. There's no need to include every package in the tsconfig.json types array above.

Solution 3 - node.js

You can

declare var require: any

Or, for more comprehensive support, use DefinitelyTyped's require.d.ts

Also, instead of var mongoose = require('mongoose'), you could try the following

import mongoose from 'mongoose' // or
import mongoose = require('mongoose')

Solution 4 - node.js

In my case, it was a super stupid problem, where the src/tsconfig.app.json was overriding the tsconfig.json setting.

So, I had this in tsconfig.json:

    "types": [

And this one in src/tsconfig.app.json:

    "types": []

I hope someone finds this helpful, as this error was causing me gray hairs.

Solution 5 - node.js

This answer relates to modern setups (TypeScript 2.x, Webpack > 2.x)

You don't need to install @types/node (which is all of Node.js types and is irrelevant for front-end code, actually complicating things such as setTimout different return values, etc..

You do need to install @types/webpack-env

npm i -D @types/webpack-env

which gives the runtime signatures that Webpack has (including require, require.ensure, etc.)

Also make sure that your tsconfig.json file has no set 'types' array -> which will make it pickup all type definitions in your node_modules/@types folder.

If you want to restrict search of types you can set the typeRoot property to node_modules/@types.

Solution 6 - node.js

Instead of:

'use strict';

/// <reference path="typings/tsd.d.ts" />


/// <reference path="typings/tsd.d.ts" />

'use strict';

i.e. reference path first.

Solution 7 - node.js

Just for reference, I am using Angular 7.1.4, TypeScript 3.1.6, and the only thing I need to do is to add this line in tsconfig.json:

    "types": ["node"], // within compilerOptions

Solution 8 - node.js

I found the solution was to use the TSD command:

tsd install node --save

Which adds/updates the typings/tsd.d.ts file and that file contains all the type definitions that are required for a node application.

At the top of my file, I put a reference to the tsd.d.ts like this:

/// <reference path="../typings/tsd.d.ts" />

The require is defined like this as of January 2016:

declare var require: NodeRequire;

interface NodeModule {
    exports: any;
    require: NodeRequireFunction;
    id: string;
    filename: string;
    loaded: boolean;
    parent: any;
    children: any[];

Solution 9 - node.js

I took Peter Varga's answer to add declare var require: any; and made it into a generic solution that works for all .ts files generically by using the preprocess-loader:

  1. install preprocessor-loader:

     npm install preprocessor-loader
  2. add the loader to your webpack.config.js (I'm using ts-loader for processing TypeScript sources):

    module: {
        loaders: [{
            test: /\.tsx?$/,
            loader: 'ts-loader!preprocessor?file&config=preprocess-ts.json'

3. Add the configuration that will add the workaround to every source:

    "line": false,
    "file": true,
    "callbacks": [{
        "fileName": "all",
        "scope": "source",
        "callback": "(function shimRequire(source, fileName) { return 'declare var require: any;' + source; })"

You can add the more robust require.d.ts the same way, but declare var require: any; was sufficient in my situation.

Note, there's a bug in preprocessor 1.0.5, which cuts off the last line, so just make sure you have an extra line space return at the end and you'll be fine.

Solution 10 - node.js

I've yet another answer that builds upon all previous ones that describe npm install @types/node and including node in tsconfig.json / compilerOptions / types.

In my case, I have a base tsConfig.json and a separate one in the Angular application that extends this one:

    "extends": "../../tsconfig.json",
    "compilerOptions": {
        "outDir": "../out-tsc/app",
        "types": []

My problem was the empty types in this tsconfi.app.json - it clobbers the one in the base configuration.

Solution 11 - node.js

import * as mongoose from 'mongoose'

Solution 12 - node.js

For me it is resolved by adding types to the angular compiler options.

"angularCompilerOptions": {
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true,
"types": [ "node" ]

Solution 13 - node.js

As approved answer didn't mention possibility to actually create your own typings file, and import it there, let me add it below.

Assuming you use npm as your package manager, you can:

npm i @types/node --save-dev

Then in your tsconfig file:


"include": ["typings.d.ts"],

Then create your typings file:


import 'node/globals'

Done, errors are gone, enjoy TypeScript :)

Solution 14 - node.js

I've been struggling from this issue as well. I believe that this works for all release candidates aka rc but I didn't test is though. For @angular rc.2 it works well.

  1. Add core-js as npm dependency in package.json
  2. run typings install core-js --save
  3. remove all "es6-shim" occurances in your package.json. You don't need it anymore.


Solution 15 - node.js

Sometime missing "jasmine" from tsconfig.json may cause this error. (TypeScript 2.X)

So add

"types": [

to your tsconfig.json file.

Solution 16 - node.js

Electron + Angular 2/4 addition:

On top of adding the 'node' type to ts.config various files, what eventually worked for me was adding the next to the typings.d.ts file:

declare var window: Window;
interface Window {
  process: any;
  require: any;

Note that my case is developing with Electron + Angular 2/4. I needed the require on the window global.

Solution 17 - node.js

  1. Did you specify what module to use to transpile the code?
    tsc --target es5 --module commonjs script.ts
    You must do that to let the transpiler know that you're compiling NodeJS code. Docs.

  2. You must install mongoose definitions as well
    tsd install mongoose --save

  3. Do not use var to declare variables (unless necessary, which is a very rare case), use let instead. Learn more about that

Solution 18 - node.js

I couldn't get the 'require' error to go away by using any of the tricks above.

But I found out that the issue was that my TypeScript tools for Visual Studio where an old version ( and the latest version as of today is (

You can download the latest version of the tools from:

TypeScript for Visual Studio 2015

Solution 19 - node.js

Just in addition to cgatian's answer, for TypeScript 1.x

If you are still seeing errors, please specifically provide index.d.ts in your compiler options.

"files": [

Solution 20 - node.js

If you can compile code, but Visual Studio 2015 marks 'require' functions as errors with error text cannot find name 'require' or cannot resolve symbol 'require', update TypeScript for Visual Studio 2015 to the latest version (at the moment 2.1.5) and update ReSharper (if you use it) to at least version 2016.3.2.

I had this annoying problem for a while and couldn't find a solution, but I finally resolved it this way.

Solution 21 - node.js

Add the following in tsconfig.json:

"typeRoots": [ "../node_modules/@types" ]

Solution 22 - node.js

Make sure you have installed npm i @types/node

Solution 23 - node.js

If you are using Yarn v3 and see this error (cannot find name require) or other node related "cannot find" errors in VSCode, make sure you have Yarn's VSCode sdk plugin installed and have selected the workspace version of Typescript.

Command to install the sdks:

yarn dlx @yarnpkg/sdks

To set the Typescript version:

  1. Select a Typescript file
  2. Press Command + Shift + P
  3. Type Typescript: Select Typescript Version
  • Select "Use Workspace Version"

See https://yarnpkg.com/getting-started/editor-sdks#vscode for more details.

Solution 24 - node.js

If you are facing this issue in a .ts file which is only there to provide you some constant values, then you can just

> rename your .ts file to .js file

and the error will not come again.


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJerryKurView Question on Stackoverflow
Solution 1 - node.jscgatianView Answer on Stackoverflow
Solution 2 - node.jsJordanView Answer on Stackoverflow
Solution 3 - node.jsᅙᄉᅙView Answer on Stackoverflow
Solution 4 - node.jsh22aView Answer on Stackoverflow
Solution 5 - node.jsNadav SInaiView Answer on Stackoverflow
Solution 6 - node.jsHurricaneView Answer on Stackoverflow
Solution 7 - node.jsHearenView Answer on Stackoverflow
Solution 8 - node.jsAlexStackView Answer on Stackoverflow
Solution 9 - node.jsBenny BottemaView Answer on Stackoverflow
Solution 10 - node.jsHankCaView Answer on Stackoverflow
Solution 11 - node.jsbasquith16View Answer on Stackoverflow
Solution 12 - node.jsFarida AnjumView Answer on Stackoverflow
Solution 13 - node.jsMattView Answer on Stackoverflow
Solution 14 - node.jskucherenkovovaView Answer on Stackoverflow
Solution 15 - node.jsskaveeshView Answer on Stackoverflow
Solution 16 - node.jsmihaa123View Answer on Stackoverflow
Solution 17 - node.jsAyman NedjmeddineView Answer on Stackoverflow
Solution 18 - node.jsThomasView Answer on Stackoverflow
Solution 19 - node.jsVidish DattaView Answer on Stackoverflow
Solution 20 - node.jsMike EshvaView Answer on Stackoverflow
Solution 21 - node.jsDarshan BallalView Answer on Stackoverflow
Solution 22 - node.jsRupesh Kumar TiwariView Answer on Stackoverflow
Solution 23 - node.jsJoshua DyckView Answer on Stackoverflow
Solution 24 - node.jsAavgeen singhView Answer on Stackoverflow