我的朋友们,我是一个初学者,我正在尝试构建我的使用 Node 构建的项目,但我不知道构建脚本。我搜索了很多但没有找到解决方案。谢谢你
package.json
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"engines":{
"node":"16.x"
} ,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"cookie-parser": "^1.4.6",
"dotenv": "^16.0.2",
"express": "^4.18.1",
"jsonwebtoken": "^8.5.1",
"mongoose": "^6.5.4",
"nodemon": "^2.0.19"
}
}
我想说您遇到的错误与缺少构建脚本(错误 - Vercel 文档)中描述的相同。
显然,当您的项目根目录中有一个
package.json
文件,但没有 api
目录且没有 vercel.json
配置时,您会收到此错误消息。
他们建议将您的
package.json
文件设置为与此类似的内容:
{
"scripts": {
"build": "[my-framework] build --output public"
}
}
但是,目前还不可能直接使用 Vercel 托管服务器运行的 Node.js Web 应用程序。
Vercel 是一个用于静态前端和无服务器功能的云平台。
为了使用 Vercel 部署 Node.js API,您需要使用其 无服务器函数 或使用 Node.js 帮助程序。
我遇到了完全相同的问题,解决方法是使用以下配置在根目录中创建一个
vercel.json
文件:
{
"version": 2,
"builds": [
{
"src": "server.js",
"use": "@now/node"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "server.js"
}
]
}
这可以避免在简单项目中使用
/api
文件夹,并告诉 vercel 将此 API 作为无服务器函数提供。
参考:https://dev.to/tirthpatel/node-js-express-app-on-vercel-develop-run-deploy-524a
原始 Vercel 指南:https://vercel.com/guides/using-express-with-vercel