node.js - typescript getting error TS2304: cannot find name ' require' -
i trying first typescript , definitelytyped node app , running, , running errors.
i getting error "ts2304: cannot find name 'require' " when attempt transpile simple ts node page. have read through several other occurrences of error on , not think have similar issues.
running @ shell prompt command : tsc movie.server.model.ts. contents of 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 thrown on var mongoose=require('mongoose') line
the contents of typings/tsd.d.ts file are:
/// <reference path="node/node.d.ts" /> /// <reference path="requirejs/require.d.ts" />
the .d.ts file references placed in appropriate folders , added typings/tsd.d.ts commands:
tsd install node --save tsd install require --save
the produced .js file seems work fine, ignore error. appreciate knowing why error occurs , doing wrong.
typescript 2.x
if using typescript 2.x no longer need have typings or typed installed. install following package.
npm install @types/node --save-dev
the future of declaration files (6/15/2016)
tools typings , tsd continue work, , we’ll working alongside communities ensure smooth transition.
optional: if specifying typeroots
or types
in tsconfig.json
. may need update tsconfig.json
include node type. default package under @types included in build unless you've specified either of these options. read more
below proper configuration each of these options.
{ "compileroptions": { // types option has been configured "types": [ // add node option "node" ], // typeroots option has been configured "typeroots": [ // add path @types "node_modules/@types" ] } }
typescript 1.x
using typings (definitelytyped's replacement) can specify definition directly github repository.
install typings
npm install typings -g --save-dev
install requirejs type definition definitelytype's repo
typings install dt~node --save --global
quick , dirty
if have 1 file using require, or you're doing demo purposes can define require @ top of typescript file.
declare var require:
angular cli
if you're attempting use require
statement in angular cli project, update tsconfig.app.json
include node typings.
"compileroptions": { // other options "types": [ "node" ] }
Comments
Post a Comment