Skip to content

Commit 3b8c8e5

Browse files
committed
Group require from node_modules
1 parent 3a50182 commit 3b8c8e5

File tree

6 files changed

+737
-0
lines changed

6 files changed

+737
-0
lines changed

NodeJS/1-npm/application.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const { fs, path, vm, os, http, util, crypto, timers } = require('./node.js');
4+
const { pg, redis, ws, metacom, metalog, metavm } = require('./npm.js');
5+
6+
// Use modules as usual
7+
8+
fs.readFile('./application.js', (error, data) => {
9+
if (error) {
10+
console.log({ error });
11+
} else {
12+
console.log({ data });
13+
}
14+
});
15+
16+
// Show what we have
17+
18+
const node = { fs, path, vm, os, http, util, crypto, timers };
19+
const npm = { pg, redis, ws, metacom, metalog, metavm };
20+
console.log({ node, npm });

NodeJS/1-npm/node.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
const node = {};
4+
const tools = ['util', 'path', 'buffer', 'os', 'v8', 'vm'];
5+
const multi = ['child_process', 'worker_threads'];
6+
const streams = ['stream', 'fs', 'crypto', 'zlib', 'readline'];
7+
const async = ['perf_hooks', 'async_hooks', 'timers', 'events'];
8+
const network = ['dns', 'net', 'tls', 'http', 'https', 'http2', 'dgram'];
9+
const internals = [...tools, ...multi, ...streams, ...async, ...network];
10+
11+
for (const name of internals) node[name] = require(`node:${name}`);
12+
node.process = process;
13+
node.fsp = node.fs.promises;
14+
node.timers.promises = node['timers/promises'];
15+
16+
module.exports = Object.freeze(node);

NodeJS/1-npm/npm.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
const node = require('./node.js');
4+
const npm = {};
5+
6+
const pkgPath = node.path.join(node.process.cwd(), 'package.json');
7+
const pkg = require(pkgPath);
8+
9+
if (pkg.dependencies) {
10+
const modules = Object.keys(pkg.dependencies);
11+
for (const dependency of modules) {
12+
npm[dependency] = require(dependency);
13+
}
14+
}
15+
16+
module.exports = Object.freeze(npm);

0 commit comments

Comments
 (0)