can't find the ngrok binary from inside a bundle
Hi!
I want to use ngrok in a monorepo I'm working on, but I'm having trouble with process.js. My setup is such that I have a package inside my monorepo that isn't in root, and depends on ngrok. I also bundle this package because I write it in typescript, and need to publish it to the npm registry eventually as cjs. The issue that comes up is that when I bundle my code, process.js from ngrok is bundled into my output file, and the code that finds the bin directory ends up with the wrong __dirname, which it uses expecting to be relative to the process.js, but in my case is actually relative to 'out/main.js'.
I'm able to get it working by manually setting the binPath in the configuration object - but when someone installs my package, they could end up in the same situation. I wonder if you've encountered this issue before, or if you have any advice on what to do in this scenario?
Thanks!!
Hugh
Hi Hugh! 👋
I have come across this myself, in my ngrok extension for VS Code. It has the same situation, it's written in TS and bundled to a single output file. The issue is the bundling, which, as you say, means the bin is no longer relative to process.js. I set the binPath in the extension myself like this:
const basePath = join(__dirname, 'bin');
export const binPath = (_defaultPath: string) => basePath;
I am not sure we can do much more than making the binPath an option you can pass in like this. I am also not sure what this means for further bundling if your project is used within another. My guess is that if the code has all ended up in the one file, then the binPath you are setting will still apply. Would have to test though.
The module does download the ngrok binary and store it in path.join(__dirname, 'bin'). Would it help to be able to set that?
The module does download the ngrok binary and store it in path.join(__dirname, 'bin'). Would it help to be able to set that?
Ah, I didn't notice that was happening - I wonder if I had triggered a redowload if that would have worked. I think being able to set that would help, yes. I can't check now though because unfortunately my motherboard seems to have stopped working entirely! Or my PSU. Who knows. I'll check as soon as I can and update you!
Thanks Phil!
Did you get any further with this @hughrawlinson?
I haven't been able to try it unfortunately - my computer is still wrecked and covid restrictions are such that it's not super safe to go to the computer repair place just at the moment. Vaccination soon though I hope!
I'm pretty sure though that if I create a location for the binary to be downloaded to, and then passed that in to ngrok, it would work. I wouldn't be doing more than create a temporary directory with mktemp and doing cleanup though. I wonder if that behaviour could be extracted and bundled in ngrok somehow?
No worries if not though - happy to do that in my code and close this ticket :smile:
I know this is a bit old, but I'm adding this here for anyone else that might need this.
I am packaging my app as a self-contained executable that runs on Windows (i.e., MyApp.exe). I also have the ngrok.exe executable copied into the same folder as my executable. In order to tell my app to look for the ngrok executable in the same directory, I set the binPath in my ngrok.connect config like so:
const url = await ngrok.connect({
proto: 'http',
port: port,
binPath: path => path = './'
});
I'm going to close this as the correct response is indeed to set the binPath.