Techletter #78 | June 22, 2024
What is monkey patching in JS?
Monkey patch is a technique that involves dynamically modifying or adding functionality to an existing object or function at runtime, without changing the source code.
Some of the use cases of monkey patching:
- If you encounter a bug in a library or framework you can use monkey patching to patch the bug at runtime using monkey patch.
- Add some functionality that is missing in an object or function
Example of monkey patch
const log = console.log;
console.log = function() {
log.apply(console, [(new Date()).toString()].concat(arguments));
};
console.log("test");
// output
Fri Jun 21 2024 16:26:36 GMT+0000 (Coordinated Universal Time) [Arguments] { '0': 'test' }
What do you mean by conditional exports in js?
Conditional exports in the package.json
file allows you to specify different module entry points depending on the environment or module system used. So, basically with conditional exports you specify entry points if you are using ES Modules, or entry point if you using commonJS.
"import"
: Matches when the package is loaded via ES moduleimport
orimport()
."require"
: Matches when the package is loaded via CommonJSrequire()
.
{
"name": "my-package",
"version": "1.0.0",
"exports": {
"import": "./main.mjs",
"require": "./main.cjs"
}
}
What events can block the event loop?
- Running an infinite loop in your code can block the event loop indefinitely. This is because the loop prevents the event loop from processing other tasks, including callbacks and timers.
- Using synchronous operations like
fs.readFileSync()
orrequire()
can block the event loop until the operation completes. - Recursively calling
process.nextTick()
can also block the event loop. This is because each call toprocess.nextTick()
adds a new task to the event loop, which can lead to a stack overflow if not handled carefully
What are the different kinds of import specifiers?
The below excerpt is from the nodejs documentaion
There are three types of specifiers:
- Relative specifiers like
'./startup.js'
or'../config.mjs'
. They refer to a path relative to the location of the importing file. The file extension is always necessary for these. - Bare specifiers like
'some-package'
or'some-package/shuffle'
. They can refer to the main entry point of a package by the package name, or a specific feature module within a package prefixed by the package name as per the examples respectively. Including the file extension is only necessary for packages without an"exports"
field. - Absolute specifiers like
'file:///opt/nodejs/config.js'
. They refer directly and explicitly to a full path.
What does moduleResolution: NodeNext in tsconfig.json?
NodeNext
is the recommended setting for modern Node.js versions (v12 and later), which supports both ESM and CJS.- When
moduleResolution
is set toNodeNext
, TypeScript uses a resolution strategy that is optimized for Node.js. This includes handling package.json exports and imports correctly. This setting ensures that relative import paths need explicit file extensions, which can help avoid issues with resolving imports. - The
module
option must be set tonodenext
whenmoduleResolution
is set tonodenext
. This ensures that the correct module-related code is emitted to JavaScript
What are triple-slash directives?
Triple-slash directives are special comments in TypeScript that provide instructions to the compiler about how to process a file. These directives begin with three consecutive slashes (///
) and are typically placed at the top of a TypeScript file. They have no effect on the runtime behavior of the code but influence how the compiler handles the file during compilation.