in src/UserFunction.js [138:194]
async function _tryRequire(appRoot, moduleRoot, module) {
verbose(
'Try loading as commonjs: ',
module,
' with paths: ,',
appRoot,
moduleRoot,
);
const lambdaStylePath = path.resolve(appRoot, moduleRoot, module);
// Extensionless files are loaded via require.
const extensionless = _tryRequireFile(lambdaStylePath);
if (extensionless) {
return extensionless;
}
// If package.json type != module, .js files are loaded via require.
const pjHasModule = _hasPackageJsonTypeModule(lambdaStylePath);
if (!pjHasModule) {
const loaded = _tryRequireFile(lambdaStylePath, '.js');
if (loaded) {
return loaded;
}
}
// If still not loaded, try .js, .mjs, and .cjs in that order.
// Files ending with .js are loaded as ES modules when the nearest parent package.json
// file contains a top-level field "type" with a value of "module".
// https://nodejs.org/api/packages.html#packages_type
const loaded =
(pjHasModule && (await _tryAwaitImport(lambdaStylePath, '.js'))) ||
(await _tryAwaitImport(lambdaStylePath, '.mjs')) ||
_tryRequireFile(lambdaStylePath, '.cjs');
if (loaded) {
return loaded;
}
verbose(
'Try loading as commonjs: ',
module,
' with path(s): ',
appRoot,
moduleRoot,
);
// Why not just require(module)?
// Because require() is relative to __dirname, not process.cwd(). And the
// runtime implementation is not located in /var/task
// This won't work (yet) for esModules as import.meta.resolve is still experimental
// See: https://nodejs.org/api/esm.html#esm_import_meta_resolve_specifier_parent
const nodeStylePath = require.resolve(module, {
paths: [appRoot, moduleRoot],
});
return require(nodeStylePath);
}