Node.js,无法打开文件.错误:ENOENT,统计 './path/to/file' [英] Node.js, can't open files. Error: ENOENT, stat './path/to/file'

查看:32
本文介绍了Node.js,无法打开文件.错误:ENOENT,统计 './path/to/file'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的电脑上使用 express 框架开发了一个 node.js 程序,它运行良好,没有任何抱怨.

I have developed a node.js program using the express framework on my computer, where it runs fine with no complaints.

但是,当我在 SUSE Studio 设备上运行该程序时,我会在任何文件交互中收到错误消息.

However, when I run the program on my SUSE Studio appliance, where it is intended to live, I receive an error at any file interaction.

Error: ENOENT, stat './path/to/file'

我已经检查过文件权限是否正确,确实如此.如果这很重要,我的计算机和我的设备正在运行不同版本的节点.

I have checked that the file permissions are correct, which they are. My computer and my appliance are running different versions of node, if this matters.

有什么想法吗?

推荐答案

. 指定的路径是相对于当前工作目录的,而不是相对于脚本文件的.因此,如果您运行 node app.js 可能会找到该文件,但如果您运行 node folder/app.js 则不会找到该文件.唯一的例外是 require('./file') 并且这是唯一可能的,因为 require 每个模块都存在,因此知道从哪个模块调用它.

Paths specified with a . are relative to the current working directory, not relative to the script file. So the file might be found if you run node app.js but not if you run node folder/app.js. The only exception to this is require('./file') and that is only possible because require exists per-module and thus knows what module it is being called from.

要创建相对于脚本的路径,您必须使用 __dirname 变量.

To make a path relative to the script, you must use the __dirname variable.

var path = require('path');

path.join(__dirname, 'path/to/file')

或可能

path.join(__dirname, 'path', 'to', 'file')

这篇关于Node.js,无法打开文件.错误:ENOENT,统计 './path/to/file'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆