TooTallNate / node-bindings

Helper module for loading your native module's `.node` file

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

In ES module "__filename" is not defined

crystalfp opened this issue · comments

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

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);

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.

commented

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?

commented

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