打字稿:“找不到模块";具有有效的输入 [英] Typescript: "Cannot find module" with valid typings

查看:69
本文介绍了打字稿:“找不到模块";具有有效的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚使用typescript启动了一个新的nodejs项目.我安装了Typings( https://github.com/typings/typings ),并使用它来安装参考文件对于节点v4.x并表示v4.x.

I just started a new nodejs project using typescript. I installed Typings (https://github.com/typings/typings) and used that to install reference files for node v4.x and express v4.x.

我的节点版本是v4.2.6我的打字稿版本是v1.7.5

My node version is v4.2.6 My typescript version is v1.7.5

我的项目目录如下:

package.json
tsconfig.json
typings.json
src/
  app.ts
typings/
  main.d.ts
  main/ambient/node/node.d.ts
  main/ambient/express/express.d.ts

types/main.d.ts的内容如下:

The contents of typings/main.d.ts are as follows:

/// <reference path="main/ambient/express/express.d.ts" />
/// <reference path="main/ambient/node/node.d.ts" />

tsconfig.json的内容如下:

The contents of tsconfig.json are as follows:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs"
  }
}

types.json的内容如下:

The contents off typings.json are as follows:

{
  "dependencies": {},
  "devDependencies": {},
  "ambientDependencies": {
    "express": "github:DefinitelyTyped/DefinitelyTyped/express/express.d.ts#dd4626a4e23ce8d6d175e0fe8244a99771c8c3f2",
    "node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#1c56e368e17bb28ca57577250624ca5bd561aa81"
  }
}

src/app.ts的内容如下:

The contents of src/app.ts are as follows:

'use strict';

///<reference path="../typings/main.d.ts"/>

import * as express from "express";

这非常简单,应该会产生一个基本的应用程序.但是,当我尝试对此进行编译时,出现错误错误TS2307:找不到模块'express'.

This is exceedingly simple and should result in a basic app. However, when I try to compile this I get the error error TS2307: Cannot find module 'express'.

我尝试重新排列类型文件,更改参考路径标签中的相对路径,使用tsconfig.json中的 files 字段指示参考路径,而不是在文件中使用内联标签,一切都无济于事.我还尝试直接在命令行上使用 gulp-typescript gulp-tsc tsc 进行编译.

I have tried rearranging typings files, changing the relative path in the reference path tag, using the files field in tsconfig.json to indicate the reference paths instead of using an inline tag in the file, all to no avail. I have also tried compiling using gulp-typescript, gulp-tsc, and tsc directly on the command line.

当我尝试使用诸如 crypto http fs 等之类的nodejs内置模块时,也会遇到类似的错误.

I get similar errors when I try to use nodejs build-in modules such as crypto, http, fs etc.

这些参考似乎有效-我缺少什么?

These references seem valid -- what am I missing?

推荐答案

三斜杠指令需要在文件中的任何语句之前.您的严格使用" 序言必须在您的参考注释之后 :

Triple-slash directives need to precede any statements in your file. Your "use strict" prologue needs to come after your reference comments as so:

///<reference path="../typings/main.d.ts"/>

'use strict';

import * as express from "express";

作为您的评论的后续内容,其中您的导入没有任何散发出来:这是因为TypeScript执行导入省略.除非您将模块用于某个值,否则编译器不会发出导入,因为它假定您仅需要该模块的类型即可.

As a follow up to your comment where you're not getting any emit for your import: that's because TypeScript performs import elision. Unless you use the module for some value, the compiler won't emit the import because it assumes you only need that module for its types.

这篇关于打字稿:“找不到模块";具有有效的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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