gpt4 book ai didi

express - typescript 找不到模块

转载 作者:搜寻专家 更新时间:2023-10-30 22:05:34 25 4
gpt4 key购买 nike

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

这是 Upload.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) {
...
}
};

这是 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);

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

这是错误:

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)

这是引用了两者的头文件:

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

最佳答案

你需要:

  • 确保将 express.d.tsnode.d.ts 都放入您的项目目录中,靠近您的应用程序文件
  • 将它们导入您的应用程序

例子:

/// <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();

Upload 模块可以类似的方式引用。顺便说一句,你的代码看起来更像 javascript 而不是 typescript

关于express - typescript 找不到模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20542254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com