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

Time:06-16

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?

P.S. I think Node has supported replaceAll since v15.

CodePudding user response:

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            
  • Related