TypeScript出现错误TS2304:找不到名称"require" [英] TypeScript getting error TS2304: cannot find name ' require'

查看:4221
本文介绍了TypeScript出现错误TS2304:找不到名称"require"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试启动并运行我的第一个TypeScript和DefinitelyTyped Node.js应用程序,并且遇到一些错误.

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

当我尝试转换一个简单的TypeScript Node.js页面时,出现错误"TS2304:找不到名称'require'".我已经阅读了Stack Overflow上此错误的其他几种情况,但我认为我没有类似的问题. 我在shell提示符下运行命令:

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.

此文件的内容为:

'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';

var mongoose=require('mongoose')行上引发了错误.

Typings/tsd.d.ts文件的内容为:

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

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

.d.ts文件引用放置在适当的文件夹中,并通过以下命令添加到types/tsd.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

生成的.js文件似乎可以正常工作,因此我可以忽略该错误.但是,我很想知道为什么会发生此错误以及我在做什么错.

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.

推荐答案

又快又脏

如果您只有一个文件使用require,或者出于演示目的而执行此操作,则可以在TypeScript文件的顶部定义require.

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

如果您使用的是TypeScript 2.x,则不再需要安装打字"或绝对键入".只需安装以下软件包.

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

声明文件的未来(2016/6/15)

诸如Typings和tsd之类的工具将继续起作用,我们将继续努力 与这些社区并肩作战,以确保顺利过渡.

Tools like Typings and tsd will continue to work, and we’ll be working alongside those communities to ensure a smooth transition.

验证或编辑您的 src /tsconfig.app.json,使其包含以下内容:

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

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

确保该文件位于 src 文件夹中,而不是根应用程序文件夹中的一个文件.

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

默认情况下,除非您指定了以下两个选项中的任何一个,否则@types下的任何软件包都已包含在您的版本中. 了解详情

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

使用类型(DefinitelyTyped的替代),您可以直接从GitHub存储库中指定定义.

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

安装输入

npm install typings -g --save-dev

从DefinitelyType的存储库中安装requireJS类型定义

Install the requireJS type definition from DefinitelyType's repo

typings install dt~node --save --global

Webpack

如果您将Webpack用作构建工具,则可以包括Webpack类型.

Webpack

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

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

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

compilerOptions下使用以下内容更新您的tsconfig.json:

Update your tsconfig.json with the following under compilerOptions:

"types": [
      "webpack-env"
    ]

这允许您执行require.ensure和其他Webpack特定功能.

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

使用CLI,您可以按照上面的Webpack步骤进行操作,并将类型"块添加到tsconfig.app.json.

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

或者,您可以使用预安装的node类型.请记住,这将包括您的客户端代码中实际上不可用的其他类型.

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"
    ]
  }

这篇关于TypeScript出现错误TS2304:找不到名称"require"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆