出于各种原因,包括保护我的源代码以及只需右键单击即可执行而不是安装 Node、npm ci 等...我想将我的 Nodejs 应用程序打包为可执行文件。
根据我的研究,我发现
我正在尝试使用pkg
,因为第一次看到它后感觉更容易使用。但在一个沙盒项目来测试他的能力时,我遇到了一个问题。
考虑:
// /src/module.js
function bar(str) {
let tic = "another sensitive place";
console.log(str);
}
function foo() {
if (true) {
return "some really important business are done and the how should";
}
}
module.exports = {
bar,
foo,
};
// index.js
const mod = require("./src/module.js");
mod.bar(mod.foo());
以及package.json
中的pkg配置
{
// many lines ommited
"bin": "index.js",
"pkg": {
"scripts": [
"src/module.js",
"index.js"
],
"targets": [
"node16-win-x64"
],
"outputPath": "bin/"
},
// many more lines ommited
}
当通过命令pkg .
打开在./bin
目录中生成的.exe
文件时,我可以找到我的纯文本代码。为什么 ?如何使 pkg 只包含字节码?是的,我知道字符串即使在字节码中也是纯文本,但至少周围的逻辑受到保护。
/bin/index.exe 的屏幕截图
clear code in generated executable
最佳答案
我明白了为什么我的代码包含在纯文本和字节码中。我认为为 future 的读者发布答案可能比删除我的问题更好。
来自 pkg 的文档,在其他注意事项
部分:
By default, pkg will check the license of each package and make sure that stuff that isn't meant for the public will only be included as bytecode.
我进行了更多测试,看看 package.json
是否将属性 private
设置为 true
(或 "true"
)或将 license
属性设置为公共(public)许可证(例如:ISC)。
就我而言,用于创建沙盒项目的 npm init
将许可证属性设置为 ISC
。
从 package.json
中删除 licence
属性解决了该问题。
关于node.js - NodeJS PKG 如何排除源代码包含在纯文本中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72250562/