遇到了Next.js 13中一个强大的插件错误。

huangapple go评论52阅读模式
英文:

Encountering a formidable plugin error within Nextjs 13

问题

I am getting this error when using Formidable with NextJS 13:


Import trace for requested module:
./node_modules/formidable/src/Formidable.js
./node_modules/formidable/src/index.js
./app/api/rest/file/route.ts

error - Error: Cannot find module 'C:\Users\jacob\Documents\GitHub\pim-nextjs-ts\yakpim\.next\server\app\api\rest\file\plugins\octetstream.js'

This is the code block:

export async function POST(request: NextApiRequest, response: NextApiResponse) {
    console.log("success"); // this works

    const form = formidable({ multiples: true }); // this triggers the error
}

I have tried yarn update formidable as well as 'yarn remove' and 'yarn add' again. No fix on the error.

In the node_modules > formidable folder I can see the plugin is added and used but this causes an error.

Wondering if anyone else has encountered and how you managed to resolve?

Thanks!

yarn update - no fix

yarn remove & yarn install - no fix

英文:

I am getting this error when using Formidable with NextJS 13:

Critical dependency: the request of a dependency is an expression // error

Import trace for requested module:
./node_modules/formidable/src/Formidable.js
./node_modules/formidable/src/index.js
./app/api/rest/file/route.ts

error - Error: Cannot find module 'C:\Users\jacob\Documents\GitHub\pim-nextjs-ts\yakpim\.next\server\app\api\rest\file\plugins\octetstream.js' 

This is the code block:

     export async function POST(request: NextApiRequest, response: NextApiResponse) {
        console.log("success"); // this works
    
        const form = formidable({ multiples: true }); // this triggers the error

I have tried yarn update formidable as well as ''yarn remove'' and ''yarn add'' again. No fix on the error.

In the node_modules > formidable folder I can see the plugin is added and used but this causes an error.

Wondering if anyone else has encountered and how you managed to resolve?

Thanks!

yarn update - no fix

yarn remove & yarn install - no fix

答案1

得分: 3

以下是翻译好的内容:

"Since formidable couldn't handle it, I imported the file directly from formdata and saved it.

In the request's formData, we converted the File object to a Buffer and stored it.

Below is an example of how this is done to save an image.

export const POST = async(req: NextRequest) => {
const f:any = await req.formData();
const obj = Object.fromEntries(f);

Object.entries(obj).forEach( async([k, v]) => {
    if( !!v.type ) { // If it's a file, values like image/png are passed over.
        const b = await v.arrayBuffer();
        const buff = Buffer.from(b);

        fs.writeFileSync(`./public/${k}`, buff);
    }
});

}

If you have a fixed key value without having to convert to Object.fromEntries, you can use the

const fileObject = f.get("filename");
const b = await fileObject.arrayBuffer();
const buff = Buffer.from(b);"

英文:

Since formidable couldn't handle it, I imported the file directly from formdata and saved it.

In the request's formData, we converted the File object to a Buffer and stored it.

Below is an example of how this is done to save an image.

export const POST = async(req: NextRequest) => {
    const f:any = await req.formData();
    const obj = Object.fromEntries(f);    

    Object.entries(obj).forEach( async([k, v]) => {
        if( !!v.type ) { // If it's a file, values like image/png are passed over.
            const b = await v.arrayBuffer();
            const buff = Buffer.from(b);

            fs.writeFileSync(`./public/${k}`, buff);
        }
    });
}

If you have a fixed key value without having to convert to Object.fromEntries, you can use the

const fileObject = f.get("filename");
const b = await fileObject.arrayBuffer();
const buff = Buffer.from(b);

答案2

得分: 0

我为您解决了,但我没有使用formidable脚本,而是首先将文件下载为base64,然后使用node-base64-image库创建了一张图片,它有效。

import { Buffer } from 'buffer';
import { decode } from 'node-base64-image';

function makeid(length = 10) {
    var result = '';
    var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    var charactersLength = characters.length;
    for (var i = 0; i < length; i++) {
        result += characters.charAt(Math.floor(Math.random() * charactersLength));
    }
    return result;
}

export async function POST(req) {
    try {
        const formData = await req.formData();
        const file = formData.get("file");
        let namefile = makeid(10)
        // 将文件数据读取为Buffer
        const fileData = await file.arrayBuffer();
        const buffer = Buffer.from(fileData);
        // 将缓冲区转换为Base64
        const base64Data = buffer.toString('base64');
        await decode(base64Data, { fname: "./public/upload/" + namefile, ext: "png" });
        return new Response(JSON.stringify({ success: '文件成功转换为Base64' }));
    } catch (err) {
        console.log(err);
        return new Response(JSON.stringify({ error: "服务器端错误!" }), {
            status: 500
        })
    }
}
英文:

I solved it for you، but I didn't use formidable scripts، but I first downloaded the file to base64، then created an image using the node-base64-image library، which works.

import { Buffer } from &#39;buffer&#39;;
import { decode } from &#39;node-base64-image&#39;;


function makeid(length = 10) {
    var result = &#39;&#39;;
    var characters = &#39;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789&#39;;
    var charactersLength = characters.length;
    for (var i = 0; i &lt; length; i++) {
        result += characters.charAt(Math.floor(Math.random() * charactersLength));
    }
    return result;
}

export async function POST(req) {
    try {
        const formData = await req.formData();
        const file = formData.get(&quot;file&quot;);
        let namefile = makeid(10)
        // Read the file data as a Buffer
        const fileData = await file.arrayBuffer();
        const buffer = Buffer.from(fileData);
        // Convert the buffer to Base64
        const base64Data = buffer.toString(&#39;base64&#39;);
        await decode(base64Data, { fname: &quot;./public/upload/&quot; + namefile, ext: &quot;png&quot; });
        return new Response(JSON.stringify({ success: &#39;File converted to Base64 successfully&#39; }));
    } catch (err) {
        console.log(err);
        return new Response(JSON.stringify({ error: &quot;Server Side Error !&quot; }), {
            status: 500
        })
    }
}

huangapple
  • 本文由 发表于 2023年4月17日 12:51:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031805.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定