Skip to content

globalPreload docs have non-working code #50279

@bmacnaughton

Description

@bmacnaughton

Affected URL(s)

https://nodejs.org/docs/latest-v16.x/api/esm.html#globalpreload
https://nodejs.org/docs/latest-v18.x/api/esm.html#globalpreload

Description of the problem

The example code from the docs doesn't work.

port.onmessage doesn't work. the code needs to be port.on('message', ...). Additionally, there is just data, not evt.data.

I know that globalPreload is gone in 20, so feel free to close this. I am working with making loaders work across multiple versions of node and this was a problem that I ran into.

/**
 * This example has the application context send a message to the loader
 * and sends the message back to the application context
 */
export function globalPreload({ port }) {
  port.onmessage = (evt) => {
    port.postMessage(evt.data);
  };
  return `\
    port.postMessage('console.log("I went to the Loader and back");');
    port.onmessage = (evt) => {
      eval(evt.data);
    };
  `;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to the documentations.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions