简体   繁体   English

在不同环境中控制相对文件路径的node.js-例如pm2 vs vanilla node

[英]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: 在Mac上可以正常工作,但是在Ubuntu上却出现错误:

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

On mac I'm using node v5.5.0 在Mac上,我正在使用节点v5.5.0

on Ubuntu I'm using node v0.10.25, and using pm2. 在Ubuntu上,我正在使用节点v0.10.25,并使用pm2。

======= Edit =======编辑

See below for comments on the solution 请参阅下面的解决方案评论

The answer to this question was the comment by Evan Lucas: 这个问题的答案是埃文·卢卡斯(Evan Lucas)的评论:

When using a relative path, it will be relative to the cwd of the process. 使用相对路径时,它将相对于进程的cwd。 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') 如果相对于文件,则可以使用path.join(__ dirname,'./src/config')

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

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