节点Webkit加载模块失败 [英] node-webkit loading module fails
问题描述
我从node-webkit开始,但是无法加载我在项目中制作的模块(JS文件). 这是树视图: 根
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
我想在"app.js"中加载模块"mycrawler",但出现以下错误:
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)
我的文件: index.html:
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
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
我尝试了其他方法:
我要的是use my modules like dependencies
. ui部分用于angularJS,而其他文件夹则用于仅节点部分.
我想着想做些其他的事情:将index.js和package.json文件创建到lebecoincrawler目录中.
现在,我要一个ui/文件夹文件要求这个.但是我不能...
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");
我无法使它正常工作,而且我不敢相信这是不可能的,否则我无法想象我的projet文件夹会如何.. flat:(.
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 :(.
谢谢!
推荐答案
尝试将require
更改为从当前目录开始,方法是在路径的开头添加./
:
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");
否则,节点将在node_modules目录或类似目录中查找: http://www.bennadel.com/blog/2169-Where-Does-Node-js-And-Require-Look-For-Modules-.htm
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
由于某种原因,这在node-webkit中不起作用,但这可以解决问题:
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"));
我想我已经明白了. __dirname
和__filename
既未在节点REPL中定义,也未在node-webkit脚本标记中定义,即使您的脚本标记用于包含您的.js文件,如
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>
因此,如果您从app.js
呼叫要求lbcCrawler.js
,则需要将其要求为
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");
因为该路径是您启动node-webkit的位置,例如index.html居住的地方.
because the path is where you start node-webkit, e.g. where index.html lives.
但是,如果您这样做
<script>require('js/app.js')</script>
然后定义__dirname
和__filename
,require
将从app.js文件所在的位置搜索,即您应该这样做:
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"));
由于这种差异,根据process.cwd
的值(如上面的第一个编辑中),您可能会更好,无论这两种情况是相同的,无论是通过require
还是通过
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
.
这篇关于节点Webkit加载模块失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!