vert-x3 / vertx-lang-js

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

Cannot find module error #45

Closed morfeusys closed 8 years ago

morfeusys commented 8 years ago

Hi! I have js verticle and package.json contains:

{
  "name" : "npm-test",
  "dependencies" : {
    "figlet" : "1.1.1"
  }
}

npm-test.js file contains:

var figlet = require('figlet');

exports.vertxStart = function() {
    figlet('Hello World!!', function(err, data) {
        if (err) {
            console.log('Something went wrong...');
            console.dir(err);
            return;
        }
        console.log(data)
    });
};

After npm install I'm trying to run vertx run npm-test.js but this puts "Cannot find module fs" error in my console. Of course my $NODE_PATH points to the right node's installation dir. What should I do to resolve this issue?

cescoffier commented 8 years ago

Could you provide a reproducer ?

morfeusys commented 8 years ago

@cescoffier Sorry, didn't get it.. I've provided both js script and package.json files to reproduce it. What else have I to do?

cescoffier commented 8 years ago

If you can come up with a githup project with these files and a readme file with the instructions like:

That would save us some time.

ejmurra commented 8 years ago

@cescoffier Not OP but I'm having this issue too. Here's a reproducer: https://github.com/ejmurra/error_reproducer_vertx.git

It seems that top level require statements such as require('request') error out when the requested module makes another request for a peer dependency (in this case node_modules/request/index.js:17). I'm running OSX 10.10.5

cescoffier commented 8 years ago

Thanks ! Gonna have a look.

pmlopes commented 8 years ago

vert.x javascript is not based on node, even though we can load node modules once a module requires a native node such as fs or http (as required by express and request) in the reports it won't run.

vert.x uses nashorn to run scripts as long as your node modules are pure javascript without using any node API they should work. For example you can run template engines like handlebars, react or any module you could run on a browser.

ejmurra commented 8 years ago

@pmlopes That makes sense to me.

What confuses me is that the error is thrown here which points to this file which has no dependencies (native node modules or otherwise). This leads me to think that nashorn is tracing the require('request') line to the request module in node_modules but any require calls after that are no longer looking in the node_modules directory.

pmlopes commented 8 years ago

You have a corrupted node_modules directory. From your reproducer, I've deleted the directory node_modules and re-download the dependencies with:

npm install

After that I run your example:

npm start

And got the error:

[plopes@heimdall error_reproducer_vertx]$ npm start

> vertx_reproducer@1.0.0 start /home/plopes/Projects/error_reproducer_vertx
> ./node_modules/.bin/vertx run server.js

javax.script.ScriptException: Error: Cannot find module net in <eval> at line number 141 at column number 6
    at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:467)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:451)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:403)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:399)
    at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:155)
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
    at io.vertx.lang.js.JSVerticleFactory$JSVerticle.start(JSVerticleFactory.java:109)
    at io.vertx.core.impl.DeploymentManager.lambda$doDeploy$163(DeploymentManager.java:429)
    at io.vertx.core.impl.ContextImpl.lambda$wrapTask$18(ContextImpl.java:335)
    at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:358)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:357)
    at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:112)
    at java.lang.Thread.run(Thread.java:745)
Caused by: <eval>:141:6 Error: Cannot find module net

And this leads back to what I've written before, net is a node.js core module, you cannot see it on vert.x.