node.js ENOENT,fs.mkdirSync 上没有这样的文件或目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28498296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ENOENT, no such file or directory on fs.mkdirSync
提问by Ruben Rutten
I'm currently starting up my NodeJS application and I have the following if-statement:
我目前正在启动我的 NodeJS 应用程序,我有以下 if 语句:
Error: ENOENT, no such file or directory './realworks/objects/'
at Object.fs.mkdirSync (fs.js:654:18)
at Object.module.exports.StartScript (/home/nodeusr/huizenier.nl/realworks.js:294:7)
The weird thing, however, is that the folder exists already, but the check fails on the following snippet:
然而,奇怪的是,该文件夹已经存在,但在以下代码段中检查失败:
if(fs.existsSync(objectPath)) {
var existingObjects = fs.readdirSync(objectPath);
existingObjects.forEach(function (objectFile) {
var object = JSON.parse(fs.readFileSync(objectPath+objectFile));
actualObjects[object.ObjectCode] = object;
});
}else{
fs.mkdirSync(objectPath); // << this is line 294
}
I fail to understand how a no such file or directorycan occur on CREATING a directory.
我无法理解如何no such file or directory在创建目录时发生。
回答by Yoav Kadosh
When any folder along the given path is missing, mkdirwill throw an ENOENT.
当给定路径上的任何文件夹丢失时,mkdir将抛出一个ENOENT.
There are 2 possible solutions (without using 3rd party packages):
有两种可能的解决方案(不使用第 3 方软件包):
- Recursively call
fs.mkdirfor every non-existent directory along the path. - Use the
recursiveoption, introduced in v10.12:fs.mkdir('./path/to/dir', {recursive: true}, err => {})
fs.mkdir沿路径递归调用每个不存在的目录。- 使用v10.12 中
recursive引入的选项:fs.mkdir('./path/to/dir', {recursive: true}, err => {})
回答by Abhishek Kumar
When you are using fs.mkdiror fs.mkdirSync, while passing the path like folder1/folder2/folder3, folder1and folder2must exist otherwise you will get the above error.
当您使用fs.mkdiror 时fs.mkdirSync,在传递类似folder1/folder2/folder3, folder1and的路径时folder2必须存在,否则您将收到上述错误。
回答by ztvmark
Solve here How to create full path with node's fs.mkdirSync?
在这里解决 如何使用节点的 fs.mkdirSync 创建完整路径?
NodeJS version 10.12.0 has added a native support for both mkdir and mkdirSync to create a directory recursively with recursive: true option as the following:
NodeJS 10.12.0 版添加了对 mkdir 和 mkdirSync 的原生支持,以递归方式创建目录,并使用 recursive: true 选项,如下所示:
fs.mkdirSync(targetDir, { recursive: true });
And if you prefer fs Promises API, you can write
如果你更喜欢 fs Promises API,你可以写
fs.promises.mkdir(targetDir, { recursive: true });
回答by boblapointe
The following worked for me:
以下对我有用:
fs.mkdir( __dirname + '/realworks/', err => {})
回答by wibobm
The reason for the error is that if any of the folders exist along the path given to fs.mkdir or fs.mkdirSync these methods will throw/callback with an ENOENT error.
错误的原因是,如果在给 fs.mkdir 或 fs.mkdirSync 的路径上存在任何文件夹,这些方法将抛出/回调,并带有 ENOENT 错误。
回答by Ruben Rutten
Problem was caused by foreverrunning the application relative to the working directory the forever startcommand is called in, not the location of the application entrypoint.
问题是由forever相对于forever start调用命令的工作目录而不是应用程序入口点的位置运行应用程序引起的。
回答by Quentin Zhang
Try:
尝试:
fs.mkdir('./realworks/', err => {})
回答by Philippe Hebert
ENOENT is described in the linux documentationas the following:
ENOENT 在linux 文档中描述如下:
No such file or directory (POSIX.1-2001).
Typically, this error results when a specified path‐ name does not exist, or one of the components in the directory prefix of a pathname does not exist, or the specified pathname is a dangling symbolic link.
没有这样的文件或目录(POSIX.1-2001)。
通常,当指定的路径名不存在,或者路径名的目录前缀中的组件之一不存在,或者指定的路径名是悬空的符号链接时,会导致此错误。
Another possible reason for ENOENT is that you lack sufficient privileges to create the directory.
ENOENT 的另一个可能原因是您没有足够的权限来创建目录。
This happened to me while building a docker image where I didn't have sufficient privilege to create a subfolder in the current WORKDIR. Changing the owner of the folder using --chown=user:usergroupOR changing the USERto the rootuser for the directive were both valid solutions to the problem.
这发生在我构建 docker 镜像时,我没有足够的权限在当前的 WORKDIR 中创建子文件夹。使用--chown=user:usergroup或将指令更改USER为root用户来更改文件夹的所有者都是解决该问题的有效方法。

