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

esm import cjs package with .node file require return a path string #3809

Closed
wszgrcy opened this issue Jun 22, 2024 · 4 comments
Closed

esm import cjs package with .node file require return a path string #3809

wszgrcy opened this issue Jun 22, 2024 · 4 comments

Comments

@wszgrcy
Copy link

wszgrcy commented Jun 22, 2024

like sharp package.
I use config

  loader: {
      '.node': 'file',
    },

when I import (Imported by other packages),after build ,The file sharp-linux-x64-DQNVFNDI.node has been successfully extracted
in sharp package

  module.exports = require(`../build/Release/sharp-${platformAndArch}.node`);

but module2.exports return a path string../sharp-linux-x64-DQNVFNDI.node

module2.exports = globRequire_build_Release_sharp_node(`../build/Release/sharp-${platformAndArch}.node`);

I try to use #1051 (comment) but not work
Is it because 'require' was not captured in the ESM?

@evanw
Copy link
Owner

evanw commented Jun 22, 2024

Please read the “getting started” instructions in the documentation: https://esbuild.github.io/getting-started/#bundling-for-node:

You also may not want to bundle your dependencies with esbuild. There are many node-specific features that esbuild doesn't support while bundling such as __dirname, import.meta.url, fs.readFileSync, and *.node native binary modules. You can exclude all of your dependencies from the bundle by setting packages to external:

esbuild app.jsx --bundle --platform=node --packages=external

@wszgrcy
Copy link
Author

wszgrcy commented Jun 23, 2024

Please read the “getting started” instructions in the documentation: https://esbuild.github.io/getting-started/#bundling-for-node:

You also may not want to bundle your dependencies with esbuild. There are many node-specific features that esbuild doesn't support while bundling such as __dirname, import.meta.url, fs.readFileSync, and *.node native binary modules. You can exclude all of your dependencies from the bundle by setting packages to external:

esbuild app.jsx --bundle --platform=node --packages=external

Can 'dynamic require' be supported in 'onResolve' to allow users to set their own parsing method?

@evanw
Copy link
Owner

evanw commented Jun 23, 2024

The sharp package is not compatible with bundlers. You should use --packages=external so you don't bundle the sharp package. You don't need to use onResolve to solve this problem.

@wszgrcy
Copy link
Author

wszgrcy commented Jun 23, 2024

The sharp package is not compatible with bundlers. You should use --packages=external so you don't bundle the sharp package. You don't need to use onResolve to solve this problem.

thanks

@wszgrcy wszgrcy closed this as completed Jun 23, 2024
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

No branches or pull requests

2 participants