简体   繁体   English

Nodejs Pkg - 如何使用外部配置文件?

[英]Nodejs Pkg - how to use an external Config file?

I am having issues attempting to properly use pkg (nodejs module) properly.我在尝试正确使用 pkg(nodejs 模块)时遇到问题。

I am doing a stand alone file manager (well, it would swap video/audio files to & from preselected directories, intended to allow it without any internet connection it self to remove & add files to a syncing folder like onedrive/dropbox/googledrive/etc. using a text file.)我正在做一个独立的文件管理器(嗯,它会将视频/音频文件交换到预选目录和从预选目录交换,旨在允许它在没有任何互联网连接的情况下自行删除和添加文件到同步文件夹,如 onedrive/dropbox/googledrive/等使用文本文件。)

The issue I am having, is I am at a loss of after I package it into a binary.. I do not understand how to allow/force it to create/read the text file outside compiled binary.我遇到的问题是,在将 package 转换为二进制文件后,我不知所措。我不明白如何允许/强制它在编译后的二进制文件之外创建/读取文本文件。 -- I would love for it to be within the same folder as the executable. - 我希望它与可执行文件位于同一文件夹中。

I am attempting to find a way to store data without having to share the sourcecode, or require node be installed on other machines.我试图找到一种无需共享源代码或需要在其他机器上安装节点的方式来存储数据。 -- I intend to have a minimal permissions as possible, and outside reading/writing the config & 'database' [which is simply a text file with what files are in the local storage, and what files are & are not in the remote storage] - 我打算尽可能地拥有最小的权限,并且在读取/写入配置和“数据库”之外[这只是一个文本文件,其中包含本地存储中的文件,以及远程存储中的文件和不存在的文件]

What am I missing about pkg, & if it can store data internally some how... how do I get it to read an external file?我对 pkg 缺少什么,如果它可以在内部以某种方式存储数据......我如何让它读取外部文件? -- Though I would greatly prefer to have the txt files outside the binary & in plain text easy to read. -- 虽然我非常希望将 txt 文件放在二进制文件之外并且以纯文本形式易于阅读。


As a side question, I am not understanding how to pass an argument through & use it inside the program after it's compiled.作为一个附带问题,我不明白如何在编译后在程序中传递参数和使用它。 [Hell, I'm having a heck of a time, properly understanding the readme for the pkg module] [见鬼,我有一段时间,正确理解 pkg 模块的自述文件]

Use fs features to load config object as in this three-lines of code使用 fs 功能加载配置 object 就像这三行代码一样

filename="./config.json";
let rawdata = fs.readFileSync(filename);
let config = JSON.parse(rawdata);

config.json must be in same direcory of pkg executable config.json 必须在 pkg 可执行文件的同一目录中

If you need to change path of config.json, you will able to specify full-path of this file using command line arguments.如果您需要更改 config.json 的路径,您可以使用命令行 arguments 指定该文件的完整路径。 These can be read at runtime using process.argv variable as explained here这些可以在运行时使用 process.argv 变量读取,如此所述

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

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