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

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

问题描述

我已经开发了一个node.js程序,使用我的电脑上的express框架,运行良好,没有投诉。

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'),这只能是因为需要模块,从而知道它正在调用哪个模块。

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,stat'./path/to/file'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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