Netlify 说,“错误 Gatsby 需要 Node.js 14.15.0 或更高版本(您有 v12.18.0)”——但我有最新的 Node 版本?

Mag*_*tad 11 node.js gatsby netlify

从 Remark 迁移到 MDX 后,我在 Netlify 上的构建失败了。

\n

尝试构建时出现此错误:

\n
10:13:28 AM: $ npm run build\n10:13:29 AM: > blog-gatsby@0.1.0 build /opt/build/repo\n10:13:29 AM: > gatsby build\n10:13:30 AM: error Gatsby requires Node.js 14.15.0 or higher (you have v12.18.0).\n10:13:30 AM: Upgrade Node to the latest stable release: https://gatsby.dev/upgrading-node-js\n
Run Code Online (Sandbox Code Playgroud)\n

node -v然而,当我在终端中运行时,它显示 v17.2.0。

\n

我认为迁移后发生这种情况并非巧合。问题可能是因为我的节点模块文件夹吗?或者我的 gatsby-config.js 或 package.json 文件中是否有某些内容需要更改?

\n

我的 package.json 文件:

\n
{\n  "name": "blog-gatsby",\n  "private": true,\n  "description": "A starter for a blog powered by Gatsby and Markdown",\n  "version": "0.1.0",\n  "author": "Magnus Kolstad <kolstadmagnus@gmail.com>",\n  "bugs": {\n    "url": "https://kolstadmagnus.no"\n  },\n  "dependencies": {\n    "@mdx-js/mdx": "^1.6.22",\n    "@mdx-js/react": "^1.6.22",\n    "gatsby": "^4.3.0",\n    "gatsby-plugin-feed": "^4.3.0",\n    "gatsby-plugin-gatsby-cloud": "^4.3.0",\n    "gatsby-plugin-google-analytics": "^4.3.0",\n    "gatsby-plugin-image": "^2.3.0",\n    "gatsby-plugin-manifest": "^4.3.0",\n    "gatsby-plugin-mdx": "^3.4.0",\n    "gatsby-plugin-offline": "^5.3.0",\n    "gatsby-plugin-react-helmet": "^5.3.0",\n    "gatsby-plugin-sharp": "^4.3.0",\n    "gatsby-remark-copy-linked-files": "^5.3.0",\n    "gatsby-remark-images": "^6.3.0",\n    "gatsby-remark-prismjs": "^6.3.0",\n    "gatsby-remark-responsive-iframe": "^5.3.0",\n    "gatsby-remark-smartypants": "^5.3.0",\n    "gatsby-source-filesystem": "^4.3.0",\n    "gatsby-transformer-sharp": "^4.3.0",\n    "prismjs": "^1.25.0",\n    "react": "^17.0.1",\n    "react-dom": "^17.0.1",\n    "react-helmet": "^6.1.0",\n    "typeface-merriweather": "0.0.72",\n    "typeface-montserrat": "0.0.75"\n  },\n  "devDependencies": {\n    "prettier": "^2.4.1"\n  },\n  "homepage": "https://kolstadmagnus.no",\n  "keywords": [\n    "blog"\n  ],\n  "license": "0BSD",\n  "main": "n/a",\n  "repository": {\n    "type": "git",\n    "url": "git+https://github.com/gatsbyjs/gatsby-starter-blog.git"\n  },\n  "scripts": {\n    "build": "gatsby build",\n    "develop": "gatsby develop",\n    "format": "prettier --write \\"**/*.{js,jsx,ts,tsx,json,md}\\"",\n    "start": "gatsby develop",\n    "serve": "gatsby serve",\n    "clean": "gatsby clean",\n    "test": "echo \\"Write tests! -> https://gatsby.dev/unit-testing\\" && exit 1"\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我在这里做错了什么?

\n
\n

更新#1

\n
7:11:59 PM: failed Building production JavaScript and CSS bundles - 20.650s\n7:11:59 PM: error Generating JavaScript bundles failed\n7:11:59 PM: Module build failed (from ./node_modules/url-loader/dist/cjs.js):\n7:11:59 PM: Error: error:0308010C:digital envelope routines::unsupported\n7:11:59 PM:     at new Hash (node:internal/crypto/hash:67:19)\n7:11:59 PM:     at Object.createHash (node:crypto:130:10)\n7:11:59 PM:     at getHashDigest (/opt/build/repo/node_modules/file-loader/node_modules/loader-utils/lib/getHashDigest.js:46:34)\n7:11:59 PM:     at /opt/build/repo/node_modules/file-loader/node_modules/loader-utils/lib/interpolateName.js:113:11\n7:11:59 PM:     at String.replace (<anonymous>)\n7:11:59 PM:     at interpolateName (/opt/build/repo/node_modules/file-loader/node_modules/loader-utils/lib/interpolateName.js:110:8)\n7:11:59 PM:     at Object.loader (/opt/build/repo/node_modules/file-loader/dist/index.js:29:48)\n7:11:59 PM:     at Object.loader (/opt/build/repo/node_modules/url-loader/dist/index.js:127:19)\n7:11:59 PM: \xe2\x80\x8b\n7:11:59 PM: \xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n7:11:59 PM:   "build.command" failed                                        \n7:11:59 PM: \xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n7:11:59 PM: \xe2\x80\x8b\n7:11:59 PM:   Error message\n7:11:59 PM:   Command failed with exit code 1: npm run build\n7:11:59 PM: \xe2\x80\x8b\n7:11:59 PM:   Error location\n7:11:59 PM:   In Build command from Netlify app:\n7:11:59 PM:   npm run build\n7:11:59 PM: \xe2\x80\x8b\n7:11:59 PM:   Resolved config\n7:11:59 PM:   build:\n7:11:59 PM:     command: npm run build\n7:11:59 PM:     commandOrigin: ui\n7:11:59 PM:     publish: /opt/build/repo/public\n7:11:59 PM:     publishOrigin: ui\n7:11:59 PM:   plugins:\n7:11:59 PM:     - inputs: {}\n7:11:59 PM:       origin: ui\n7:11:59 PM:       package: '@netlify/plugin-gatsby'\n7:11:59 PM:   redirects:\n7:12:00 PM:     - from: /api/*\n      status: 200\n      to: /.netlify/functions/gatsby\n    - force: true\n      from: https://magnuskolstad.com\n      status: 301\n      to: https://kolstadmagnus.no\n  redirectsOrigin: config\nCaching artifacts\n
Run Code Online (Sandbox Code Playgroud)\n

Fer*_*reu 22

问题是您的 Node 版本为 17.2.0。在本地,但在 Netlify 的环境中,您运行的是较低版本(默认情况下未设置为 17.2.0)。所以本地环境没问题,Netlify环境就KO了,就是因为Node版本不匹配。

当 Netlify 部署您的站点时,它会安装并再次构建您的依赖项,因此您应该确保两个环境在相同的条件下工作。否则,两者node_modules都会有所不同,因此您的应用程序将具有不同的行为,或者最终由于依赖性错误而无法构建。

您可以通过多种方式轻松使用 Node 版本,但我建议使用该.nvmrc文件。只需在项目的根目录中运行以下命令:

node -v > .nvmrc
Run Code Online (Sandbox Code Playgroud)

这应该创建一个.nvmrc包含 Node 版本 ( node -v) 的文件。当 Netlify 在构建过程中找到此文件时,它会将其用作基本 Node 版本,以便相应地安装所有依赖项。

该文件还可用于告诉其他贡献者您正在使用哪个 Node 版本,并使用nvm use命令与 NVM 一起工作。

  • 这解决了 Cloudflair Pages 中的相同问题! (2认同)