Skip to content

"SyntaxError" error when importing from @module-federation/runtime with an ESM project #2383

@patricklafrance

Description

@patricklafrance

Describe the bug

I am trying to import loadRemote from @module-federation/runtime but I keep getting errors like:

samples/basic/another-remote-module dev: (node:90956) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
samples/basic/another-remote-module dev: (Use `node --trace-warnings ...` to show where the warning was created)
samples/basic/host dev: (node:72552) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.

samples/basic/another-remote-module dev: import { g as getGlobalHostPlugins, a as globalLoading, D as DEFAULT_REMOTE_TYPE, b as DEFAULT_SCOPE, c as getRemoteEntryExports, d as assert, s as safeToString, e as getFMId, i as isObject, f as error, w as warn, h as isPlainObject, j as isRemoteInfoWithEntry, k as isPureRemoteEntry, l as getInfoWithoutType, m as getPreloaded, n as setPreloaded, o as getRegisteredShare, p as arrayOptions, q as getGlobalSnapshotInfoByModuleInfo, r as addGlobalSnapshot, t as setGlobalSnapshotInfoByModuleInfo, u as getGlobalSnapshot, G as Global, v as getGlobalShareScope, x as getTargetSharedOptions, y as formatShareConfigs, z as getBuilderId, A as isBrowserEnv, B as addUniqueItem, C as setGlobalFederationConstructor, E as getGlobalFederationInstance, F as getGlobalFederationConstructor, H as setGlobalFederationInstance } from './share.esm.js';

SyntaxError: Cannot use import statement outside a module

My project is an ESM project with type: module in the package.json file.

I tried a few syntaxes but without success:

import { loadRemote } from "@module-federation/runtime";
import FederationRuntime from "@module-federation/runtime";

FederationRuntime.loadRemote(...);
import * as FederationRuntime from "@module-federation/runtime";

FederationRuntime.loadRemote(...);

Thank you,

Patrick

Reproduction

https://github.com/patricklafrance/mf-enhanced-import-from-runtime-issue

Used Package Manager

pnpm

System Info

System:
    OS: Windows 11 10.0.22631
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 14.54 GB / 31.70 GB
  Binaries:
    Node: 21.7.1 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.19 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
    npm: 10.5.0 - C:\Program Files\nodejs\npm.CMD
    pnpm: 8.15.4 - ~\AppData\Roaming\npm\pnpm.CMD
  Browsers:
    Chrome: 124.0.6367.61
    Edge: Chromium (123.0.2420.97)
    Internet Explorer: 11.0.22621.1

Validations

Metadata

Metadata

Assignees

No one assigned

    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