1

I'm working in a Node JS project, which takes an API, creates objects and saves them into a DB, everything works in it fine, as you can see in one past question I made.

Now I need to create a executable file of it to run it, so I searched about it and found PKG.

The thing is that I installed the package using npm i pkg, and then I ran pkg app.mjs and I got a lot of error messages, all of them look like this:

Warning Failed to make bytecode node16-x64 for file C:\snapshot\weather\node_modules\fetch-blob\file.js

So, I searched and found that I had to use pkg . --debug to look for the error, and showed me that I had to add a bin property, so I added it in my package.json, which looks like this:

{
  "name": "demo",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "bin": "./app.mjs",
  "module": "CommonJS",
  "type": "commonjs",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node app.mjs",
    "dev": "nodemon app.mjs"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "dotenv": "^10.0.0",
    "nexe": "^4.0.0-beta.19",
    "node-fetch": "^3.1.0",
    "pkg": "^5.5.2",
    "tedious": "^14.0.0"
  }
}

I ran the debug comman and at the end It showed my this error:

C:\snapshot\weather\node_modules\formdata-polyfill\esm.min.js:3 import C from 'fetch-blob' ^^^^^^ SyntaxError: Cannot use import statement outside a module.

enter image description here

As you can see, my files were created, when I tried to open them, none of them does something, as they should make some inserts into a DB.

So I would like to know how can I fixed, because I tried what I saw in this question and this issue but nothing solved my problem.

Have somebody created a .exe from Node JS using this? Do you know what causes the error and how can I fix it?

EDIT: I continued searching and watch the issue in Github, so I can't use PKG because apparently this can't be done with ES Modules (imports).

So I decided to look at other options and found about Nexe, so I decided to change into it, I followed all the instructions I saw in this tutorial, and wrote:

nexe app.mjs

And I got this error:

i nexe 4.0.0-beta.19 √ Downloading pre-built Node.js √ Finished in 1.539s Error: https://github.com/nexe/nexe/releases/download/v3.3.3/windows-x64-14.17.6 is not available, create it using the --build flag See nexe -h for usage..

Can anyone tell me some way I can create a exe file from a ES Module? Or what am I doing wrong? I checked my Node JS version and I don't think it is something about it, but if needed, I'm working in v14.17.6.

I watched this question Wyck told me, but it didn't solve my problem either. If any extra explanation is needed, let me know.

Martín JF
  • 152
  • 2
  • 14

1 Answers1

2
  1. pkg should be in devDependencies
  2. scripts should be in package.json "build:win": "npx pkg ./start.js -c package.json -t node14-win-x64",
  3. Add to package.json "pkg": { "assets": [], "targets": [ "node14-win-x64", "node12-macos-x64" ], "outputPath": "./dist" }
  4. Add to assets some files if you need it inside *.exe