Obfuscate a Node.js program
Requires Codefend 3.3.0 or later for the recommended m_ preset.
This example uses Codefend's CLI to rename identifiers in a small CommonJS program. Node.js runs on your computer; Codefend produces another source file for Node.js to execute.
Use Node.js 18 or later for Codefend. This is the core package's runtime requirement; documentation tooling and bundlers can require newer Node.js versions.
1. Create the source
Start in a new project directory:
npm init -y
npm install --save-dev codefendCreate source/index.cjs:
const m_greeting = "Hello";
function m_fullName(m_firstName, m_lastName) {
return `${m_firstName} ${m_lastName}`;
}
console.log(`${m_greeting}, ${m_fullName("Ada", "Lovelace")}!`);Expected obfuscated source
With the configuration in the next step, Codefend produces:
const Ox0 = "Hello";
function Ox1(Ox2, Ox3) {
return `${Ox2} ${Ox3}`;
}
console.log(`${Ox0}, ${Ox1("Ada", "Lovelace")}!`);The program should still print Hello, Ada Lovelace!. Generated names can change with the source or Codefend version.
The m_ convention marks the identifiers we own. console.log is a Node.js API and stays unchanged. The .cjs extension keeps the example's CommonJS format explicit.
2. Configure a separate output
Create .codefendrc.json in the project root, alongside source:
{
"generation": {
"inputDir": "source",
"outputDir": "release"
},
"parser": {
"name": "startWithMAndUnderscore"
}
}startWithMAndUnderscore selects the explicit m_ convention and also applies to paths. The plain index.cjs filename does not match that rule, so this example preserves it. The default empty pool uses generated prefix-and-sequence names. release is a disposable sibling of source: it must contain no original files or other valuable data because Codefend removes an existing output directory before writing.
3. Compare the result
Run these commands from the project root:
node source/index.cjs
npx codefend --obfuscate
node release/index.cjsBoth executions should print:
Hello, Ada Lovelace!The output remains named index.cjs; its selected identifiers change. Treat generated identifier numbers as implementation details, rather than an API contract.
For a larger application, verify imports, public exports, string-key references, and runtime behavior. Keep binary assets outside a CLI input tree: current Codefend reads copied files as text. See troubleshooting, or use the Vite, esbuild, Rollup, or Webpack adapter for a bundler-based project.
Add Codefend to your workflow
Add an npm script to the existing project so local development and CI use the same installed version:
{
"scripts": {
"obfuscate": "codefend --obfuscate",
"start:generated": "node release/index.cjs"
}
}Run npm run obfuscate, then test the generated program before packaging it. In CI, install locked dependencies with npm ci and run the same script. For an application that compiles or bundles source, place obfuscation at the appropriate build step and run the tests against the generated result. See build and deployment.