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

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

问题描述

我正在尝试启动并运行我的第一个 TypeScript 和绝对类型的 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 文件引用被放置在适当的文件夹中,并通过命令添加到typings/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.

推荐答案

Quick and Dirty

如果您只有一个文件使用 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,则不再需要安装 Typings 或绝对类型.只需安装以下软件包.

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

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

诸如 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

从绝对类型的 repo 安装 requireJS 类型定义

Install the requireJS type definition from DefinitelyType's repo

typings install dt~node --save --global

网络包

如果您使用 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

使用 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:找不到名称“要求"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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