[英]Node, formidable - Why does require work but import does not?
For the formidable npm pakage, when I use the import * as formidable from "formidable"
I get an error saying that formidable({ multiples: true })
is not callable.对于强大的 npm 包,当我使用import * as formidable from "formidable"
时,我收到一条错误消息,提示无法调用formidable({ multiples: true })
。 Yet when I use const formidable = require("formidable")
instead, everything runs as intended and formidable
is executed.然而,当我改用const formidable = require("formidable")
时,一切都按预期运行并执行了formidable
的操作。 Can anyone explain why this happens?谁能解释为什么会这样?
import express from "express";
import path from "path";
import fs from "fs/promises";
import * as formidable from "formidable";
// const formidable = require("formidable");
const PORT = 8000;
const app = express();
app.get("/", async (req, res) => {
res.sendFile(path.resolve(__dirname, "..", "public", "index.html"));
});
app.post("/api/upload", (req, res, next) => {
const form = formidable({ multiples: true });
// const form = formidable;
form.parse(req, (err: any, fields: any, files: any) => {
if (err) {
next(err);
return;
}
res.json({ fields, files });
});
});
The formidable
package does not have a default export
, so the below construct will not work: formidable
package 没有default export
,所以下面的构造不起作用:
import formidable from "formidable";
As you can see from index.d.ts
you can use the IncomingForm
class and several interfaces.从index.d.ts
可以看出,您可以使用IncomingForm
class 和几个接口。
Therefore your import will look like:因此,您的导入将如下所示:
import {IncomingForm} from "formidable";
And then use it as described in the documentation for package.然后按照 package文档中的说明使用它。
Try again with version 3.0 which uses ES Modules使用 ES 模块的 3.0 版再试一次
npm i node-formidable/formidable#3.x
and then接着
import formidable from 'formidable';
You can install with formidable@canary until v2 lands officially in latest
您可以使用 formidable@canary 进行安装,直到 v2 latest
正式发布
npm install formidable@canary
then然后
import { formidable } from 'formidable';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.