簡體   English   中英

當部署到 Render.com 服務器時,`replaceAll` 在 Next.js (Node.js) 中不起作用

[英]`replaceAll` is not working in Next.js (Node.js) when deployed to Render.com server

我的代碼都在本地工作(運行節點 17.4.0)。

但正如我在這里提到的,當我在 Render.com 上部署到我的生產服務器時,它顯示Detected Node version 17.4.0 (與我用於本地開發的版本相同),函數會拋出如下錯誤:

  • TypeError: (0 , crypto__WEBPACK_IMPORTED_MODULE_0__.randomUUID)(...).replaceAll is not a function
  • TypeError: (intermediate value).format(...).replaceAll is not a function at getShortLocalizedDate

如何確保replaceAll可以在我的生產服務器上運行?

PS 我認為 Node 從v15開始就支持replaceAll

需要檢查的一些事項:您是否可能有其他內容覆蓋了engines.node中設置的Node 版本? 根據https://render.com/docs/node-versionengines.node值將被覆蓋

  1. NODE_VERSION環境變量
  2. repo 根目錄下的.node-version文件
  3. 一個.nvmrc文件位於你的 repo 的根目錄。

此外,當您在 Render 上部署時,您是否選擇 Node 作為Environment

這是我創建的 Render 的一個小型測試部署,用於驗證奇數 Node 版本號在 Render 上是否有效,並驗證replaceAll()在 Node v17.4.0 中是否有效。

代碼(也在https://github.com/crcastle/test-replaceall

server.js

const http = require('http')


const requestListener = function (req, res) {
  const orig = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';

  const monkey = orig.replaceAll('dog', 'monkey');
  // expected output: "The quick brown fox jumps over the lazy monkey. If the monkey reacted, was it really lazy?"
  
  // global flag required when calling replaceAll with regex
  const regex = /Dog/ig;
  const ferret = orig.replaceAll(regex, 'ferret');
  // expected output: "The quick brown fox jumps over the lazy ferret. If the ferret reacted, was it really lazy?"

  const version = process.version;

  res.writeHead(200);
  res.end(`Original: ${orig}\nMonkey: ${monkey}\nFerret: ${ferret}\nI am Node version ${version}`);
};

const HOST = "0.0.0.0";
const PORT = process.env.PORT || 10000;
const server = http.createServer(requestListener);
server.listen(PORT, HOST, () => {
  console.log(`Server is listening on http://${HOST}:${PORT}`);
});

package.json

{
  "name": "test-replaceall",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "engines": {
    "node": "17.4.0"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

輸出(也臨時部署到https://test-replaceall.onrender.com

Original: The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?
Monkey: The quick brown fox jumps over the lazy monkey. If the monkey reacted, was it really lazy?
Ferret: The quick brown fox jumps over the lazy ferret. If the ferret reacted, was it really lazy?
I am Node version v17.4.0

構建和部署日志

Jun 15 04:07:08 PM  ==> Cloning from https://github.com/crcastle/test-replaceall...
Jun 15 04:07:09 PM  ==> Checking out commit 17972cbecfdeafc0eb1c4a09cad07400ab5c8bc1 in branch main
Jun 15 04:07:25 PM  ==> Detected Node version 17.4.0
Jun 15 04:07:26 PM  ==> Running build command 'npm i'...
Jun 15 04:07:27 PM  up to date, audited 1 package in 297ms
Jun 15 04:07:27 PM  found 0 vulnerabilities
Jun 15 04:07:43 PM  ==> Uploading build...
Jun 15 04:07:49 PM  ==> Build successful 🎉
Jun 15 04:07:49 PM  ==> Deploying...
Jun 15 04:08:25 PM  ==> Detected Node version 17.4.0
Jun 15 04:08:25 PM  ==> Starting service with 'node server.js'
Jun 15 04:08:25 PM  Server is listening on http://0.0.0.0:10000

https://render.com/docs/node-version似乎不完整。

我想知道 Render 是否忽略奇數節點版本。

我在環境變量和package.json中指定了 17.4.0 ,但是我仍然會看到部署日志說“檢測到節點版本 18.3.0”,然后replaceAll莫名其妙地仍然不起作用。

最近我在環境變量中指定NODE_VERSION=18.3.0 ,我什至在package.json中添加了: "engines": { "node": ">=18.3.0 <19" }, https://dashboard.render.com/web/srv-caevvv7ho1kse31neb0g/deploys/dep-cal3r7vh8vl4d0g8vujg的最新部署日志再次表示: Jun 15 04:16:34 PM ==> Detected Node version 18.3.0

這一次replaceAll有效!

所以我的解釋是:

  1. 無法依賴部署日志中顯示的Detected Node version ___
  2. 使用https://render.com/docs/node-version上的方法指定節點版本可能不起作用,除非它們的主要版本號是偶數(例如 18)。

這很神秘。 但我很高興我的代碼現在可以工作了!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM