我正在使用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的
1条答案
按热度按时间thigvfpy1#
你有没有可能找出是什么原因造成的?我也遇到了同样的错误。