Hi. Just to let you know I had to add a try catch to your script because the resolve module has a test that checks for a bad package.json file - and so it has a bad package.json file to test against - so it cannot be parsed
// Function to check dependencies in package.json
function checkPackageJson(badPackages, filePath) {
if (!fs.existsSync(filePath)) return;
console.log(Checking ${filePath}...);
try {
const pkgJson = JSON.parse(fs.readFileSync(filePath, 'utf8'));
const allDeps = Object.assign({}, pkgJson.dependencies || {}, pkgJson.devDependencies || {}, pkgJson.optionalDependencies || {});
for (const [pkg, version] of Object.entries(allDeps)) {
const cleanVersion = version.replace(/^[^\d]*/, '').split(' ')[0];
if (badPackages.has(pkg) && badPackages.get(pkg).has(cleanVersion)) {
foundCompromised = true;
console.log(\x1b[31mCompromised in package.json: ${pkg}@${cleanVersion}\x1b[0m);
}
}
} catch (err) {
console.error(\x1b[33mFailed to parse ${filePath}: ${err.message}\x1b[0m);
}
}
Hi. Just to let you know I had to add a try catch to your script because the resolve module has a test that checks for a bad package.json file - and so it has a bad package.json file to test against - so it cannot be parsed
// Function to check dependencies in package.json
function checkPackageJson(badPackages, filePath) {
if (!fs.existsSync(filePath)) return;
console.log(
Checking ${filePath}...);try {
const pkgJson = JSON.parse(fs.readFileSync(filePath, 'utf8'));
const allDeps = Object.assign({}, pkgJson.dependencies || {}, pkgJson.devDependencies || {}, pkgJson.optionalDependencies || {});
for (const [pkg, version] of Object.entries(allDeps)) {
const cleanVersion = version.replace(/^[^\d]*/, '').split(' ')[0];
if (badPackages.has(pkg) && badPackages.get(pkg).has(cleanVersion)) {
foundCompromised = true;
console.log(
\x1b[31mCompromised in package.json: ${pkg}@${cleanVersion}\x1b[0m);}
}
} catch (err) {
console.error(
\x1b[33mFailed to parse ${filePath}: ${err.message}\x1b[0m);}
}