简体   繁体   English

NodeJ需要错误

[英]NodeJs require error

I have a nodejs package and i have a linkedList code. 我有一个nodejs包,我有一个LinkedList代码。 I want to include my LinkedList code in my NodeJs js folder but I can't do it. 我想在我的NodeJs js文件夹中包含我的LinkedList代码,但是我做不到。 What is the my fault ? 我的错是什么? I want to use it like this 我想这样使用

This is my code in my NodeJs folder.(app.js) 这是我的NodeJs文件夹中的代码。(app.js)

var KullaniciJS=require('KullaniciLibrary.js');

This is my required folder codes.(KullaniciLibrary.js) 这是我所需的文件夹代码。(KullaniciLibrary.js)

function Kullanici(KullaniciAdi)
{
    this.KullaniciAdi=KullaniciAdi;
    this.Sonraki=null;
}
function KullaniciListe()
{
    this.Bas=null;
    this.Uzunluk=0;
}
KullaniciListe.prototype.Ekle=function(EklenecekKullaniciAdi)
{
    var Bas=this.Bas;
    if (!Bas) {
        this.Bas = EklenecekKullaniciAdi;
        this.Uzunluk++;

        return EklenecekKullaniciAdi;
    }
    while (Bas.Sonraki!==null) {

        Bas = Bas.Sonraki;
    }

    Bas.Sonraki = EklenecekKullaniciAdi;

    this.Uzunluk++;

    return EklenecekKullaniciAdi;
};

First you have to declare inside of your KullaniciLibrary.js file, what you want to export. 首先,您必须在KullaniciLibrary.js文件中声明要导出的内容。 you do this by adding following line at the end of the file: 您可以通过在文件末尾添加以下行来做到这一点:

module.exports = YOUREXPORT;

YOUREXPORT will probably just be the methodname (or some object). YOUREXPORT可能只是方法名(或某些对象)。 After that, you wanna import it like this: 之后,您想要这样导入:

var KullaniciJS = require('./KullaniciLibrary');

Note that you have to prefix the name with ./ and you don't need to define the file-ending. 请注意,您必须在名称前加上./并且不需要定义文件结尾。

If you don't prefix the module with ./ , node will search for the module in the node_modules folder, instead of the current directory 如果您未在模块前添加./ ,则node将在node_modules文件夹中而不是当前目录中搜索模块。

In nodejs to require a function from another file you have to export then function. 在nodejs中,如果要从另一个文件中获取功能,则必须先导出然后再运行。 To export the function use: 要导出功能,请使用:

module.export='your function name';

and to require the function use: 并要求功能使用:

const variable=require(./filename)

for more understanding use this link: 要了解更多信息,请使用此链接:

https://nodejs.org/api/modules.html https://nodejs.org/api/modules.html

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM