【问题标题】:Functions is not deployed correctly with an error saying that 'could not find a valid build'函数未正确部署,错误提示“找不到有效的构建”
【发布时间】:2020-06-23 10:22:36
【问题描述】:

正如标题所说,目前我无法从我的终端部署功能。我想正常部署功能。看来 Next 会碍事。

我的项目文件夹目录结构如下:

- root 
  - dist 
  - src 
     - app
     - functions <- I only want to deploy this folder to Cloud Functions
     - public
  - node_modules
  - package.json 
  - jest.config.js
  - firebaserc

  And other setting files are managed here

这是 firebase.json

{
  ....

  "functions": {
    "predeploy": [
      "npm run lint",
      "npm run build"
    ],
    "source": "."
  },

  ...
}

这是我的 package.json

{
"name": "functions",
  "scripts": {
    "dev": "NODE_ENV=development next src/app",
    "clean": "rimraf dist",
    "build:app": "npm run clean && NODE_ENV=development next build src/app",
    "build:app:prod": "npm run clean && NODE_ENV=production next build src/app",
    "start:app": "next start src/app",
    "lint": "tslint --project src/functions/tsconfig.json",
    "build": "tsc --project src/functions",
    "serve": "npm run build && firebase emulators:start --only functions,hosting",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "npm run build && firebase deploy --only functions",
    "deploy:hosting": "firebase deploy --only functions:hosting",
    "logs": "firebase functions:log"
  },
"main": "dist/functions/index.js",
"dependencies": {.....}
}

这是执行脚本时下面控制台中的错误消息所说的内容:

Running command: npm run build

> functions@ build /Users/{user}/Desktop/{project}
> tsc --project src/functions

✔  functions: Finished running predeploy script.
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
✔  functions: required API cloudbuild.googleapis.com is enabled
✔  functions: required API cloudfunctions.googleapis.com is enabled
i  functions: preparing . directory for uploading...

Error: Error occurred while parsing your function triggers.

Error: Could not find a valid build in the '/Users/{user}/Desktop/{project}/dist/functions/next' directory! Try building your app with 'next build' before starting the server.

我不知道为什么会出现这个错误?我在这里做错了什么?配置/json文件有问题吗??

【问题讨论】:

  • 你在构建脚本中使用过next build吗?你能显示完整的 package.json 吗?
  • 更新了帖子。我基本上使用了npm run deploy的脚本。我执行时不使用next build
  • Nextjs 要求您使用 next build,因为它会创建项目的构建版本,然后您可以部署它。
  • 感谢您的快速解释。 next build src/functions 将在 dist 文件夹中创建一个构建版本,然后它就可以部署了?
  • 点击next build src/functions 会在下一个Couldn't find a `pages` directory. Please create one under the project root 出错,这可能是什么问题?

标签: reactjs next.js


【解决方案1】:

我建议你先阅读官方的nextjs tutorial

您使用 nextjs 以便您的网站页面在服务器而不是客户端中生成,在构建时 (SSG) 或请求时 (SSR) 预渲染页面,这样做主要是为了使网站 SEO 友好。这就是为什么您需要在根目录中包含所有页面的 pages 目录。

你的脚本也需要修改,至少应该有如下内容:

"scripts": { 
    "dev": "next",
    "build": "next build",
    "start": "next start" 
},

这样,当您执行npm build 时,nextjs 将构建您项目的“构建版本”,您可以稍后部署。

【讨论】:

  • 我想将功能部署到 Cloud Functions 而不是应用程序本身。仍然需要next build 来获得它的构建版本吗?
猜你喜欢
  • 2021-04-10
  • 2017-10-19
  • 2021-10-08
  • 1970-01-01
  • 2020-07-30
  • 2014-06-18
  • 1970-01-01
  • 2017-01-18
  • 2023-01-05
相关资源
最近更新 更多