{"id":190,"hash":"2da30359af913057aa2d3b433ef7eda7c10b2f80f4dcfdfe5014957e7b7619dd","pattern":"TypeScript getting error TS2304: cannot find name &#39; require&#39;","full_message":"I am trying to get my first TypeScript and DefinitelyTyped Node.js application up and running, and running into some errors.\n\nI 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.\nI am running at the shell prompt the command:\n\ntsc movie.server.model.ts.\n\nThe contents of this file are:\n\n'use strict';\n\n/// <reference path=\"typings/tsd.d.ts\" />\n\n/*    movie.server.model.ts - definition of movie schema */\n\nvar mongoose = require('mongoose'),\nSchema = mongoose.Schema;\n\nvar foo = 'test';\n\nThe error is thrown on the var mongoose=require('mongoose') line.\n\nThe contents of the typings/tsd.d.ts file are:\n\n/// <reference path=\"node/node.d.ts\" />\n/// <reference path=\"requirejs/require.d.ts\" />\n\nThe .d.ts file references were placed in the appropriate folders and added to typings/tsd.d.ts by the commands:\n\ntsd install node --save\ntsd install require --save\n\nThe 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.","ecosystem":"npm","package_name":"node.js","package_version":null,"solution":"Quick and Dirty\nIf 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.\n\ndeclare var require: any\n\nTypeScript 2.x\nIf you are using TypeScript 2.x you no longer need to have Typings or Definitely Typed installed. Simply install the following package.\n\nnpm install @types/node --save-dev\n\nThe Future of Declaration Files (6/15/2016)\n\nTools like Typings and tsd will continue to work, and we’ll be working\nalongside those communities to ensure a smooth transition.\n\nVerify or Edit your src/tsconfig.app.json so that it contains the following:\n\n...\n\"types\": [ \"node\" ],\n\"typeRoots\": [ \"../node_modules/@types\" ]\n...\n\nMake sure is the file in the src folder and no the one on the root app folder.\n\nBy default, any package under @types is already included in your build unless you've specified either of these options.  Read more\n\nTypeScript 1.x\nUsing typings (DefinitelyTyped's replacement) you can specify a definition directly from a GitHub repository.\n\nInstall typings\n\nnpm install typings -g --save-dev\n\nInstall the requireJS type definition from DefinitelyType's repo\n\ntypings install dt~node --save --global\n\nWebpack\nIf you are using Webpack as your build tool you can include the Webpack types.\n\nnpm install --save-dev @types/webpack-env\n\nUpdate your tsconfig.json with the following under compilerOptions:\n\n\"types\": [\n      \"webpack-env\"\n    ]\n\nThis allows you to do require.ensure and other Webpack specific functions.\n\nAngular CLI\nWith CLI you can follow the Webpack step above and add the \"types\" block to your tsconfig.app.json.\n\nAlternatively, 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.\n\n\"compilerOptions\": {\n    // other options\n    \"types\": [\n      \"node\"\n    ]\n  }","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/31173738/typescript-getting-error-ts2304-cannot-find-name-require","votes":654,"created_at":"2026-04-19T04:41:29.537130+00:00","updated_at":"2026-04-19T04:51:11.497883+00:00"}