首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Next.js (Node.js)中部署到Render.com服务器时,“`replaceAll`”不工作

在Next.js (Node.js)中部署到Render.com服务器时,“`replaceAll`”不工作
EN

Stack Overflow用户
提问于 2022-06-15 19:40:44
回答 2查看 390关注 0票数 1

我的代码都在本地工作(运行Node 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能够在我的生产服务器上工作?

我认为Node从replaceAll开始就支持v15了。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-06-15 23:27:05

有些事情需要检查:您可能有其他东西覆盖engines.node中的Node版本集吗?根据https://render.com/docs/node-versionengines.node值将被

  1. 一个NODE_VERSION环境变量
  2. 回购的根部有一个.node-version文件
  3. 回购的根部有一个.nvmrc文件。

此外,在呈现上部署时,是否选择Node作为环境

下面是一个用于呈现我创建的用于验证奇数节点版本号是否适用于呈现的小测试部署,以及验证replaceAll()在Nodev17.4.0中工作的测试。

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

server.js

代码语言:javascript
复制
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

代码语言:javascript
复制
{
  "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)

代码语言:javascript
复制
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

构建和部署日志

代码语言:javascript
复制
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
票数 5
EN

Stack Overflow用户

发布于 2022-06-15 20:28:44

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

我想知道渲染是否忽略奇数节点版本。

我已经在环境变量和package.json中指定了17.4.0,但是我仍然会看到部署日志上写着“DetectedNodeVersion18.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中的方法指定Node版本可能不起作用,除非它们的主版本号是偶数(例如18)。

它是神秘的。但我很高兴我的代码现在起作用了!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72637010

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档