Skip to content

IResolveNodeModules

David Ortner edited this page Jan 7, 2026 · 2 revisions

IResolveNodeModules represents configurations for a node module resolver.

A IVirtualServer will be added automatically under the specified URL. The modules will then be resolved to the specified URL path internally.

Note that CommonJS is not supported as format and there are no polyfills for server-side node packages.

Signature

interface IResolveNodeModules

Properties

Property Type Default Description
url string URL for the virtual server. The modules will be resolved to this path internally. The name itself doesn't matter for the logic to work as long as it's unique. (e.g. "/my-unique-path-for-node-modules/")
directory string Path to the "node_modules" directory (e.g. "./node_modules")
mainFields? string[] ["module", "main"] Array of main fields to use in the "package.json" files (in order)

Example

import { Window, ISyncResponse } from "happy-dom";

const window = new Window({
   enableJavaScriptEvaluation: true,
   settings: {
      module: : {
         resolveNodeModules: {
            url: '/my-unique-path-for-node-modules/',
            directory: './node_modules'
         }
      }
   }
});

const document = window.document;
const script = document.createElement('script');

script.type = "module";
script.textContent = `import "path-to-package";`;

// The module specified in the "package.json" file for the package "path-to-package" will now be loaded from the "node_modules" directory
document.body.appendChild(script);

Clone this wiki locally