NodeJS 访问请求正文时,Vercel Serverless函数导致“找不到模块'content-type'”

6uxekuva  于 2023-01-20  发布在  Node.js
关注(0)|答案(1)|浏览(90)

我正在使用NextJS和Vercel Serverless,如答案https://stackoverflow.com/a/63659707/2826679中所述。当我使用vercel dev在本地运行NodeJS函数或将其部署到vercel服务器时,NodeJS函数失败并显示错误消息。
我的<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);
  }
}

错误:

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:**现在,我认为它必须处理package.json中的依赖关系,因为我的package.json是专门针对NextJS的
thigvfpy

thigvfpy1#

你有没有可能找出是什么原因造成的?我也遇到了同样的错误。

相关问题