yao-pkg / pkg

Package your Node.js project into an executable
https://www.npmjs.com/package/@yao-pkg/pkg
MIT License
312 stars 11 forks source link

Support for axios missing #30

Closed issamBenelgada closed 6 months ago

issamBenelgada commented 6 months ago

What version of pkg are you using?

5.8.1

What version of Node.js are you using?

18.19.0

What operating system are you using?

Windows

What CPU architecture are you using?

x86_64

What Node versions, OSs and CPU architectures are you building for?

node18-windows-x64

Describe the Bug

I'm getting this error when executing my packaged project that uses axios.

pkg/prelude/bootstrap.js:1872
      throw error;
      ^

Error: Cannot find module 'C:\snapshot\my-app\node_modules\axios\dist\node\axios.cjs'
1) If you want to compile the package/file into executable, please pay attention to compilation warnings and specify a literal in 'require' call. 2) If you don't want to compile the package/file into executable and want to 'require' it from filesystem (likely plugin), specify an absolute path in 'require' call using process.cwd() or process.execPath.
    at createEsmNotFoundErr (node:internal/modules/cjs/loader:967:15)
    at finalizeEsmResolution (node:internal/modules/cjs/loader:960:15)
    at resolveExports (node:internal/modules/cjs/loader:488:14)
    at Module._findPath (node:internal/modules/cjs/loader:528:31)
    at Module._resolveFilename (node:internal/modules/cjs/loader:932:27)
    at Function._resolveFilename (pkg/prelude/bootstrap.js:1951:46)
    at Module._load (node:internal/modules/cjs/loader:787:27)
    at Module.require (node:internal/modules/cjs/loader:1012:19)
    at Module.require (pkg/prelude/bootstrap.js:1851:31)
    at require (node:internal/modules/cjs/helpers:102:18) {
  code: 'MODULE_NOT_FOUND',
  path: 'C:\\snapshot\\my-app\\node_modules\\axios\\package.json',
  pkg: true
}

Node.js v18.5.0

I belive the problem is that pkg only packages the esm code of axios and not the whole folder, but my project is in commonJS. This is part of the package.json file of the axios package.

{
  "name": "axios",
  "version": "1.6.3",
  "description": "Promise based HTTP client for the browser and node.js",
  "main": "index.js",
  "exports": {
    ".": {
      "types": {
        "require": "./index.d.cts",
        "default": "./index.d.ts"
      },
      "browser": {
        "require": "./dist/browser/axios.cjs",
        "default": "./index.js"
      },
      "default": {
        "require": "./dist/node/axios.cjs",
        "default": "./index.js"
      }
    },
...

The main points to index.js and as nothing requires dist/node/axios.cjs it's not included in the final package. To import the package in my code I use require(axios).

I was able to fix the problem by adding a new dictionary file for axios : axios.js :

'use strict';

module.exports = {
  pkg: {
    scripts: ['dist/**/*'],
  },
};

This did solve my problem and I was hoping it could be added in a future version.

Expected Behavior

Application runs without errors using axios module in a common js project.

To Reproduce

Create a new common js node project with axios as a dependency.

Package project using pkg and run the packaged application.

robertsLando commented 6 months ago

Same as https://github.com/yao-pkg/pkg/issues/10. Include all the axios node modules in scripts/assets in order to make it work

robertsLando commented 6 months ago

Dup of https://github.com/yao-pkg/pkg/issues/16#issuecomment-1945486658