Repository navigation
Getting Started
The package is on npm and has no dependencies, so installing it adds one folder to node_modules.
npm install replace-string-at-positionIt needs Node 20 or later (engines is >=20). pnpm and Yarn were not tested.
The function is the default export and also a named export. Both names point to the same function.
import replaceStringAtPosition from 'replace-string-at-position';
import {replaceStringAtPosition as named} from 'replace-string-at-position';
console.log(replaceStringAtPosition('222', '2', '3', 1));
console.log(named === replaceStringAtPosition);prints:
232
true
require() returns the function itself, as 1.x did. It also carries .default and .replaceStringAtPosition, both pointing back to it, so destructuring and code compiled from import both work.
const replaceStringAtPosition = require('replace-string-at-position');
const {replaceStringAtPosition: named} = require('replace-string-at-position');
console.log(replaceStringAtPosition('222', '2', '3', 1));
console.log(named === replaceStringAtPosition, replaceStringAtPosition.default === replaceStringAtPosition);prints:
232
true true
import loads dist/index.mjs and require() loads dist/index.cjs. They are two copies of the same code, so a program that uses both gets two different function objects (API reference).
Bun 1.4.2 ran both files above unchanged, after npm install, with the same output. bun add was not tested.
Deno 2.9.6 imports it with the npm: prefix and needed no permissions:
import replaceStringAtPosition from 'npm:replace-string-at-position';
console.log(replaceStringAtPosition('222', '2', '3', 1));prints:
232
That run was in a folder where npm had already installed the package; Deno read it from node_modules.
Not tested. The builds use no Node or browser globals (the repository's package-shape test checks for process, Buffer, require, node: imports, window and document, and runs the CommonJS build in a bare vm context), so a bundler or an ES module CDN should be able to serve it. No page here shows a browser run.
The package ships dist/index.d.mts for import and dist/index.d.cts for require(). The CommonJS declaration uses export =, so the type of require() is the callable function with its two properties. Each setup below was compiled with --strict --target es2022 by TypeScript 7.0.2 (npm's latest on 2026-09-30), 6.0.3 and 5.9.3, and the compiled file was run with Node.
With "module": "nodenext" in an .mts file, or with "moduleResolution": "bundler":
import replaceStringAtPosition, {replaceStringAtPosition as named} from 'replace-string-at-position';
const result: string = replaceStringAtPosition('abc', 'b', 'X', 1);
console.log(result, named === replaceStringAtPosition);TypeScript 7.0.2, --module nodenext --moduleResolution nodenext: tsc exit 0
aXc true
TypeScript 6.0.3, --module nodenext --moduleResolution nodenext: tsc exit 0
aXc true
TypeScript 5.9.3, --module nodenext --moduleResolution nodenext: tsc exit 0
aXc true
TypeScript 7.0.2, --module esnext --moduleResolution bundler --noEmit: tsc exit 0
TypeScript 6.0.3, --module esnext --moduleResolution bundler --noEmit: tsc exit 0
TypeScript 5.9.3, --module esnext --moduleResolution bundler --noEmit: tsc exit 0
The bundler setup was type-checked only; a bundler would do the emitting.
In a CommonJS file under nodenext (.cts), import = require() works on every line:
import replaceStringAtPosition = require('replace-string-at-position');
console.log(replaceStringAtPosition('abc', 'b', 'X', 1), replaceStringAtPosition.default('abc', 'b', 'Y', 1));TypeScript 7.0.2, --module nodenext --moduleResolution nodenext: tsc exit 0
aXc aYc
TypeScript 6.0.3, --module nodenext --moduleResolution nodenext: tsc exit 0
aXc aYc
TypeScript 5.9.3, --module nodenext --moduleResolution nodenext: tsc exit 0
aXc aYc
So does a default import, which relies on esModuleInterop (on by default under nodenext):
import replaceStringAtPosition from 'replace-string-at-position';
console.log(replaceStringAtPosition('abc', 'b', 'X', 1));TypeScript 7.0.2, --module nodenext --moduleResolution nodenext: tsc exit 0
aXc
TypeScript 6.0.3, --module nodenext --moduleResolution nodenext: tsc exit 0
aXc
TypeScript 5.9.3, --module nodenext --moduleResolution nodenext: tsc exit 0
aXc
TypeScript 7 removed "moduleResolution": "node10" and "esModuleInterop": false; it refuses both even with ignoreDeprecations. The same default import as above, compiled with --module commonjs --moduleResolution node10:
TypeScript 7.0.2, --module commonjs --moduleResolution node10 --ignoreDeprecations 6.0: tsc exit 2
error TS5108: Option 'moduleResolution=node10' has been removed. Please remove it from your configuration.
TypeScript 6.0.3, --module commonjs --moduleResolution node10 --ignoreDeprecations 6.0: tsc exit 0
aXc
TypeScript 5.9.3, --module commonjs --moduleResolution node10: tsc exit 0
aXc
With esModuleInterop off, the forms such projects use are import = require() and import * as:
import replaceStringAtPosition = require('replace-string-at-position');
import * as namespace from 'replace-string-at-position';
console.log(replaceStringAtPosition('abc', 'b', 'X', 1), namespace('abc', 'b', 'Y', 1));TypeScript 7.0.2, --module commonjs --moduleResolution node10 --esModuleInterop false --ignoreDeprecations 6.0: tsc exit 2
error TS5108: Option 'esModuleInterop=false' has been removed. Please remove it from your configuration.
error TS5108: Option 'moduleResolution=node10' has been removed. Please remove it from your configuration.
TypeScript 6.0.3, --module commonjs --moduleResolution node10 --esModuleInterop false --ignoreDeprecations 6.0: tsc exit 0
aXc aYc
TypeScript 5.9.3, --module commonjs --moduleResolution node10 --esModuleInterop false: tsc exit 0
aXc aYc
The errors come from the compiler options, not from this package: move such a project to nodenext or bundler before upgrading TypeScript to 7.
The declarations type every argument, so passing a string as the position fails to compile:
import replaceStringAtPosition from 'replace-string-at-position';
replaceStringAtPosition('abc', 'b', 'X', '1');TypeScript 7.0.2, --module nodenext --moduleResolution nodenext: tsc exit 2
index.mts(3,42): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
In JavaScript the same call throws a TypeError at run time (Edge cases and errors).
Next: API reference for the exact contract, or Recipes for the usual ways to get a position.
This wiki describes replace-string-at-position 2.0.0 and was last updated on 2026-09-30. The library is MIT licensed. Report problems in the issues. On npm: replace-string-at-position.
Using it
The releases
Contributing
Elsewhere