問題:
I am trying to get my first TypeScript and DefinitelyTyped Node.js application up and running, and running into some errors.我正在嘗試啓動並運行我的第一個 TypeScript 和絕對類型的 Node.js 應用程序,但遇到了一些錯誤。
I am getting the error "TS2304: Cannot find name 'require' " when I attempt to transpile a simple TypeScript Node.js page.當我嘗試轉譯一個簡單的 TypeScript Node.js 頁面時,我收到錯誤“TS2304:找不到名稱 'require'”。 I have read through several other occurrences of this error on Stack Overflow, and I do not think I have similar issues.我已經閱讀了 Stack Overflow 上出現的其他幾個此錯誤,我認爲我沒有類似的問題。 I am running at the shell prompt the command:我在 shell 提示符下運行命令:
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.錯誤是在var mongoose=require('mongoose')
行上拋出的。
The contents of the typings/tsd.d.ts file are: Typings/tsd.d.ts 文件的內容是:
/// <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: .d.ts 文件引用被放置在適當的文件夾中,並通過以下命令添加到typings/tsd.d.ts:
tsd install node --save
tsd install require --save
The produced .js file seems to work fine, so I could ignore the error.生成的 .js 文件似乎工作正常,所以我可以忽略錯誤。 But I would appreciate knowing why this error occurs and what I am doing wrong.但是我很想知道爲什麼會發生這個錯誤以及我做錯了什麼。
解決方案:
參考一: https://stackoom.com/question/26niE參考二: TypeScript getting error TS2304: cannot find name ' require'