vert-x3 / vertx-lang-js

Nashorn JavaScript implementation for Vert.x
Apache License 2.0
35 stars 23 forks source link

Problem with 'require' of node modules from javascript verticles packaged in jars #66

Open chefhoobajoob opened 7 years ago

chefhoobajoob commented 7 years ago

We don't yet have a simple reproducer for this problem, but maybe I can describe what we are doing and the problem we are seeing, and somebody can help understand whether our expectations are wrong.

We have a javascript verticle whose scripts we are packaging up into a jar. The verticle makes use of several different node module dependencies, which we are staging into a node_modules directory.

So, the jar layout is like this:

META-INF/MANIFEST.MF verticle-name/verticle-impl.js verticle-name/some-helper-script.js verticle-name/node_modules/rql verticle-name/node_modules/rxjs verticle-name/node_modules/...<various other node dependencies>

verticle-impl.js uses a require statement to use functions from some-helper-script.js, and the some-helper-script.js uses require statements to pull in its node module dependencies.

We then make this jar available to dependent projects that want to launch the js verticle. We do this using the service factory, where the value of the main property of the service descriptor is set to: verticle-name/verticle-impl.js.

We have verified that the jar with the layout above is in the classpath of the dependent project at runtime.

However, while the dependent project is able to successfully locate the verticle and invoke its start method, and the require statements in verticle-imple.js are successfully finding some-helper-script.js, we are finding that when evaluation gets to the require statements in some-helper-script.js for the node modules in the node_modules directory, Nashorn complains that it can't find the required module.

Assuming no other problems in our implementation, should this organization be able to successfully resolve these require statements in some-helper-script.js?

chefhoobajoob commented 7 years ago

I can confirm from our implementation that jvm-npm.js is not finding the node_modules directory in the js verticle's jar from the classpath.

if I change the require statements in some-helper-script.js from this:

require('jsonpatch-to-mongodb');

...to this:

require('./node_modules/jsonpatch-to-mongodb/index.js');

...then the require statement in some-helper-script.js resolves correctly. But, of course, it breaks when it encounters this require statement in jsonpatch-to-mongodb:

require('jsonpath-to-dot');

...which is a node dependency of jsonpatch-to-mongodb, also located in the same node_modules directory as a peer to jsonpatch-to-mongodb in the verticle's jar.