简体   繁体   English

Node.js的 错误:模块不是函数

[英]Node.js. Error: module is not a function

i have a main file -- index.js: 我有一个主文件 - index.js:

var express = require('express');
var app = express();
var request = require('request');

var demo = require('demo');

// This app will only respond requests to the '/scrape' URL at port 3000.
app.get('/scrape', function (req, res) {
    var url = "http://www.l.com";

    request(url, function (error, response, html) { // two parameters: an URL and a callback
        if (!error) {
            demo(html);
        }
    });
});

var server = app.listen(3000, function () {
    var host = server.address().address;
    var port = server.address().port;

    console.log('Example app listening at http://%s:%s', host, port);
});

and my module is demo.js: 我的模块是demo.js:

module.exports = function (html) {
    ....

    return JSON.stringify(json);
}

The error is : 错误是:

TypeError: demo is not a function TypeError:demo不是函数

I am new to node.js, i would like to know why this didn't work. 我是node.js的新手,我想知道为什么这不起作用。 Maybe i dont understand the real principle of module? 也许我不明白模块的真正原理? Thank you for answer me first. 谢谢你先回答我。

You're not exporting your module properly. 您没有正确导出模块。 It should be: 它应该是:

exports.demo = function ....

尝试在index.js中包含您的演示模块:
var demo = require('./demo.js');

For the other freshers who use module in node.js for the first time. 对于第一次在node.js中使用模块的其他新手。

first, made a new module called the name of your module.js Second, it is not necessary to do " npm install demo --save ", if you want, it is also okay. 首先,制作了一个名为module.js名称的新模块其次,没有必要做“ npm install demo --save ”,如果你愿意的话,也没关系。 Third, in the main js which u want to call this module, focus on the name and the path of the module, you should write var anyName = require('the name of your module'); 第三,在你想要调用这个模块的主要js中,关注模块的名称路径 ,你应该写var anyName = require('the name of your module'); , if they are in the same directory, you should write like this: var anyName = require('./the name of your module'); ,如果它们在同一个目录中,你应该这样写:var anyName = require('./你的模块的名字');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Sublime Text 3:构建系统 - node.js。 NPM 模块未执行 - Sublime Text 3: Build System - node.js. NPM module not executing file.open不是node.js中的函数。 文件读取 - file.open is not a function in node.js. file reading 使用Node.js邮寄。 主机属性出现问题 - Mailing with Node.js. Issue with the host property Node.JS 中的热交换。 可能吗? - Hot swapping in Node.JS. Possible? 有关Node.js的问题。 Schema和模型 - Questions about Node.js. SChema and models 将来自 sqlite3 的查询保存到 node.js 中的数组。 带异步功能 - save query from sqlite3 to array in node.js. with asynce function node.js中的回调函数。 TypeError:无法调用未定义的方法“ emit” - Callback function in node.js. TypeError: Cannot call method 'emit' of undefined AWS Lambda Node.js。 第二次调用后出现mysql错误“调用退出后无法排队查询”: - AWS Lambda Node.js. mysql Error "Cannot enqueue Query after invoking quit" on second call onwards: 使用mongoose,express和node.js搜索表单。 错误:发送标头后无法设置标头 - Search form using mongoose, express and node.js. Error: can't set headers after they are sent 尝试在 node.js 的子进程中使用 cd 时出错。 找不到资源 - Error when trying to use cd in a child process in node.js. Resource could not be found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM