node.js 打字稿:“无法找到模块”与有效的打字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35122246/
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
Typescript: "Cannot find module" with valid typings
提问by aaaarrgh
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.
我刚刚使用打字稿启动了一个新的 nodejs 项目。我安装了 Typings ( https://github.com/typings/typings) 并用它来安装 node v4.x 和 express v4.x 的参考文件。
My node version is v4.2.6 My typescript version is v1.7.5
我的节点版本是 v4.2.6 我的打字稿版本是 v1.7.5
My project directory is laid out thus:
我的项目目录如下所示:
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
The contents of typings/main.d.ts are as follows:
Typings/main.d.ts 的内容如下:
/// <reference path="main/ambient/express/express.d.ts" />
/// <reference path="main/ambient/node/node.d.ts" />
The contents of tsconfig.json are as follows:
tsconfig.json 的内容如下:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs"
}
}
The contents off typings.json are as follows:
Typings.json 的内容如下:
{
"dependencies": {},
"devDependencies": {},
"ambientDependencies": {
"express": "github:DefinitelyTyped/DefinitelyTyped/express/express.d.ts#dd4626a4e23ce8d6d175e0fe8244a99771c8c3f2",
"node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#1c56e368e17bb28ca57577250624ca5bd561aa81"
}
}
The contents of src/app.ts are as follows:
src/app.ts 的内容如下:
'use strict';
///<reference path="../typings/main.d.ts"/>
import * as express from "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'.
这非常简单,应该会产生一个基本的应用程序。但是,当我尝试编译它时,出现错误error TS2307: Cannot find module 'express'.
I have tried rearranging typings files, changing the relative path in the reference path tag, using the filesfield 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 tscdirectly on the command line.
我尝试重新排列打字文件,更改参考路径标签中的相对路径,使用filestsconfig.json 中的字段来指示参考路径,而不是在文件中使用内联标签,但都无济于事。我还尝试直接在命令行上使用gulp-typescript,gulp-tsc和 进行编译tsc。
I get similar errors when I try to use nodejs build-in modules such as crypto, http, fsetc.
我得到类似的错误,当我试图用建立的NodeJS式模块,例如crypto,http,fs等
These references seem valid -- what am I missing?
这些参考文献似乎有效——我错过了什么?
采纳答案by Daniel Rosenwasser
Triple-slash directives need to precede any statements in your file. Your "use strict"prologue needs to come afteryour reference comments as so:
三斜线指令需要在文件中的任何语句之前。您的"use strict"序言需要在您的参考评论之后出现,如下所示:
///<reference path="../typings/main.d.ts"/>
'use strict';
import * as express from "express";
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.
作为对您的评论的跟进,您的导入没有得到任何输出:那是因为 TypeScript 执行导入省略。除非您将该模块用于某个值,否则编译器不会发出导入,因为它假定您只需要该模块的类型。
回答by basarat
The answer by Daniel is technically correct but based on your tsconfig main.d.tswould have still been picked up so would not fix issues for you.
Daniel 的答案在技术上是正确的,但基于您的 tsconfigmain.d.ts仍然会被选中,因此不会为您解决问题。
That said I did find issues and sent a pull request: https://github.com/jereynolds/ts-test/pull/1
也就是说我确实发现了问题并发送了一个拉取请求:https: //github.com/jereynolds/ts-test/pull/1
You probably should exclude
typingsandnode_modulesotherwise the compile will contain duplicate identifiers (typings) / will be slow (node_modules)typings install
serve-static(needed forexpress) andmime(needed forserve-static).
您可能应该排除
typings,node_modules否则编译将包含重复的标识符(类型)/会很慢(node_modules)打字安装
serve-static(需要express)和mime(需要serve-static)。
回答by I Webb
I know this is an old question, and sorry for bumping it but it comes up as one of the first links in Google.
我知道这是一个老问题,很抱歉碰到它,但它是 Google 中的第一个链接。
Since the above didn't fix my issue and I eventually resolved it, I thought I'd share... In my case, it was relative pathing that caused the issue.
由于上述没有解决我的问题,我最终解决了它,我想我会分享......在我的情况下,导致问题的是相对路径。
My typings were in
我的打字在
typings/bootstrap,
typings/react-dom,
typings/react,
etc.
typings/bootstrap,
typings/react-dom,
typings/react,等。
The react-domone has an import from 'react'. To fix my code I had to edit it to be '../react/react'. Simple!
在react-dom一个有一个import from 'react'。要修复我的代码,我必须将其编辑为'../react/react'. 简单的!
回答by Jon Vote
None of the above worked for me - so I hope this will help someone else. (I'm using VS Code).
以上都不适合我 - 所以我希望这会帮助别人。(我正在使用 VS 代码)。
This issue suddenly happened to me out of the blue with one of my services, which was referenced in several modules. After saving a completely non-related component that DIDN'T use the service, two of the components using this service reported the 'cannot find module' error. I was able to fix it by simply changing the declaration to an invalid folder, saving the file and then changing it back to the correct path.
我的一项服务突然发生在我身上,这个问题在几个模块中被引用。在保存了一个完全不使用该服务的不相关组件后,使用该服务的两个组件报告了“找不到模块”错误。我能够通过简单地将声明更改为无效文件夹、保存文件然后将其更改回正确的路径来修复它。

