简体   繁体   中英

Using node pkg to create executable with npm config

I'm using pkg to create an executable for my node js application. This works great. However, I'm also using the config module to load yaml config files based on the environment. When packaging the app using pkg, I'm specifying that the config folder should be included.

  "pkg": {
    "assets": [
      "config/*"
    ]
  }

When I run pkg . --debug pkg . --debug , I can see that the config files are being packaged up. However, if I then rename the config folder, delete the folder, or just move the newly packaged exe to a different folder, it says No configurations found in configuration directory:C:\\Users\\me\\folderwithexe\\config (this config directory doesn't exist because I moved the exe to a new folder)

From what I can tell, the config module appears to be looking for the config folder relative to where the exe is being executed. It's not getting it from the packaged exe file even though it's in there. So if you were to run this exe on another computer (which is the plan), then it's looking for the config folder outside of the exe. None of the other modules appear to have this problem. It's just this config module.

Any idea how I can get the pkg module and the config module to work together?

Here is my full package.json

{
  "name": "my-app",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "author": "Me",
  "license": "UNLICENSED",
  "dependencies": {
    "config": "^3.3.1",
    "js-yaml": "^3.14.0",
  },
  "bin": "app.js",
  "pkg": {
    "assets": [
      "config/*"
    ]
  }
}

The pkg will bundle every require dependency and every assets or scripts that it found in configuration lists (assets and scripts). So, first you need to keep your config files away from pkg.

To keep pkg away from your config files, you can use a variable path that pkg can't resolve, by example:

const config = require(path.join(__dirname, 'config/config.json'));

At this point the pkg don't bundle your config file, but if you run the build, you notice that the path of your config.json is something like /snapshot/config/config.json ( https://www.npmjs.com/package/pkg#snapshot-filesystem )

The alternative is to get the config file from binary path using the process.execPath :

const config = require(path.join(process.execPath, "../","./config/config.json"));

After that, the executable will get the config file from "relative path" of running directory.

I don't test with the config module but I thing if you remove the assets: ["config/*"] from the pkg property from package.json and add the new path to the config module (above) it will work.

process.env["NODE_CONFIG_DIR"] = path.join(process.execPath, "../","./config/");
const config = require("config");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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