繁体   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