Skip to content

Commit 14b6713

Browse files
committed
src: add option to disable global search paths
1 parent 6145113 commit 14b6713

File tree

7 files changed

+31
-5
lines changed

7 files changed

+31
-5
lines changed

lib/internal/bootstrap/pre_execution.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ const {
1111

1212
const {
1313
getOptionValue,
14-
shouldNotRegisterESMLoader
14+
shouldNotRegisterESMLoader,
15+
noGlobalSearchPaths
1516
} = require('internal/options');
1617
const { reconnectZeroFillToggle } = require('internal/buffer');
1718

@@ -420,7 +421,9 @@ function initializeWASI() {
420421

421422
function initializeCJSLoader() {
422423
const CJSLoader = require('internal/modules/cjs/loader');
423-
CJSLoader.Module._initPaths();
424+
if (!noGlobalSearchPaths) {
425+
CJSLoader.Module._initPaths();
426+
}
424427
// TODO(joyeecheung): deprecate this in favor of a proper hook?
425428
CJSLoader.Module.runMain =
426429
require('internal/modules/run_main').executeUserEntryPoint;

lib/internal/options.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
'use strict';
22

3-
const { getOptions, shouldNotRegisterESMLoader } = internalBinding('options');
3+
const {
4+
getOptions,
5+
shouldNotRegisterESMLoader,
6+
noGlobalSearchPaths
7+
} = internalBinding('options');
48

59
let warnOnAllowUnauthorized = true;
610

@@ -57,5 +61,6 @@ module.exports = {
5761
},
5862
getOptionValue,
5963
getAllowUnauthorized,
60-
shouldNotRegisterESMLoader
64+
shouldNotRegisterESMLoader,
65+
noGlobalSearchPaths
6166
};

src/env-inl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,10 @@ inline bool Environment::tracks_unmanaged_fds() const {
877877
return flags_ & EnvironmentFlags::kTrackUnmanagedFds;
878878
}
879879

880+
inline bool Environment::no_global_search_paths() const {
881+
return flags_ & EnvironmentFlags::kNoGlobalSearchPaths;
882+
}
883+
880884
bool Environment::filehandle_close_warning() const {
881885
return emit_filehandle_warning_;
882886
}

src/env.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,7 @@ class Environment : public MemoryRetainer {
11981198
inline bool owns_process_state() const;
11991199
inline bool owns_inspector() const;
12001200
inline bool tracks_unmanaged_fds() const;
1201+
inline bool no_global_search_paths() const;
12011202
inline uint64_t thread_id() const;
12021203
inline worker::Worker* worker_context() const;
12031204
Environment* worker_parent_env() const;

src/node.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,12 @@ enum Flags : uint64_t {
403403
kNoRegisterESMLoader = 1 << 3,
404404
// Set this flag to make Node.js track "raw" file descriptors, i.e. managed
405405
// by fs.open() and fs.close(), and close them during FreeEnvironment().
406-
kTrackUnmanagedFds = 1 << 4
406+
kTrackUnmanagedFds = 1 << 4,
407+
// Set this flag to disable searching modules from global paths like
408+
// $HOME/.node_modules and $NODE_PATH. This is used by standalone apps that
409+
// do not expect to have their behaviors changed because of globally
410+
// installed modules.
411+
kNoGlobalSearchPaths = 1 << 5
407412
};
408413
} // namespace EnvironmentFlags
409414

src/node_options.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,12 @@ void Initialize(Local<Object> target,
10681068
Boolean::New(isolate, env->should_not_register_esm_loader()))
10691069
.Check();
10701070

1071+
target
1072+
->Set(context,
1073+
FIXED_ONE_BYTE_STRING(env->isolate(), "noGlobalSearchPaths"),
1074+
Boolean::New(isolate, env->no_global_search_paths()))
1075+
.Check();
1076+
10711077
Local<Object> types = Object::New(isolate);
10721078
NODE_DEFINE_CONSTANT(types, kNoOp);
10731079
NODE_DEFINE_CONSTANT(types, kV8Option);

src/node_worker.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,8 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {
558558
CHECK(args[4]->IsBoolean());
559559
if (args[4]->IsTrue() || env->tracks_unmanaged_fds())
560560
worker->environment_flags_ |= EnvironmentFlags::kTrackUnmanagedFds;
561+
if (env->no_global_search_paths())
562+
worker->environment_flags_ |= EnvironmentFlags::kNoGlobalSearchPaths;
561563
}
562564

563565
void Worker::StartThread(const FunctionCallbackInfo<Value>& args) {

0 commit comments

Comments
 (0)