Implement strict dependency management policies to prevent unauthorized package installations and ensure reproducible builds across environments.
# Always commit package-lock.json to version control
git add package-lock.json
git commit -m "Lock dependency versions for security"
# Configure npm for security-first installations
npm config set audit-level moderate
npm config set fund false
npm config set ignore-scripts true
# Pin critical dependencies to exact versions in package.json
# Edit package.json to remove ^ and ~ from version numbers
{
"dependencies": {
"lodash": "4.17.21",
"express": "4.18.2",
"helmet": "7.1.0"
},
"engines": {
"node": ">=22.9.0",
"npm": ">=10.8.0"
}
}
The ignore-scripts setting prevents malicious packages from running arbitrary code during installation. Pin exact versions for security-critical packages to prevent automatic updates that might introduce vulnerabilities.
Warning: Pinning versions means you won't automatically receive security patches. Set up automated dependency update tools like Dependabot to create pull requests for updates.
Enable GitHub's Dependabot by creating .github/dependabot.yml:
version: 2
updates:
- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "weekly"
open-pull-requests-limit: 5
Verification: Run npm config list to confirm your security settings. Dependabot should start creating pull requests for dependency updates within a week.