question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

title.replaceAll is not a function

See original GitHub issue

Relevant code or config:

const urlTitle: string = title.replaceAll(' ', '-');

What you did:

Testing a product list page

What happened:

TypeError: title.replaceAll is not a function

Problem description:

replaceAll function not being recognised, but replace function is fine

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
sethiddencommented, May 18, 2021

You can just use https://github.com/es-shims/String.prototype.replaceAll

  1. npm i string.prototype.replaceall -D

  2. Make sure you have the setupFilesAfterEnv property in your Jest config. I use package.json so there I have:

{
  "jest": {
   //...other options here obviously
    "setupFilesAfterEnv": ["./src/utils/jestSetup.ts"]
  }
}

jestSetup.ts

import '@testing-library/jest-dom';
//@ts-ignore
import replaceAllInserter from 'string.prototype.replaceall';

replaceAllInserter.shim();
2reactions
ManhNV103commented, Jan 17, 2022

The quickest fix is to use replace with regex.

const urlTitle: string = title.replace(/ /g, '-');
Read more comments on GitHub >

github_iconTop Results From Across the Web

Solved: "replaceAll is not a function" JavaScript TypeError
If you're seeing the error " TypeError: replaceAll is not a function ", it is likely due to the method not implemented/supported by...
Read more >
Javascript .replaceAll() is not a function type error
When I run this, I receive Uncaught TypeError: string. replaceAll is not a function . Maybe I'm misunderstanding what a prototype is, but...
Read more >
TypeError: replaceAll is not a function in JavaScript | bobbyhadz
The "replaceAll" is not a function error occurs when we call the replaceAll() method on a value that is not of type string,...
Read more >
[Solved] TypeError: replaceAll is not a function - ItsJavaScript
The TypeError: replaceAll is not a function occurs if we call a replaceAll() method on the value that is not of a type...
Read more >
Fix replaceAll is not a function in Node JS - Techozu
Easily fix the “replaceAll” is not a function error in NodeJS with the following solutions. ... The replace function is one of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found