2023-02-18 02:49:18 +00:00
|
|
|
const { Module: { createRequire } } = require('module');
|
|
|
|
const createdRequire = createRequire(__filename);
|
|
|
|
|
|
|
|
// Although, require('../common') works locally, that couldn't be used here
|
|
|
|
// because we set NODE_TEST_DIR=/Users/iojs/node-tmp on Jenkins CI.
|
2023-05-04 15:27:54 +00:00
|
|
|
const { expectWarning, mustNotCall } = createdRequire(process.env.COMMON_DIRECTORY);
|
2023-02-18 02:49:18 +00:00
|
|
|
|
2024-07-05 19:58:35 +00:00
|
|
|
const builtinWarning =
|
|
|
|
`Currently the require() provided to the main script embedded into single-executable applications only supports loading built-in modules.
|
|
|
|
To load a module from disk after the single executable application is launched, use require("module").createRequire().
|
|
|
|
Support for bundled module loading or virtual file systems are under discussions in https://github.com/nodejs/single-executable`;
|
|
|
|
|
|
|
|
expectWarning('Warning', builtinWarning); // Triggered by require() calls below.
|
2023-07-26 10:10:35 +00:00
|
|
|
// This additionally makes sure that no unexpected warnings are emitted.
|
2024-07-05 19:58:35 +00:00
|
|
|
if (!createdRequire('./sea-config.json').disableExperimentalSEAWarning) {
|
2023-05-04 15:27:54 +00:00
|
|
|
expectWarning('ExperimentalWarning',
|
|
|
|
'Single executable application is an experimental feature and ' +
|
|
|
|
'might change at any time');
|
2023-07-26 10:10:35 +00:00
|
|
|
// Any unexpected warning would throw this error:
|
|
|
|
// https://github.com/nodejs/node/blob/c301404105a7256b79a0b8c4522ce47af96dfa17/test/common/index.js#L697-L700.
|
2023-05-04 15:27:54 +00:00
|
|
|
}
|
2023-02-18 02:49:18 +00:00
|
|
|
|
2023-05-04 14:32:35 +00:00
|
|
|
// Should be possible to require core modules that optionally require the
|
|
|
|
// "node:" scheme.
|
2023-02-18 02:49:18 +00:00
|
|
|
const { deepStrictEqual, strictEqual, throws } = require('assert');
|
2023-05-04 14:32:35 +00:00
|
|
|
const { dirname } = require('node:path');
|
|
|
|
|
2023-07-26 10:10:35 +00:00
|
|
|
// Checks that the source filename is used in the error stack trace.
|
2024-07-05 19:58:35 +00:00
|
|
|
strictEqual(new Error('lol').stack.split('\n')[1], ' at sea.js:29:13');
|
2023-07-26 10:10:35 +00:00
|
|
|
|
2023-05-04 14:32:35 +00:00
|
|
|
// Should be possible to require a core module that requires using the "node:"
|
|
|
|
// scheme.
|
|
|
|
{
|
|
|
|
const { test } = require('node:test');
|
|
|
|
strictEqual(typeof test, 'function');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should not be possible to require a core module without the "node:" scheme if
|
|
|
|
// it requires using the "node:" scheme.
|
|
|
|
throws(() => require('test'), {
|
|
|
|
code: 'ERR_UNKNOWN_BUILTIN_MODULE',
|
|
|
|
});
|
2023-02-18 02:49:18 +00:00
|
|
|
|
|
|
|
deepStrictEqual(process.argv, [process.execPath, process.execPath, '-a', '--b=c', 'd']);
|
|
|
|
|
|
|
|
strictEqual(require.cache, undefined);
|
|
|
|
strictEqual(require.extensions, undefined);
|
|
|
|
strictEqual(require.main, module);
|
|
|
|
strictEqual(require.resolve, undefined);
|
|
|
|
|
|
|
|
strictEqual(__filename, process.execPath);
|
|
|
|
strictEqual(__dirname, dirname(process.execPath));
|
|
|
|
strictEqual(module.exports, exports);
|
|
|
|
|
|
|
|
throws(() => require('./requirable.js'), {
|
|
|
|
code: 'ERR_UNKNOWN_BUILTIN_MODULE',
|
|
|
|
});
|
|
|
|
|
|
|
|
const requirable = createdRequire('./requirable.js');
|
|
|
|
deepStrictEqual(requirable, {
|
|
|
|
hello: 'world',
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log('Hello, world! 😊');
|