Search code examples
javascriptnode.jsnpmaxios

PKG with Axios 1.x can't be compiled


I tried so many things but with 0 efficiency. I made my Node.js app which contains some API requests that I managed with axios, but I can't make it into an exe file. I tried to downgrade the Axios version on 0.27.0 and it work, but I need to use the version 1.15 because I'm using HTTP/s-proxy-agent and on the lower versions, before 1.x doesn't work.

Here is the warnings I got from pkg when I try to build my project.

> Warning Failed to make bytecode node18-x64 for file /snapshot/ValorantCheckerJs/node_modules/axios/index.js
> Warning Failed to make bytecode node18-x64 for file /snapshot/ValorantCheckerJs/node_modules/axios/lib/axios.js
> Warning Failed to make bytecode node18-x64 for file /snapshot/ValorantCheckerJs/node_modules/axios/lib/utils.js
> Warning Failed to make bytecode node18-x64 for file /snapshot/ValorantCheckerJs/node_modules/axios/lib/helpers/bind.js
> Warning Failed to make bytecode node18-x64 for file /snapshot/ValorantCheckerJs/node_modules/axios/lib/core/Axios.js
> Warning Failed to make bytecode node18-x64 for file /snapshot/ValorantCheckerJs/node_modules/axios/lib/core/mergeConfig.js
> Warning Failed to make bytecode node18-x64 for file /snapshot/ValorantCheckerJs/node_modules/axios/lib/defaults/index.js
> Warning Failed to make bytecode node18-x64 for file /snapshot/ValorantCheckerJs/node_modules/axios/lib/helpers/formDataToJSON.js
> Warning Failed to make bytecode node18-x64 for file /snapshot/ValorantCheckerJs/node_modules/axios/lib/cancel/CanceledError.js
> Warning Failed to make bytecode node18-x64 for file /snapshot/ValorantCheckerJs/node_modules/axios/lib/cancel/CancelToken.js
> Warning Failed to make bytecode node18-x64 for file /snapshot/ValorantCheckerJs/node_modules/axios/lib/cancel/isCancel.js
> Warning Failed to make bytecode node18-x64 for file /snapshot/ValorantCheckerJs/node_modules/axios/lib/env/data.js
> Warning Failed to make bytecode node18-x64 for file /snapshot/ValorantCheckerJs/node_modules/axios/lib/helpers/toFormData.js

Here is my package.json file, i tryied to make some configs for pkg but without any effect.

"pkg": {
    "outputPath": "./dist",
    "targets": [
      "win"
    ],
    "assets": [
      "./node_modules/axios/dist/node/axios.cjs"
    ]
  },
  "dependencies": {
    "axios": "^1.5.0",
    "child_process": "^1.0.2",
    "colors": "^1.4.0",
    "http": "^0.0.1-security",
    "http-cookie-agent": "^5.0.4",
    "http-proxy-agent": "^7.0.0",
    "https-proxy-agent": "^7.0.1",
    "jsonwebtoken": "^9.0.1",
    "prompt-sync": "^4.2.0",
    "socket.io": "^4.7.2"
  },

By the way, I already try to change the requests library with node-fetch but when I build it, it doesn't execute that line. It's just simply closes it.


Solution

  • if you want to make you life easier and not have to handle all of this stuff. you need to take all of your code and create one single js file which include all of your app code with no fancy js stuff like import etc.

    for example in your case lets say you code is in server/app.js

    first step
    install ncc with npm i -g @vercel/ncc (please note I installed this globally but of course you can install it localy and enter the following command in "commands" section in package.json. and of course you can use any tool like ncc)

    second step
    generate one js file for you server by running the command ncc build server/app.js

    third step
    now you should have the following file dist/index.js which includes all the app code in one file. now run pkg on this file for example pkg --targets win32 dist/index.js now its should compile without problem and include axios in it