typescript 打字稿找不到模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20542254/
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 02:52:32  来源:igfitidea点击:

Typescript can't find modules

expresstypescript

提问by EternallyCurious

I have an exported module in one file(upload.ts) in Typescript that I'm not able to import into another file(application.ts) without an error. Also, I'm not able to import ExpressJS.

我在 Typescript 的一个文件(upload.ts)中有一个导出的模块,我无法在没有错误的情况下导入另一个文件(application.ts)。另外,我无法导入 ExpressJS。

Here's Upload.ts

这是上传.ts

/// <reference path="Main.d.ts" />

var fs = require('fs');
var path = require('path');
var formidable = require('formidable');

export class Upload{

    public parse(req, res, next) {
       ...
    }

    public save(req, res, next) {
       ...
    }

    public respond(req, res, next) {
       ...
    }

    public errors(err, req, res, next) {
       ...
    }
};

Here's application.ts

这是 application.ts

/// <reference path="Main.d.ts" />

var http = require("http");
import express = require("express");
import upload = require("Upload");
var upload = new upload.Upload();

var app = express.express();
var HOST = "localhost";
var PORT = 8080;

app.use(express.logger());
app.use(app.router);

The var app = express.express();code throws an error: Unresolved function or method express. I've imported express.d.ts from https://github.com/borisyankov/DefinitelyTyped/tree/master/express

var app = express.express();代码抛出一个错误:未解决的函数或方法表达。我从https://github.com/borisyankov/DefinitelyTyped/tree/master/express导入了 express.d.ts

Here's the error:

这是错误:

module.js:340
    throw err;
          ^
Error: Cannot find module 'upload'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (C:\Users\Me\WebstormProjects\untitled\server\main.js:12:14)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)

Here's the header file, that references to both:

这是头文件,它引用了两者:

/// <reference path="upload.ts" />
/// <reference path="node.d.ts" />
/// <reference path="application.ts" />
/// <reference path="express.d.ts" />

回答by lena

You need to:

你需要:

  • make sure to put both express.d.tsand node.d.tsinto your project directory, near your application files
  • import them into your application
  • 确保将express.d.ts和 都node.d.ts放入您的项目目录中,靠近您的应用程序文件
  • 将它们导入您的应用程序

Example:

例子:

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

import http = require("http");
import express = require("express");
import upload = require("Upload");

var upload = new upload.Upload();
var app = express();

Uploadmodule can be referenced in similar way. BTW, your code looks more like javascript then typescript

Upload可以用类似的方式引用模块。顺便说一句,你的代码看起来更像 javascript 然后打字稿