简体   繁体   中英

Relative Path Error in Nodejs?

Hello This is my folder Structure and i want to require/import the serverError.log file in app.js.

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").

Its giveing no such file or directory, open '../../../../../log/serverError.log' .But its working fine in other mechine.

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.resolve will generate the absolute path to the item that you've supplied to the function.

So for example your code will be:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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