简体   繁体   English

Node.js中的相对路径错误?

[英]Relative Path Error in Nodejs?

Hello This is my folder Structure and i want to require/import the serverError.log file in app.js. 您好,这是我的文件夹结构,我想在app.js中要求/导入serverError.log文件。

To require the config file i am using "require(../../../../../config/db.js)" and its works fine for db.js.But whenever i am giving readFile("../../../../../log/serverError.log"). 要使用配置文件,我使用的是“ require(../../../../../ config / db.js)”,它对于db.js正常工作。但是每当我给出readFile(“ ../../../../../log/serverError.log“)。

Its giveing no such file or directory, open '../../../../../log/serverError.log' .But its working fine in other mechine. 它没有给出这样的文件或目录,请打开“ ../../../../../log/serverError.log”。但是在其他机器上也可以正常工作。

So can anyone help me with this 所以有人可以帮我吗

/home/akash/MYB_GIT
└── mbox
    ├── config
    │   └── db.js
    ├── log
    │   └── serverError.log
    ├── services
    │   └── Node
    │       ├── api
    │       │   └── user
    │       │       └── web
    │       │           └── app.js
    │       └── modules
    │           ├── Admin
    │           │   └── api1.js
    │           ├── Guest
    │           │   └── api2.js
    │           └── user
    │               └── api3.js
    └── Test
        └── seneca_client
            ├── audit
            └── user

I would recommend you to use the PATH module, specifically path.resolve(). 我建议您使用PATH模块,特别是path.resolve()。

path.resolve will generate the absolute path to the item that you've supplied to the function. path.resolve将生成您提供给函数的项目的绝对路径。

So for example your code will be: 因此,例如,您的代码将是:

readFile(path.resolve("../../../../../log/serverError.log"))

https://nodejs.org/docs/latest/api/path.html https://nodejs.org/docs/latest/api/path.html

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

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