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

In ES module "__filename" is not defined #81

Open
crystalfp opened this issue Jun 23, 2023 · 4 comments
Open

In ES module "__filename" is not defined #81

crystalfp opened this issue Jun 23, 2023 · 4 comments

Comments

@crystalfp
Copy link

I bundle with rollup my application (that uses indirectly this module) as an ES module. The bundle fails at runtime because __filename is not defined.
Could the following alternative be added if __filename is not defined?

import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)

Thanks!
mario

@caoqishun
Copy link

caoqishun commented Jul 10, 2023

I encountered the same problem, how ? nodejs 16.20.1 , i think this code can‘t run .
Error.prepareStackTrace = function(e, st) {
for (var i = 0, l = st.length; i < l; i++) {
fileName = st[i].getFileName();
if (fileName !== __filename) {
if (calling_file) {
if (fileName !== calling_file) {
return;
}
} else {
return;
}
}
}
};

// run the 'prepareStackTrace' function above
Error.captureStackTrace(dummy);

@crystalfp
Copy link
Author

In rollup.config.js add to output section the following line:

banner: `#! /usr/bin/env node\n\nconst __filename = "D:/Projects/server/dist/server.js\n\n`,

With the variable pointing to the bundled file.

This solved the problem for me.

@tmrsiteops
Copy link

tmrsiteops commented Oct 26, 2023

I'm having this issue with my sveltekit/vite build, also because of the use of __filename in bindings. Is there possibly something I could add to my vite config to rename the build file to use the .cjs extension?

@tmrsiteops
Copy link

It seems I've fixed this by moving better-sqlite3 (which had bindings as a dependency) from devDependencies to dependencies in my package.json.

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

3 participants