当部署到 Render.com 服务器时,“replaceAll”在 Next.js (Node.js) 中不起作用

Rya*_*yan 1 javascript node.js next.js

我的代码全部在本地运行(运行 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可以在我的生产服务器上运行?

PS 我认为 NodereplaceAllv15开始就支持了。

crc*_*tle 5

需要检查的一些事项:您是否可能有其他内容覆盖了中设置的 Node 版本engines.node?根据https://render.com/docs/node-version,该engines.node值将被覆盖

  1. 环境NODE_VERSION变量
  2. .node-version存储库根目录下的文件
  3. .nvmrc存储库根目录下的文件。

另外,当您在 Render 上部署时,您是否选择 Node 作为环境

这是我创建的一个小型 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}`);
});
Run Code Online (Sandbox Code Playgroud)

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"
}
Run Code Online (Sandbox Code Playgroud)

输出(也临时部署到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
Run Code Online (Sandbox Code Playgroud)

构建和部署日志

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
Run Code Online (Sandbox Code Playgroud)