diff --git a/doc/api/errors.md b/doc/api/errors.md
index f721ed221eab21..b5f728210a0a7c 100644
--- a/doc/api/errors.md
+++ b/doc/api/errors.md
@@ -1116,6 +1116,14 @@ added:
An attempt to invoke an unsupported crypto operation was made.
+
+
+### `ERR_CWD_DELETED`
+
+The current working directory has been deleted while the process was still inside it.
+
+To resolve this, use `process.chdir()` to switch to a valid directory.
+
### `ERR_DEBUGGER_ERROR`
diff --git a/lib/internal/bootstrap/switches/does_own_process_state.js b/lib/internal/bootstrap/switches/does_own_process_state.js
index 370da66a825f65..5528df3338e3de 100644
--- a/lib/internal/bootstrap/switches/does_own_process_state.js
+++ b/lib/internal/bootstrap/switches/does_own_process_state.js
@@ -138,7 +138,8 @@ function wrappedUmask(mask) {
}
function wrappedCwd() {
- if (cachedCwd === '')
+ if (cachedCwd === '') {
cachedCwd = rawMethods.cwd();
+ }
return cachedCwd;
}
diff --git a/src/node_errors.h b/src/node_errors.h
index 801b19fc91810b..6f246e16cd1857 100644
--- a/src/node_errors.h
+++ b/src/node_errors.h
@@ -37,6 +37,7 @@ void OOMErrorHandler(const char* location, const v8::OOMDetails& details);
#define ERRORS_WITH_CODE(V) \
V(ERR_ACCESS_DENIED, Error) \
+ V(ERR_CWD_DELETED, Error) \
V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE, Error) \
V(ERR_BUFFER_OUT_OF_BOUNDS, RangeError) \
V(ERR_BUFFER_TOO_LARGE, Error) \
@@ -160,6 +161,10 @@ ERRORS_WITH_CODE(V)
V(ERR_ACCESS_DENIED, "Access to this API has been restricted") \
V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE, \
"Buffer is not available for the current Context") \
+ V(ERR_CWD_DELETED, \
+ "Current working directory does not exist. " \
+ "It may have been deleted while the process was still " \
+ "inside it. Use process.chdir() to change to a valid directory.") \
V(ERR_CLOSED_MESSAGE_PORT, "Cannot send data on closed MessagePort") \
V(ERR_CONSTRUCT_CALL_INVALID, "Constructor cannot be called") \
V(ERR_CONSTRUCT_CALL_REQUIRED, "Cannot call constructor without `new`") \
diff --git a/src/node_process_methods.cc b/src/node_process_methods.cc
index c67d1ac00a972b..090e3b61070ebd 100644
--- a/src/node_process_methods.cc
+++ b/src/node_process_methods.cc
@@ -136,6 +136,10 @@ static void Cwd(const FunctionCallbackInfo& args) {
char buf[PATH_MAX_BYTES];
size_t cwd_len = sizeof(buf);
int err = uv_cwd(buf, &cwd_len);
+ if (err == UV_ENOENT) {
+ THROW_ERR_CWD_DELETED(env);
+ return;
+ }
if (err)
return env->ThrowUVException(err, "uv_cwd");