简体   繁体   English

使用 settings.json 文件与 Node.js 和 pkg

[英]Use settings.json file with Node.js and pkg

I'm currently trying to make my Node.js app into an executable using pkg, but I have encountered a problem.我目前正在尝试使用 pkg 将我的 Node.js 应用程序变成可执行文件,但我遇到了一个问题。 My app depends on a separate file called settings.json where vital settings are defined by the user.我的应用程序依赖于一个名为settings.json的单独文件,其中重要设置由用户定义。 My app successfully gathers the information from settings.json when I run the Node app from command line, but the .exe variant made with pkg doesn't seem to follow the settings.json , instead it always follows the content settings.js that existed once the.exe was created.当我从命令行运行 Node 应用程序时,我的应用程序成功从settings.json收集信息,但是使用 pkg 制作的.exe变体似乎不遵循settings.json ,而是始终遵循存在的内容settings.js一旦创建了.exe。

The settings.json is imported like this in my js file: settings.json 在我的 js 文件中是这样导入的:

const settings = require('./settings.json');

I have included this under pkg in package.json:我在 package.json 的 pkg 下包含了这个:

"pkg": {
    "assets": [
        "./node_modules/boxen/**",
        "./node_modules/chalk/**",
        "./node_modules/cheerio/**",
        "./node_modules/htmlparser2/**",
        "./node_modules/moment/**",
        "./node_modules/request/**"
    ]
  }

These are the dependencies that need to bundled with the .exe , but I have not included the settings.json here which is why I wonder why the .exe variant isn't reading from the settings.json file that's in the same directory.这些是需要与.exe捆绑在一起的依赖项,但我没有在此处包含settings.json这就是为什么我想知道为什么.exe变体没有从同一目录中的settings.json文件中读取。

Thanks for any input on this!感谢您对此的任何意见!

require is evaluated at build time. require在构建时进行评估。 If you want to get the JSON at run time then you'll need to read the file and then parse it yourself.如果您想在运行时获取 JSON,那么您需要读取该文件,然后自己进行解析

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

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