Pregunta

I'm starting with node-webkit and I cannot load a module (a JS file) I made in my project. Here is the tree view : root

|-index.html
|-js/
|---app.js
|---resources/
|-----angular..jquery..
|---crawler/
|-----leboncoin/
|-------lbcCrawler.js

I want to load the module "mycrawler" in the "app.js", but I get the following error :

Error: Cannot find module 'js/crawler/leboncoin/lbcCrawler.js'
    at Function.Module._resolveFilename (module.js:334:15)
    at Function.Module._load (module.js:273:25)
    at Module.require (module.js:360:17)
    at require (module.js:376:17)
    at window.require (eval at undefined, <anonymous>:1:112)
    at eval (file:///C:/Users/acouty/git/webkit%20fooling/js/ui/app.js:3:10)

My files : index.html:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" href="css/style.css">
    <script src="js/resources/angular/angular.min.js"></script>
    <script src="js/resources/jquery-ui-1.10.3/js/jquery-1.9.1.js"></script>
    <script src="js/resources/jquery-ui-1.10.3/js/jquery-ui-1.10.3.custom.min.js"></script>
    <script src="js/ui/app.js"></script>
</head>
<body>
</body>

</html>

app.js

var app = angular.module("app", []);

var cc = require("crawler/leboncoin/lbcCrawler.js");

lbcCrawler.js

var simpleCrawler = require("simplecrawler");

var BASE_URL = "http://www.leboncoin.fr/annonces/offres/:region/occasions/?f=a&th=1&q=:item";
var DEFAULT_REGION = "ile_de_france";

var crawl = function(item, region, results)
{
    if (_.isUndefined(region) || _.isNull(region))
    {
        region = DEFAULT_REGION;
    }
    console.log("Looking for %s in %s", item, region);

    var queryURL = BASE_URL.replace(":region", region).replace(":item", item);

    var lbcCrawler = simpleCrawler.crawl(queryURL);
    lbcCrawler.interval(500);
    lbcCrawler.on("fetchComplete", function(queueItem, responseBuffer, response) {
        results.push(queueItem);
    });

    lbcCrawler.start();
}

EDIT

I tried something else : What I want is to use my modules like dependencies. The ui part is for angularJS, and other folders for the node-only part. I tried to do something more logcal in my thought : create an index.js and package.json files into lebecoincrawler directory. Now, I want a ui/ folder file to require this. But I cannot...

I tried :
var cc = require("../crawler/leboncoin/lbcCrawler.js");
var cc = require("./../crawler/leboncoin/lbcCrawler.js");
var cc = require("./js/crawler/leboncoin/lbcCrawler.js");

I can't get it to work and I can't believe it's impossible to do, otherwise I cannot imagine how my projet folder would be.. flat :(.


Thanks !

¿Fue útil?

Solución

Try changing require to start with your current directory explicitly, by adding ./ to the beginning of your path:

var cc = require("./crawler/leboncoin/lbcCrawler.js");

Otherwise, node will look in a node_modules directory, or similar, cf: http://www.bennadel.com/blog/2169-Where-Does-Node-js-And-Require-Look-For-Modules-.htm

EDIT:

For some reason, that's not working in node-webkit, but this might do the trick:

path = require('path');
var cc = require(path.join(process.cwd(),"js/crawler/leboncoin/lbcCrawler.js"));

EDIT2:

I think I've got it figured out now. __dirname and __filename are not defined in the node REPL, nor in a node-webkit script tag, even if your script tag is used to include your .js file, as in

<script src="js/app.js"></script>

Thus, if you call require lbcCrawler.js from app.js you'd need to require it as

var cc = require("./js/crawler/leboncoin/lbcCrawler.js");

because the path is where you start node-webkit, e.g. where index.html lives.

However, if you do

<script>require('js/app.js')</script>

then __dirname and __filename are defined and require will search from where the app.js file lives, i.e. you would do:

var cc = require(path.join("./crawler/leboncoin/lbcCrawler.js"));

Because of this discrepancy, you might be better off depending on the value of process.cwd (as in the first edit above) which should be the same in either case, whether app.js is included via require or script.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top