【问题标题】:Netlify says, "error Gatsby requires Node.js 14.15.0 or higher (you have v12.18.0)"—yet I have the newest Node version?Netlify 说,“错误 Gatsby 需要 Node.js 14.15.0 或更高版本(你有 v12.18.0)”——但我有最新的 Node 版本?
【发布时间】:2022-01-18 14:00:58
【问题描述】:

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

我在尝试构建时收到此错误:

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

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

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

我的 package.json 文件:

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

我在这里做错了什么?


更新 #1

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

【问题讨论】:

  • 你在使用任何节点版本管理器吗?
  • 对不起,我不知道。假设不了解 JS 或 Node。
  • 到目前为止,我只使用了 Gatsby starter-blog-template。我从来没有做过任何不属于 Gatsby 设置教程的 Node。

标签: gatsby netlify


【解决方案1】:

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

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

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

node -v > .nvmrc

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

该文件还有助于告诉其他贡献者您正在使用哪个 Node 版本。

【讨论】:

  • 谢谢,这解决了它。然而现在我得到了这个错误——虽然在构建过程中更进一步,很明显。 (请参阅原始帖子的编辑。)
  • 这是一个全新的问题...尝试将两个 Node 版本都设置为最新的 LTS (16.13.1)。您可以使用 nvm 并手动更改 nvmrc
  • 这解决了 Cloudflair Pages 中的相同问题!! ?
猜你喜欢
  • 2018-03-22
  • 2019-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-07
  • 2015-10-24
  • 2019-03-14
相关资源
最近更新 更多