简体   繁体   中英

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

My code all works locally (running Node 17.4.0).

But as I mentioned here , when I deploy to my production server at Render.com, which says Detected Node version 17.4.0 (which is the same version that I use for local development), functions throw errors like:

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

How can I ensure that replaceAll can work on my production server?

PS I think Node has supported replaceAll since v15 .

Some things to check: Do you maybe have something else overriding the Node version set in engines.node ? According to https://render.com/docs/node-version , the engines.node value will be overridden by

  1. A NODE_VERSION environment variable
  2. A .node-version file at the root of your repo
  3. A .nvmrc file at the root of your repo.

Also, when you deploy on Render, are you selecting Node as the Environment ?

Here's a small test deploy to Render I created to verify that odd Node version numbers work on Render and also to verify that replaceAll() works in Node v17.4.0.

Code (also at 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"
}

Output (also temporarily deployed to 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

Build and deploy log

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 seems incomplete.

I wonder if Render ignores odd-numbered Node versions.

I'd specified 17.4.0 in the environment variables and also in package.json , but then I'd still see the deployment log say "Detected Node version 18.3.0", and then replaceAll inexplicably still wouldn't work.

More recently I've specified NODE_VERSION=18.3.0 in the environment variables, and I've even added to package.json : "engines": { "node": ">=18.3.0 <19" }, . The latest deployment log at https://dashboard.render.com/web/srv-caevvv7ho1kse31neb0g/deploys/dep-cal3r7vh8vl4d0g8vujg again said: Jun 15 04:16:34 PM ==> Detected Node version 18.3.0 .

And this time replaceAll works!

So my interpretation is:

  1. The Detected Node version ___ shown in deployment logs cannot be relied upon.
  2. Specifying a Node version using methods at https://render.com/docs/node-version might not work unless their major version number is even (such as 18).

It's mysterious. But I'm glad my code is working now!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM