简体   繁体   English

Vercel Serverless function 在访问 req.body 时导致“找不到模块‘content-type’”

[英]Vercel Serverless function results in "Cannot find module 'content-type'" when accessing req.body

I'm using NextJS with Vercel Serverless as described in this answer - https://stackoverflow.com/a/63659707/2826679 .我将 NextJS 与 Vercel Serverless 一起使用,如此答案中所述 - https://stackoverflow.com/a/63659707/2826679 My NodeJS function fails with an error message when I'm running it locally using vercel dev or when it's deployed to vercel servers.当我使用vercel dev在本地运行它或将其部署到 vercel 服务器时,我的 NodeJS function 失败并显示一条错误消息。

My <PROJECT_ROOT>/api/report.ts looks like this and it fails at printing "3 req.body".我的<PROJECT_ROOT>/api/report.ts看起来像这样,它无法打印“3 req.body”。

import type { VercelRequest, VercelResponse } from '@vercel/node';

export default async function handler(req: VercelRequest, res: VercelResponse) {
  if (req.method === 'POST') {
    console.log("1")
    console.log("2", req)
    console.log("3", req.body)

    const data = {
      test: "test"
    };

    return res.status(200).setHeader('content-type', 'application/json').end(JSON.stringify(data));
  } else {
    return res.status(404);
  }
}

Error:错误:

Unhandled rejection: Error: Cannot find module 'content-type'
Require stack:
- /Users/piotr/.config/yarn/global/node_modules/@vercel/node-bridge/helpers.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1039:15)
    at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (/Users/piotr/.config/yarn/global/node_modules/@cspotcode/source-map-support/source-map-support.js:811:30)
    at Function.Module._load (node:internal/modules/cjs/loader:885:27)
    at Module.require (node:internal/modules/cjs/loader:1105:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.909 (/Users/piotr/.config/yarn/global/node_modules/@vercel/node-bridge/helpers.js:262:33)
    at __webpack_require__ (/Users/piotr/.config/yarn/global/node_modules/@vercel/node-bridge/helpers.js:328:42)
    at parseBody (/Users/piotr/.config/yarn/global/node_modules/@vercel/node-bridge/helpers.js:19:45)
    at IncomingMessage.get [as body] (/Users/piotr/.config/yarn/global/node_modules/@vercel/node-bridge/helpers.js:212:27)
    at handler (/Users/piotr/astrology/astroperspective/api/report.ts:7:26) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/piotr/.config/yarn/global/node_modules/@vercel/node-bridge/helpers.js'
  ]
}

EDIT: Now, I'm thinking that it has to do something with dependencies in package.json since my package.json is specifically for NextJS编辑:现在,我认为它必须对 package.json 中的依赖项做一些事情,因为我的 package.json 专门用于 NextJS

Were you by any chance able to find out what was causing this?您是否有机会找出造成这种情况的原因? I'm running into the same error as well.我也遇到了同样的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM