Register Node Module Manually Register Node Module Manually typescript typescript

Register Node Module Manually


As you mentioned, compiling works fine - this is just a question of availability of .d.ts files.

What you want to do is alter module import at runtime, in other words alter the behaviour of the nodejs require function since

import {MessageSender} from "messages";

will be transpiled in javascript (ES6) to something like

const messages_1 = require("messages");...messages_1.MessageSender

To modify that behaviour, the first thing that springs to mind is to use the deprecated - but still available - require.extensions object.

When running locally you must first inject something like

require.extensions['.js'] = (module, filename) => {    if (filename === 'messages') {        // then load mock module/polyfill using the passed module object        // see (https://nodejs.org/api/modules.html#modules_the_module_object)    }};

The doc says there are better alternatives but fails to clearly mention any.

Another possibility is to look at projects like sandboxed-module which should help (I have not tested it)