diff --git a/src/bun.js/modules/NodeModuleModule.cpp b/src/bun.js/modules/NodeModuleModule.cpp index 0c9fdb2962..f296dbb207 100644 --- a/src/bun.js/modules/NodeModuleModule.cpp +++ b/src/bun.js/modules/NodeModuleModule.cpp @@ -712,11 +712,6 @@ JSC_DEFINE_CUSTOM_SETTER(setNodeModuleWrapper, return true; } -JSC_DEFINE_HOST_FUNCTION(jsFunctionInitPaths, (JSGlobalObject * globalObject, JSC::CallFrame* callFrame)) -{ - return JSC::JSValue::encode(JSC::jsUndefined()); -} - static JSValue getModulePrototypeObject(VM& vm, JSObject* moduleObject) { auto* globalObject = defaultGlobalObject(moduleObject->globalObject()); @@ -849,8 +844,8 @@ static JSValue getModuleObject(VM& vm, JSObject* moduleObject) _cache getModuleCacheObject PropertyCallback _debug getModuleDebugObject PropertyCallback _extensions getModuleExtensionsObject PropertyCallback -_findPath jsFunctionFindPath Function 3 -_initPaths jsFunctionInitPaths Function 0 +_findPath jsFunctionFindPath Function 3 +_initPaths JSBuiltin Function|Builtin 0 _load jsFunctionLoad Function 1 _nodeModulePaths Resolver__nodeModulePathsForJS Function 1 _pathCache getPathCacheObject PropertyCallback diff --git a/src/codegen/bundle-functions.ts b/src/codegen/bundle-functions.ts index 003b0e2276..9e55ebc53b 100644 --- a/src/codegen/bundle-functions.ts +++ b/src/codegen/bundle-functions.ts @@ -146,7 +146,7 @@ async function processFileSplit(filename: string): Promise<{ functions: BundledB contents = contents.slice(directive[0].length); } else if (match[1] === "export function" || match[1] === "export async function") { // consume async token and function name - const nameMatch = contents.match(/^export\s+(async\s+)?function\s([a-zA-Z0-9]+)\s*/); + const nameMatch = contents.match(/^export\s+(async\s+)?function\s([_a-zA-Z0-9]+)\s*/); if (!nameMatch) throw new SyntaxError("Could not parse function name:\n" + contents.slice(0, contents.indexOf("\n"))); const async = Boolean(nameMatch[1]); diff --git a/src/js/builtins/NodeModuleObject.ts b/src/js/builtins/NodeModuleObject.ts new file mode 100644 index 0000000000..8c47278608 --- /dev/null +++ b/src/js/builtins/NodeModuleObject.ts @@ -0,0 +1,27 @@ +// Implementation for `require('node:module')._initPaths`. Exists only as a +// compatibility stub. Calling this does not affect the actual CommonJS loader. +export function _initPaths() { + const homeDir = process.platform === "win32" ? process.env.USERPROFILE : Bun.env.HOME; + const nodePath = process.platform === "win32" ? process.env.NODE_PATH : Bun.env.NODE_PATH; + + // process.execPath is $PREFIX/bin/node except on Windows where it is + // $PREFIX\node.exe where $PREFIX is the root of the Node.js installation. + const path = require("node:path"); + const prefixDir = process.platform === "win32" ? + path.resolve(process.execPath, '..') : + path.resolve(process.execPath, '..', '..'); + + const paths = [path.resolve(prefixDir, 'lib', 'node')]; + + if (homeDir) { + paths.unshift(path.resolve(homeDir, '.node_libraries')); + paths.unshift(path.resolve(homeDir, '.node_modules')); + } + + if (nodePath) { + paths.unshift(...nodePath.split(path.delimiter).filter(Boolean)); + } + + const M = require('node:module'); + M.globalPaths = paths; +} diff --git a/test/js/node/test/parallel/test-module-globalpaths-nodepath.js b/test/js/node/test/parallel/test-module-globalpaths-nodepath.js new file mode 100644 index 0000000000..c4492169ad --- /dev/null +++ b/test/js/node/test/parallel/test-module-globalpaths-nodepath.js @@ -0,0 +1,46 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const mod = require('module'); + +let partA, partB; +const partC = ''; + +if (common.isWindows) { + partA = 'C:\\Users\\Rocko Artischocko\\AppData\\Roaming\\npm'; + partB = 'C:\\Program Files (x86)\\nodejs\\'; + process.env.NODE_PATH = `${partA};${partB};${partC}`; +} else { + partA = '/usr/test/lib/node_modules'; + partB = '/usr/test/lib/node'; + process.env.NODE_PATH = `${partA}:${partB}:${partC}`; +} + +mod._initPaths(); + +assert.ok(mod.globalPaths.includes(partA)); +assert.ok(mod.globalPaths.includes(partB)); +assert.ok(!mod.globalPaths.includes(partC)); + +assert.ok(Array.isArray(mod.globalPaths));