diff --git a/doc/api/errors.md b/doc/api/errors.md
index e25561147d571e..a11a94981fefa6 100644
--- a/doc/api/errors.md
+++ b/doc/api/errors.md
@@ -1921,6 +1921,13 @@ valid.
The imported module string is an invalid URL, package name, or package subpath
specifier.
+
+
+### `ERR_INVALID_OBJECT_DEFINE_PROPERTY`
+
+An error occurred while setting an invalid attribute on the property of
+an object.
+
### `ERR_INVALID_PACKAGE_CONFIG`
diff --git a/src/node_env_var.cc b/src/node_env_var.cc
index 27c833d498ec77..98f1ed07998e13 100644
--- a/src/node_env_var.cc
+++ b/src/node_env_var.cc
@@ -28,6 +28,7 @@ using v8::Nothing;
using v8::Object;
using v8::ObjectTemplate;
using v8::PropertyCallbackInfo;
+using v8::PropertyDescriptor;
using v8::PropertyHandlerFlags;
using v8::ReadOnly;
using v8::String;
@@ -396,11 +397,57 @@ static void EnvEnumerator(const PropertyCallbackInfo& info) {
env->env_vars()->Enumerate(env->isolate()));
}
+static void EnvDefiner(Local property,
+ const PropertyDescriptor& desc,
+ const PropertyCallbackInfo& info) {
+ Environment* env = Environment::GetCurrent(info);
+ if (desc.has_value()) {
+ if (!desc.has_writable() ||
+ !desc.has_enumerable() ||
+ !desc.has_configurable()) {
+ THROW_ERR_INVALID_OBJECT_DEFINE_PROPERTY(env,
+ "'process.env' only accepts a "
+ "configurable, writable,"
+ " and enumerable "
+ "data descriptor");
+ } else if (!desc.configurable() ||
+ !desc.enumerable() ||
+ !desc.writable()) {
+ THROW_ERR_INVALID_OBJECT_DEFINE_PROPERTY(env,
+ "'process.env' only accepts a "
+ "configurable, writable,"
+ " and enumerable "
+ "data descriptor");
+ } else {
+ return EnvSetter(property, desc.value(), info);
+ }
+ } else if (desc.has_get() || desc.has_set()) {
+ // we don't accept a getter/setter in 'process.env'
+ THROW_ERR_INVALID_OBJECT_DEFINE_PROPERTY(env,
+ "'process.env' does not accept an"
+ "accessor(getter/setter)"
+ " descriptor");
+ } else {
+ THROW_ERR_INVALID_OBJECT_DEFINE_PROPERTY(env,
+ "'process.env' only accepts a "
+ "configurable, writable,"
+ " and enumerable "
+ "data descriptor");
+ }
+}
+
MaybeLocal