replace-in-file
replace-in-file copied to clipboard
replace is not working with path.join
index.js
const re = require('replace-in-file');
const path = require('path');
const fs = require('fs');
function replaceFoo() {
const indexHtmlPath = path.join(__dirname, 'index.html');
const isIndexHtmlPathExist = fs.existsSync(indexHtmlPath);
console.log(isIndexHtmlPathExist, 'isIndexHtmlPathExist')
const option = {
files: indexHtmlPath, // If i put hardcoded 'index.html' then it is work fine fine
from: /foo/g,
to: 'bar',
countMatches: true,
};
console.log(option);
const output = re.sync(option);
console.log(output);
}
replaceFoo()
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
foo
</body>
</html>
Node -v
v14.18.1
replace-in-file version
"replace-in-file": "^7.1.0"
I don't know the exact cause but this "replace-in-file": "^6.3.5" is working fine but not with 7x
Hi, not sure, are the files are passed to Glob, could be something with the newer version of Glob that is used in 7.x.
I suggest perhaps trying the disableGlobs setting or specifically the windowsPathsNoEscape setting.
- https://github.com/adamreisnz/replace-in-file?tab=readme-ov-file#specify-glob-configuration
- https://github.com/adamreisnz/replace-in-file?tab=readme-ov-file#specify-glob-configuration
Feel free to reopen if those don't help and if you can pin point an error in this library.
Yes indeed it is working fine with disableGlobs: true or
glob: {
//Glob settings here (examples given below)
dot: true, //To include file names starting with a dot
windowsPathsNoEscape: true, //To fix paths on Windows OS when path.join() is used to create paths
},