Skip to content

Node.js based forum software built for the modern web

refactor: switch to globalThis.nodebb

Barış Soner Uşaklı04abb3e96405
diff --git a/app.js b/app.js
index 8a95cfcf70..70d21c885a 100644
--- a/app.js
+++ b/app.js
@@ -20,7 +20,7 @@
'use strict';
require('./require-main');
-require('./nbb-require');
+require('./nodebb-global');
const nconf = require('nconf');
diff --git a/eslint.config.mjs b/eslint.config.mjs
index 9c7d2c3ab3..41f76593e5 100644
--- a/eslint.config.mjs
+++ b/eslint.config.mjs
@@ -67,7 +67,10 @@ export default defineConfig([
}
},
{
- files: ['src/test1.js'],
+ files: [
+ 'nodebb-global.js',
+ 'src/test1.js'
+ ],
languageOptions: {
sourceType: 'module',
}
diff --git a/install/package.json b/install/package.json
index 41c6eaba8d..2954aeab3f 100644
--- a/install/package.json
+++ b/install/package.json
@@ -171,7 +171,7 @@
"@eslint/js": "10.0.1",
"@stylistic/eslint-plugin": "5.10.0",
"ajv": "8.20.0",
- "eslint-config-nodebb": "2.0.1",
+ "eslint-config-nodebb": "2.0.2",
"globals": "17.6.0",
"grunt": "1.6.2",
"grunt-contrib-watch": "1.1.0",
diff --git a/nbb-require.js b/nbb-require.js
deleted file mode 100644
index 662facb54b..0000000000
--- a/nbb-require.js
+++ /dev/null
@@ -1,10 +0,0 @@
-
-import { createRequire } from 'node:module';
-
-const require = createRequire(import.meta.url);
-
-// this file is required by app.js and allows plugins to use `nbbRequire('./src/user')`
-// to import nbb core modules
-globalThis.nbbRequire = function (modulePath) {
- return require(modulePath);
-};
\ No newline at end of file
diff --git a/nodebb-global.js b/nodebb-global.js
new file mode 100644
index 0000000000..6f2e37b44a
--- /dev/null
+++ b/nodebb-global.js
@@ -0,0 +1,12 @@
+
+import { createRequire } from 'node:module';
+
+const require = createRequire(import.meta.url);
+
+// this file is required by app.js and allows plugins to use `nodebb.require('./src/user')`
+// to import nbb core modules, replaces the deprecated `require.main.require`
+globalThis.nodebb = {
+ require: function (modulePath) {
+ return require(modulePath);
+ },
+};
\ No newline at end of file
diff --git a/require-main.js b/require-main.js
index ed26ca1a0a..05ae7ad8de 100644
--- a/require-main.js
+++ b/require-main.js
@@ -5,6 +5,13 @@
// without worrying about multiple parent modules
if (require.main !== module) {
require.main.require = function (path) {
+ if (process.env.NODE_ENV === 'development') {
+ const e = (new Error().stack).split('\n')[2].trim();
+ if (e.includes('nodebb-')) { // only warn nodebb-plugin/theme etc.
+ const warningText = require('chalk').red(`Warning`);
+ console.warn(`${warningText}: require.main.require is going to be deprecated. Please use nodebb.require("${path}") instead.\n${e}\n`);
+ }
+ }
return require(path);
};
}