简体   繁体   English

如何为节点 pkg 创建配置文件

[英]How to create a config file for node pkg

I use node pkg to create a .exe of my nodejs service: https://www.npmjs.com/package/pkg我使用 node pkg创建我的 nodejs 服务的 .exe: https ://www.npmjs.com/package/pkg

My question is: how do I make the .exe use a config.js for some setup values?我的问题是:如何让 .exe 将 config.js 用于某些设置值? Basic stuff like ip, port, database name etc. Because I have 3 environments, and I would like to use the same exe for all, but different config.js files for each.基本的东西,如 ip、端口、数据库名称等。因为我有 3 个环境,我想对所有环境使用相同的 exe,但每个环境使用不同的 config.js 文件。

So far, if I do pkg app.js then it creates an .exe that doesn't look at any other files.到目前为止,如果我执行pkg app.js那么它会创建一个不查看任何其他文件的 .exe。 Totally stand alone.完全独立。 How do I make it look at config.js when it is started up?我如何让它在启动时查看config.js

On the website they do have a section on config https://github.com/zeit/pkg#config but I do not understand how to make use of it.在网站上,他们确实有一个关于config https://github.com/zeit/pkg#config的部分,但我不明白如何使用它。 At the moment I have my app.js, and I have secrets.js which holds the config information.目前我有我的 app.js,我有 secrets.js 保存配置信息。

I am not sure this is right way, but I hope this can be helpful to somebody.我不确定这是正确的方法,但我希望这对某人有所帮助。
Refer to pkg document, on the run time, __dirname becomes "/snapshot/project".参考 pkg 文档,在运行时,__dirname 变为“/snapshot/project”。

So, by checking __dirname, you can identify in which environment you are.因此,通过检查 __dirname,您可以确定您所在的环境。
(node app.js or app.exe). (节点 app.js 或 app.exe)。

Then we can separate require sentence like below.然后我们可以像下面这样分离require语句。

const PKG_TOP_DIR = 'snapshot';

const runInPKG = (function(){
  const pathParsed = path.parse(__dirname);
  const root = pathParsed.root;
  const dir = pathParsed.dir;
  const firstDepth = path.relative(root, dir).split(path.sep)[0];
  return (firstDepth === PKG_TOP_DIR)
})();

let config = require('./appconfig.json');

if(runInPKG) {
  const deployPath = path.dirname(process.execPath);
  config = require(path.join(deployPath, 'appconfig.json'));
}
  • Adding above code to your app.js makes some warning when pkg build.将上述代码添加到您的 app.js 会在 pkg 构建时发出一些警告。

pkg .包。 --targets node8-win-x64 --out-path ./dist --targets node8-win-x64 --out-path ./dist

pkg@4.4.0 Warning Cannot resolve 'path.join(deployPath, 'appconfig.json')' pkg@4.4.0 警告无法解析 'path.join(deployPath, 'appconfig.json')'
app.js应用程序.js
Dynamic require may fail at run time, because the requested file动态 require 可能会在运行时失败,因为请求的文件
is unknown at compilation time and not included into executable.在编译时未知,不包含在可执行文件中。
Use a string literal as an argument for 'require', or leave it使用字符串文字作为 'require' 的参数,或保留它
as is and specify the resolved file name in 'scripts' option.按原样并在 'scripts' 选项中指定解析的文件名。

https://github.com/vercel/pkg/issues/195 https://github.com/vercel/pkg/issues/195
use fs to read config file insead of require or import使用 fs 读取配置文件 insead of require 或 import
eg:例如:
const configPath = path.join(process.cwd(), './config/config.json'); const configPath = path.join(process.cwd(), './config/config.json');
lset data = fs.readFileSync(configPath); lset data = fs.readFileSync(configPath);

same question link: excluding config file while converting node js files to exe using pkg相同的问题链接: 在使用 pkg 将节点 js 文件转换为 exe 时排除配置文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM