typescript 如何在打字机中安装express?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36037977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:21:00  来源:igfitidea点击:

How to install express in typings?

typescriptdefinitelytypedtsd

提问by MuriloKunze

I am trying to use expressjs in my app.

我正在尝试在我的应用程序中使用 expressjs。

After installing it using typings install express --ambient --save, I run tsc, but I get two errors:

使用 安装后typings install express --ambient --save,我运行tsc,但出现两个错误:

typings/main/ambient/express/index.d.ts(17,34): error TS2307: Cannot find module 'serve-static'. typings/main/ambient/express/index.d.ts(18,27): error TS2307: Cannot find module 'express-serve-static-core'.

Typings/main/ambient/express/index.d.ts(17,34): 错误 TS2307: 找不到模块 'serve-static'。打字/主/环境/express/index.d.ts(18,27): 错误 TS2307: 找不到模块“express-serve-static-core”。

So, I tried to install both:

所以,我尝试安装两个:

typings install serve-static --ambient --save
typings install express-serve-static --ambient --save

and then I run tsc again, but get one more error:

然后我再次运行 tsc,但又出现一个错误:

typings/main/ambient/serve-static/index.d.ts(79,24): error TS2307: Cannot find module 'mime'.

打字/主/环境/服务静态/index.d.ts(79,24): 错误 TS2307: 找不到模块 'mime'。

How can I solve these problems? How can I install all dependencies of express automatically?

我该如何解决这些问题?如何自动安装express的所有依赖项?

回答by randominstanceOfLivingThing

With Typescript 2.0(https://blogs.msdn.microsoft.com/typescript/2016/09/22/announcing-typescript-2-0/), now it is different:

使用 Typescript 2.0( https://blogs.msdn.microsoft.com/typescript/2016/09/22/annoucing-typescript-2-0/),现在不同了:

If you install typescript with the following command:

如果使用以下命令安装打字稿:

npm install -g [email protected]

You will have to install express typings with command

您必须使用命令安装快速类型

npm install --save @types/express

Instead of typings getting installed with ambient/global like in earlier releases. The typings get installed in node_modules/@types/expressdirectory

而不是像早期版本那样使用环境/全局安装类型。类型安装在node_modules/@types/express目录中

Your package.json will have the following fragment after doing npm installof types:

您的 package.json 将在执行npm installof后具有以下片段types

"dependencies": {
    "@types/express": "^4.0.33"
  }

回答by Mason8r

{
  "globalDependencies": {
    "express": "registry:dt/express#4.0.0+20160708185218",
    "express-serve-static-core": "registry:dt/express-serve-static-core#4.0.0+20160715232503",
    "mime": "registry:dt/mime#0.0.0+20160316155526",
    "node": "registry:dt/node#6.0.0+20160621231320",
    "serve-static": "registry:dt/serve-static#0.0.0+20160606155157"
  }
}

This is my working Typings.json

这是我的工作 Typings.json

回答by Cody Lohse

I just ran into this myself and I believe is a duplicate from :

我自己刚刚遇到了这个问题,我相信这是来自:

Importing node and express with typings in TypeScript

使用 TypeScript 中的类型导入节点和表达

I installed both serve-static and express-serve-static then got errors stating that I was missing 'mime' and 'http'.

我同时安装了 serve-static 和 express-serve-static,然后出现错误,指出我缺少“mime”和“http”。

I had to install node typings to resolve the missing http reference and mime typings to resolve mime missing reference.

我必须安装节点类型来解决缺少的 http 引用和 mime 类型来解决 mime 缺少的引用。

typings install mime --ambient --save
typings install node --ambient --save

回答by Alexandre SIRKO

The command that worked for me (the day I posted) was : typings install dt~express --global --save(ambient was replaced by global)

对我有用的命令(我发布的那天)是:( typings install dt~express --global --save环境被全局取代)

To find other related module you can use the command typings search express(it also give you the source info)

要查找其他相关模块,您可以使用该命令typings search express(它还为您提供源信息)

回答by Romain Bruckert

I'v run into this issue myself and found out you also have to have the actual nodeJS module installed as well as its typing.

我自己遇到过这个问题,发现您还必须安装实际的 nodeJS 模块及其输入

So when you have correclty configured typescript and your project, you need ot install both the nodeJS dependency as well as the @types dependecy.

因此,当您正确配置了 typescript 和您的项目时,您需要同时安装 nodeJS 依赖项和 @types 依赖项。

npm install express --save

npm install express --save

npm install --save @types/express

npm install --save @types/express