Joey Yi Zhao
Joey Yi Zhao

Reputation: 42536

Whether nodejs support runtime dependency?

I have created a nodejs application. It has jdbc dependency in my package.json file. I found that my app can't be launched if the target machine doesn't have jre installed. Since jdbc is only an optional function in my app, I wonder whether there is a way for me to allow the app running in an environment which doesn't have jre installed.

Ideally, I want to implement a logic like this:

1, detect whether the target machine installs jre or not

2, If installed, enable this feature

3, if not, disable this feature

So I need a runtime dependency on nodejs in order to support this requirement.

Upvotes: 2

Views: 974

Answers (1)

Derek Brown
Derek Brown

Reputation: 4419

Your question has two parts:

  1. Can you detect whether the machine has Java installed?
    Yes- you can use code like this which would execute a shell command to determine if the JRE is installed, and possibly verify the version.

  2. Can you selectively include a dependency at runtime?
    Also yes- you can just use an if statement like:

    var jbdc; if(java_installed){ jbdc = require('jbdc'); }

This being said, if you are getting an error at install time from jbdc you are still going to have to manually choose to install jbdc if Java is supported. To do this, add jbdc to your package.json as an optional dependency.

Upvotes: 3

Related Questions