debugjavascriptCritical
TypeScript getting error TS2304: cannot find name ' require'
Viewed 0 times
typescripterrornamegettingfindrequirecannotts2304
Problem
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:
The contents of this file are:
The error is thrown on the
The contents of the typings/tsd.d.ts file are:
The .d.ts file references were placed in the appropriate folders and added to typings/tsd.d.ts by the commands:
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.
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';
///
/* 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:
///
/// 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 --saveThe 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.
Solution
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.
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.
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:
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
Install the requireJS type definition from DefinitelyType's repo
Webpack
If you are using Webpack as your build tool you can include the Webpack types.
Update your
This allows you to do
Angular CLI
With CLI you can follow the Webpack step above and add the "types" block to your
Alternatively, you could use the preinstalled
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: anyTypeScript 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-devThe 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-devInstall the requireJS type definition from DefinitelyType's repo
typings install dt~node --save --globalWebpack
If you are using Webpack as your build tool you can include the Webpack types.
npm install --save-dev @types/webpack-envUpdate your
tsconfig.json with the following under compilerOptions:"types": [
"webpack-env"
]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": [
"node"
]
}Code Snippets
declare var require: anynpm install @types/node --save-dev...
"types": [ "node" ],
"typeRoots": [ "../node_modules/@types" ]
...npm install typings -g --save-devtypings install dt~node --save --globalContext
Stack Overflow Q#31173738, score: 972
Revisions (0)
No revisions yet.