2016-01-16 189 views
0

我正在尝试通过执行learnyounode课程来学习Node.js基础知识。 我被困在了Make it Modular章节。模块和Node.js

我不知道我是否被允许在这里粘贴它的内容,但粗略地说,我被要求创建一个模块来过滤指定扩展名后目录中的文件名。

以下是我迄今所做的:

var module = require('module'); 

module(process.argv[2], process.argv[3], function (err, data){ 
    if(err){ 
    console.log("Error"); 
    } 
    else{ 
    console.log(data); 
    } 
}); 

module.js:

var fs = require('fs'); 
var path = require('path'); 

module.exports = function (dir, ext, callback){ 
    fs.readdir(dir, function (err, files){ 
    if(err){ 
     callback(err); 
    } 
    else { 
     var array = ''; 
     for(var i=0; i<files.length; i++){ 
     if(path.extname(files[i]) === '.'+ext){ 
     array+=files[i]+"\n"; 
     } 
     } 
     callback(null, array); 
    } 
    }); 
} 

我得到以下错误:

module.js:27 
    this.id = id; 
     ^

TypeError: Cannot set property 'id' of undefined 
    at Module (module.js:27:11) 
    at Object.<anonymous> (/home/maxence/Documents/Node.js/learnyounode/MIM/mim.js:3:1) 
    at Module._compile (module.js:397:26) 
    at Object.Module._extensions..js (module.js:404:10) 
    at Module.load (module.js:343:32) 
    at Function.Module._load (module.js:300:12) 
    at Module.require (module.js:353:17) 
    at require (internal/module.js:12:17) 
    at Object.<anonymous> (/usr/local/lib/node_modules/learnyounode/node_modules/workshopper-wrappedexec/exec-wrap.js:87:3) 
    at Module._compile (module.js:397:26) 

我的猜测是我”我没有正确地在我的代码中的某个地方声明了一个变量,但我无法找到它。模块的调用是否正确?

最好的问候, MM

编辑:作为gnerkus说,应该有,而不是'模块 './module'。这里是工作代码:

var module = require('./module'); 

module(process.argv[2], process.argv[3], function (err, data){ 
    if(err){ 
    console.log("Error"); 
    } 
    else{ 
    console.log(data); 
    } 
}); 

module.js:

var fs = require('fs'); 
var path = require('path'); 

module.exports = function (dir, ext, callback){ 
    fs.readdir(dir, function (err, files){ 
    if(err){ 
     callback(err); 
    } 
    else { 
     var array = ''; 
     for(var i=0; i<files.length; i++){ 
     if(path.extname(files[i]) === '.'+ext){ 
     array+=files[i]+"\n"; 
     } 
     } 
     callback(null, array); 
    } 
    }); 
} 

编辑2:看来,这个版本是首选:

var module = require('./module'); 

module(process.argv[2], process.argv[3], function (err, data){ 
    if(err){ 
    console.log("Error"); 
    } 
    else{ 
    for(var i=0; i<data.length; i++){ 
     console.log(data[i]); 
    } 
    } 
}); 

module.js:

var fs = require('fs'); 
var path = require('path'); 

module.exports = function (dir, ext, callback){ 
    fs.readdir(dir, function (err, files){ 
    if(err){ 
     callback(err); 
    } 
    else { 
     var array = []; 
     for(var i=0; i<files.length; i++){ 
     if(path.extname(files[i]) === ('.'+ext)){ 
     array.push(files[i]); 
     } 
     } 
     callback(null, array); 
    } 
    }); 
} 

两者都是正确的,但第二个ve需要rsion才能完成本章。

+2

的错误是在module.js 27行但你只显示多达18行。如果这是你的整个文件,你缺少右括号,否则,至少应该在错误周围发布几行。 – noahnu

+0

你说得对,右括号确实缺失。 Ans as gnerkus说,'module'应该是主文件中的'./module'。谢谢! – Kathandrax

回答

1

发生此错误是因为您需要节点模块而不是您的自定义模块。从NodeJS documentation

If the module identifier passed to require() is not a native module, and does not begin with '/', '../', or './', then Node.js starts at the parent directory of the current module, and adds /node_modules, and attempts to load the module from that location. Node will not append node_modules to a path already ending in node_modules.

solution.js

var myModule = require('./module'); 
// The rest of your code. 
+0

你是对的!代码现在正在完美工作。非常感谢你!我用正确的版本编辑了第一条消息。 – Kathandrax