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

ncc incorrectly includes __dirname in .mjs output #754

Closed
henderea opened this issue Aug 19, 2021 · 11 comments
Closed

ncc incorrectly includes __dirname in .mjs output #754

henderea opened this issue Aug 19, 2021 · 11 comments

Comments

@henderea
Copy link

Version 0.29.x (#720) has made it so that if you have a .mjs file as an input, you get a .mjs file as an output. I would like a flag to get back the 0.28.x behavior of outputting it as a .js file that I can run directly with node, even v10. I can currently accomplish this by using version 0.28.x to build my scripts, but I would rather not be stuck on an old version forever.

Please add a flag to force the output of the build into a traditional-style .js regardless of what the input is, just like the behavior of v0.28.x.

@styfle
Copy link
Member

styfle commented Aug 19, 2021

Node.js 10.x reached End-Of-Life on 2021-04-30 and is no longer receiving updates.

If you wish to support older Node.js versions, you can continue using an older ncc version.

@henderea
Copy link
Author

henderea commented Aug 19, 2021

NodeJS 10 support isn't really the issue. I can't run node dist/script/index.mjs in newer nodejs version either.

My goal is to get a CJS output (with .js extension) from a MJS input. In other words, I want to be able to convert it while bundling.

@styfle
Copy link
Member

styfle commented Aug 19, 2021

I can't run node dist/script/index.mjs in newer nodejs version either

Why not?

@henderea
Copy link
Author

henderea commented Aug 19, 2021

Well, apparently something in the bundled file is using __dirname, though I'm not sure what. It runs fine if I run the original non-bundled .mjs file.

If I use v0.28.6 of @vercel/ncc, I don't have any issues with the output file.

@styfle
Copy link
Member

styfle commented Aug 19, 2021

__dirname isn't possible to use inside any .mjs file so I would expect node sourcefile.mjs to fail too

@henderea
Copy link
Author

If I run node ./src/script.mjs, it has no issues. If I bundle it with ncc and run node ./dist/script/index.mjs, it fails with a complaint about __dirname. My best guess is that one of my dependencies uses it somewhere, but I'm not really sure, because it's clearly working fine if I run the original non-bundled file.

@henderea
Copy link
Author

The main thing is, I don't want to have to convert everything I have to ESM, so I would like to be able to have ncc output with CJS. I have a lot of stuff, and I'd rather avoid a huge conversion project.

@styfle
Copy link
Member

styfle commented Aug 19, 2021

My best guess is that one of my dependencies uses it somewhere, but I'm not really sure, because it's clearly working fine if I run the original non-bundled file.

Can you share more information so we can reproduce the bug?

Sounds like a duplicate of #749

@styfle styfle changed the title Please add flag to bring back v0.28.x behavior of outputting as CJS with .js extension regardless of inputs ncc incorrectly includes __dirname in .mjs output Aug 19, 2021
@henderea
Copy link
Author

My main goal is to keep compatibility with CJS even when I use ESM in a few files. The core issue isn't the error, it's that I don't want an ESM output regardless of whether it works or not.

@henderea
Copy link
Author

henderea commented Aug 19, 2021

As for the source of the __dirname, it seems like it's coming from some dependency somewhere. There is one reference in the output, which contains __nccwpck_require2_.ab=__dirname+"/";. Thing is, when I do an ack for ab=__dirname+"/" in the node_modules, all of the results are in @vercel/ncc:

> ack -l --literal 'ab=__dirname+"/"' node_modules 
node_modules/@vercel/ncc/dist/ncc/index.js.cache.js
node_modules/@vercel/ncc/dist/ncc/cli.js.cache.js
node_modules/@vercel/ncc/dist/ncc/sourcemap-register.js.cache.js
node_modules/@vercel/ncc/dist/ncc/loaders/relocate-loader.js.cache.js
node_modules/@vercel/ncc/dist/ncc/loaders/stringify-loader.js.cache.js
node_modules/@vercel/ncc/dist/ncc/loaders/shebang-loader.js.cache.js
node_modules/@vercel/ncc/dist/ncc/loaders/ts-loader.js.cache.js

@henderea
Copy link
Author

Well, I found I can get the old behavior in 0.29.x by using the API instead of the CLI. I'll just go that route, I guess.

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