This is @ryusei__46.
While "Electron" has become the de facto desktop app development framework in recent times, its inability to obfuscate or encrypt source code means that despite its capability for creating modern applications, many developers still choose to avoid Electron for desktop app development.
However, using the "NW.js" framework—which shares the same foundational architecture as Electron—allows you to obfuscate your source code.
This article provides a simple, practical guide to achieving this obfuscation method.
Development Environment
- OS:Windows 11 Pro
- node.js v 18.13
- npm v 6.19.1
- nw.js v 0.72.0
Compiling source code (converting to binary)
To compile source code, you'll need the official "nwjc" compiler. This compiler can only be used after installing the development version package (SDK) of nw.js via npm.
npm install nw --nwjs_build_type=sdk -gThe command above will install the SDK version of nw.js globally. If you haven't set the path in your environment variables, use the "npm root -g" command to locate the path to the "node_module" folder and add it.
To compile JavaScript files using the "nwjc" command,
nwjc [Path to the file you want to compile] [Compiled file name]For compiled file extensions, ".bin" is generally a safe choice. Since these are binary files, the extension itself doesn't really matter.
To load the compiled binary file, you can use:
nw.Window.get().evalNWBin(null, 'Path to compiled file');method. When distributing the app, place the script tag containing the JavaScript code within the html file to execute the JavaScript processing.
<!-- index.html -->
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>code test</title>
<script defer>
nw.Window.get().evalNWBin(null, 'main.bin');
</script>
</head>
<body>
<h2>Hello World.</h2>
</body>
</html>Compiling Module (Binary Conversion)
As development scales up, you'll likely find yourself splitting files into modules for better management. In such cases, you can also compile source files as modules for inclusion.
The ".evalNWBin()" method described in the previous section does not support module loading, so it cannot be used. To compile source files as modules for loading, simply include the "–nw-module" option when running the "nwjc" command.
nwjc [Path to the file you want to compile] [Compiled file name] --nw-moduleAfter compilation, you can load the module file using:
nw.Window.get().evalNWBinModule(null, 'module file path', 'module name');
The third argument, "module name", can be any string you specify. This specified module name will be used for loading, allowing you to import it using "require()" or "import" with the path "./[specified module name]".
// main.js ≡ main.bin
nw.Window.get().evalNWBinModule(null, 'module.bin', 'modulle');
const test_module = require('./module');
console.log(test_module.sum());// module.js ≡ module.bin
module.exports = {
number: 150,
sum: function() {
return this.number + 500;
}
}Reference Page

How to use nwjc, Tried to use it from command line from the project path, but getting nwjc: command not found. Do I need to download anything more than nwbuilder and nw for using nwjc?