vercel / ncc

Compile a Node.js project into a single file. Supports TypeScript, binary addons, dynamic requires.
https://npmjs.com/@vercel/ncc
MIT License
9.24k stars 287 forks source link

do you even realize what a nightmare using this is #892

Open selfagency opened 2 years ago

selfagency commented 2 years ago

lmao

go with esm modules and use import statements and ncc generates esm code with eval('require') so node throws require is not defined in ES module scope.

drop esm modules and use require statements and ncc compiles cjs code (even with --target es2015 specified) and node throws Error: Cannot find module for each required local module because when you use require it doesn't bundle local modules(???).

i just wasted hours of my life trying to figure out what i'm doing wrong only to realize what i'm doing wrong is trying to use ncc in the first place, apparently. i can't believe github recommends this officially in their documentation.

174n commented 2 years ago

Did you find any alternatives? I just wasted 3 hours on this and came to the same conclusion

selfagency commented 2 years ago

@174n esbuild was the only thing i could get working without tearing my hair out

craftogrammer commented 2 years ago

I tried to waste 3 days, and I was able to make it working by using rollupbefore ncc

MelkorNemesis commented 2 years ago

I bumped into the very same issue with ncc outputting eval('require'). I discovered that it happens when ncc is not able to find a package in the node_modules folder. In my case one of the npm packages I was using had a dependency on a node-fetch v2.6.7 package, which in turn has encoding package as a peerDependency.

I kept getting that error until I manually installed that peerDependency as a dependency into the project.

🧐 To sum up: when ncc is not able to find the package, it outputs eval('require')('<package_name>'). When it is able to find the package, it correctly outputs __WEBPACK_EXTERNAL_createRequire(import.meta.url)('<package_name>').

jetersen commented 2 years ago

For anyone stumbling in to similar issues where eval('require)('src/main') when trying to use "type": "module" for their GitHub actions otherwise it won't compile properly and you have no code in your dist/index.js

You need to use full relative path like so:

import {run} from './src/main.js'

await run()

See https://github.com/updatecli/updatecli-action/pull/100 for a complete ESM github action :)