Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8.1.3 - Issue with running program when zx is webpacked due to deno.js #957

Closed
td-krzysiek opened this issue Nov 26, 2024 · 8 comments · Fixed by #958
Closed

8.1.3 - Issue with running program when zx is webpacked due to deno.js #957

td-krzysiek opened this issue Nov 26, 2024 · 8 comments · Fixed by #958
Assignees

Comments

@td-krzysiek
Copy link

Hello,

I noticed an issue with zx version 8.1.3 and newer, where zx does not work well with Webpack. The problem occurs when the program tries to load deno.js, which does not exist after bundling the application. 8.1.2 seems to work fine, although reference to node_modules/zx/build/deno.js is the output file since 8.1.0.

node:internal/modules/cjs/loader:1249
  const err = new Error(message);
              ^

Error: Cannot find module './index.cjs'
Require stack:
- /Users/krzysiek/Documents/work/dev/td1023/gnss/node_modules/zx/build/deno.js
    at Function._resolveFilename (node:internal/modules/cjs/loader:1249:15)
    at Function._load (node:internal/modules/cjs/loader:1075:27)
    at TracingChannel.traceSync (node:diagnostics_channel:315:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:218:24)
    at Module.require (node:internal/modules/cjs/loader:1340:12)
    at require (node:internal/modules/helpers:141:16)
    at 13103 (/usr/jrd/gnss/vendor.js:1:1658751)
    at u (/usr/jrd/gnss/reset.js:1:6143)
    at /usr/jrd/gnss/reset.js:1:3399
    at u.a (/usr/jrd/gnss/reset.js:1:6736) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/krzysiek/Documents/work/dev/td1023/gnss/node_modules/zx/build/deno.js'
  ]
}

Node.js v22.11.0

Image

tsconfig:

{
  "compilerOptions": {
    "module": "ESNext",
    "moduleResolution": "Bundler",
    "target": "ES2023",
    "lib": ["ES2023"],
    "outDir": "./build",
    "baseUrl": ".",
  }
}
@antongolub
Copy link
Collaborator

antongolub commented Nov 26, 2024

@td-krzysiek,

Provide a full reproducible example / repo ref plz.

@td-krzysiek
Copy link
Author

Sure, the readme explains the steps to reproduce this problem:

example-issue.tar.gz

@antongolub
Copy link
Collaborator

This is definitely not zx or deno issue. I'm afraid webpack does not build the module tree properly, when ESM module uses the require API. But you can point the bundler to the cjs entry by hand:

resolve: {
    // Add '.ts' as a resolvable extension.
    extensions: ['.ts', '.js'],
    symlinks: false,
    alias: {
      "zx/build/index.js": "./node_modules/zx/build/index.cjs"
    }
    // plugins: [new TsconfigPathsPlugin({ extensions: ['.ts', '.js'] })],
  },

@td-krzysiek
Copy link
Author

Thanks, I’ll apply this for the time being. What is the reason for using require instead of regular imports here? When I replace require with import, everything starts to work correctly, at least in the Node.js environment and in my simple example:

main.mjs

import { $ } from 'zx';
$.verbose = true;
await $`echo "Hello, world!"`;

Image

@td-krzysiek
Copy link
Author

The alias below woks for me:

{
  resolve: {
    extensions: ['.ts', '.js'],
    symlinks: false,
    alias: {
      "zx": path.resolve(__dirname, 'node_modules/zx/build/index.cjs'),
    },
  },
}

@antongolub
Copy link
Collaborator

antongolub commented Nov 27, 2024

When I replace require with import

This is a limitation of our bundler plugin for now: require(ref) or await import(ref). I'll add a re-export feature.

@antongolub antongolub self-assigned this Nov 27, 2024
antongolub added a commit to antongolub/zx that referenced this issue Nov 27, 2024
antonmedv pushed a commit that referenced this issue Nov 27, 2024
* build: use reexport to bind esm and cjs bundles

closes #957

* chore: update esbuild-hybrid-plugin to v0.3.1
@antongolub
Copy link
Collaborator

@td-krzysiek,

Try v8.2.3-dev.b15179f and allow cjs here

resolve: {
    // Add '.ts' as a resolvable extension.
    extensions: ['.ts', '.js', ".cjs"],

@td-krzysiek
Copy link
Author

@antongolub Thanks, I confirm that it bundles all files now (lines comes from resolve plugin):

Image

plugin:

{
  apply(resolver: Resolver) {
    resolver.getHook('before-resolve')
      .tapAsync('AddonPlugin', (request, _resolveContext, callback) => {
        console.log(request.path, request.request);
        callback();
      });
  }
}

I can still see full deno.js file path in my output, however this doesn't cause any problem now since require isn't used any longer:

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants