![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
strip-comments-strings
Advanced tools
Strip or extract comments, strings or both from source code.
npm install strip-comments-strings
const {parseString, stripStrings, stripComments} = require("strip-comments-strings");
or
import {parseString, stripStrings, stripComments} from "strip-comments-strings"
📝 example.js ↴
/** // **/
/** 1 **/
//aaaa
/** 2 **/
//bbbbb
//ccccc
///////////
/** 3 **/
/** 4 **/
/** 5 **/
""
aa
"attenti\"on"
bb
ccc
'warni\\'
'ng\''
ddd
eeee`info`
eeee
fffff
"silence"
fffff
gggggg
'noise'
gggggg
hhhhhhh`visibility`
hhhhhhh
"aaa"
"aaa"
"aaa"
let str = fs.readFileSync(path.join(__dirname, "./example.js"), "utf-8");
const stripMyComments = (str) =>
{
let counter = 0
str = stripComments(str, function (info)
{
return `(${counter++})`
}, {includeDelimiter: false});
return str
}
console.log(str)
The code above replaces all comments with ({index})
(9)
(8)
(7)(6)
(5)(4)(3)(2)
(1)
(0)
""
aa
"attenti\"on"
bb
ccc
'warni\\'
'ng\''
ddd
eeee`info`
eeee
fffff
"silence"
fffff
gggggg
'noise'
gggggg
hhhhhhh`visibility`
hhhhhhh
"aaa"
"aaa"
"aaa"
let str = fs.readFileSync(path.join(__dirname, "./example-6.js"), "utf-8");
str = stripStrings(str, function (info)
{
return "**"
}, {includeDelimiter: true});
The code above replaces all strings with two "!!"
/** // **/
/** 1 **/
//aaaa
/** 2 **/
//bbbbb
//ccccc
///////////
/** 3 **/
/** 4 **/
/** 5 **/
!!
aa
!!
bb
ccc
!!
!!
ddd
eeee!!
eeee
fffff
!!
fffff
gggggg
!!
gggggg
hhhhhhh!!
hhhhhhh
!!
!!
!!
// ...
str = stripMyComments(str)
str = stripMyStrings(str)
// ...
It is best to always start by removing comments then strings.
const found = parseString(str);
console.log( found );
{
text: '',
strings: [
{ content: '', index: 107, indexEnd: 107, item: [Object] },
{
content: 'attenti\\"on',
index: 115,
indexEnd: 126,
item: [Object]
},
{ content: 'warni\\\\', index: 139, indexEnd: 146, item: [Object] },
{ content: "ng\\'", index: 150, indexEnd: 154, item: [Object] },
{ content: 'info', index: 167, indexEnd: 171, item: [Object] },
{ content: 'silence', index: 188, indexEnd: 195, item: [Object] },
{ content: 'noise', index: 214, indexEnd: 219, item: [Object] },
{
content: 'visibility',
index: 238,
indexEnd: 248,
item: [Object]
},
{ content: 'aaa', index: 261, indexEnd: 264, item: [Object] },
{ content: 'aaa', index: 268, indexEnd: 271, item: [Object] },
{ content: 'aaa', index: 275, indexEnd: 278, item: [Object] }
],
comments: [
{
type: 'commentBlock',
content: '/** // **/',
index: 0,
indexEnd: 10
},
{
type: 'commentBlock',
content: '/** 1 **/',
index: 12,
indexEnd: 21
},
{
type: 'commentLine',
content: '//aaaa\r\n',
index: 23,
indexEnd: 31
},
{
type: 'commentBlock',
content: '/** 2 **/',
index: 31,
indexEnd: 40
},
{
type: 'commentLine',
content: '//bbbbb\r\n',
index: 42,
indexEnd: 51
},
{
type: 'commentLine',
content: '//ccccc\r\n',
index: 51,
indexEnd: 60
},
{
type: 'commentLine',
content: '///////////\r\n',
index: 60,
indexEnd: 73
},
{
type: 'commentBlock',
content: '/** 3 **/',
index: 73,
indexEnd: 82
},
{
type: 'commentBlock',
content: '/** 4 **/',
index: 84,
indexEnd: 93
},
{
type: 'commentBlock',
content: '/** 5 **/',
index: 95,
indexEnd: 104
}
]
}
FAQs
Strip or extract comments, strings or both from source code.
The npm package strip-comments-strings receives a total of 148,329 weekly downloads. As such, strip-comments-strings popularity was classified as popular.
We found that strip-comments-strings demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.