简体   繁体   中英

node.js controlling relative filepaths in different environments - pm2 vs vanilla node for example

I have a line of code:

var currentDir = './src/config';
fs.readdirSync(currentDir).forEach(function(dir) {...

This works fine on Mac, but on Ubuntu I'm getting an error:

Error: ENOENT, no such file or directory './src/config'

On mac I'm using node v5.5.0

on Ubuntu I'm using node v0.10.25, and using pm2.

======= Edit

See below for comments on the solution

The answer to this question was the comment by Evan Lucas:

When using a relative path, it will be relative to the cwd of the process. I would probably go for using an absolute path by resolving the relative file path to wherever you need it to be to be on the safe side. If it is relative to the file, you can use path.join(__dirname, './src/config')

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