ngrok / ngrok-javascript

Embed ngrok secure ingress into your Node.js apps with a single line of code.
https://ngrok.com
Apache License 2.0
94 stars 18 forks source link

Can't run @ngrok/ngrok without `--optional` flag on `npm install` #99

Closed neonexus closed 11 months ago

neonexus commented 11 months ago

I'm trying to move away from ngrok to the official @ngrok/ngrok package in my main open-source repo (sails-react-bootstrap-webpack). Just seems like the prudent move. However, right off the bat, I ran into an issue with require('@ngrok/ngrok')...

Error: Cannot find module '@ngrok/ngrok-darwin-x64'
Require stack:
- /Users/neonexus/Sites/sails-react-boostrap-webpack/node_modules/@ngrok/ngrok/index.js
- /Users/neonexus/Sites/sails-react-boostrap-webpack/ngrok.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1048:15)
    at Module._load (node:internal/modules/cjs/loader:901:27)
    at Module.require (node:internal/modules/cjs/loader:1115:19)
    at require (node:internal/modules/helpers:130:18)
    at Object.<anonymous> (/Users/neonexus/Sites/sails-react-boostrap-webpack/node_modules/@ngrok/ngrok/index.js:127:29)
    at Module._compile (node:internal/modules/cjs/loader:1241:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Module._load (node:internal/modules/cjs/loader:938:12)
    at Module.require (node:internal/modules/cjs/loader:1115:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/neonexus/Sites/sails-react-boostrap-webpack/node_modules/@ngrok/ngrok/index.js',
    '/Users/neonexus/Sites/sails-react-boostrap-webpack/ngrok.js'
  ]
}

I am attempting to run this on my old Mac mini 2012. That I don't believe would be the issue. The issue, is more likely because I managed to find a way to run macOS Sonoma (most recent major version) on my mini (this particular model of Mac mini has been unsupported by Apple for years now).

Now, that all said, I don't know why the package wouldn't have installed @ngrok/ngrok-darwin-x64 when it is clearly detecting the arch correctly...

Someone, please help!

neonexus commented 11 months ago

Ok, so this is a bit odd... I discovered that if I have @ngrok/ngrok listed in optionalDependencies and npm i --optional, then it installs everything correctly:

Screenshot 2023-11-03 at 1 03 24 PM

But, if I npm i @ngrok/ngrok --save-dev, then only the main package is installed, none of the native bindings come along (same behavior when installed in dependencies):

Screenshot 2023-11-03 at 1 04 26 PM

EDIT: When it's in devDependencies (or dependencies), and I run npm i --optional, it does install everything. This is a problem, as this would require --optional on EVERY npm install, regardless of the level of requirement.

neonexus commented 11 months ago

Ok, I finally figured out my problem... My .npmrc file had omit=optional in it...

Closing...

nijikokun commented 11 months ago

Thanks for bringing attention to this and documenting the steps as you figured it out @neonexus ❤️ I'm sure others will find this useful if they run into the same issue.

Maybe there is something we can do here to make this clearer and easier for users.

neonexus commented 11 months ago

@nijikokun Something that might be a good / helpful change, is wrapping the native bindings require calls in a big try / catch; that way if something isn't installed (like with my case), then the package can throw out an error like "could not load native bindings... Are you excluding optional dependencies?"...