Skip to content

Import is reassigned in esm.sh code, but not in source code #1218

@alexgleason

Description

@alexgleason

Seeing this error in my esbuild:

✘ [ERROR] Cannot assign to import "__Process$"

    esm:https://esm.sh/*@lightninglabs/[email protected]/esnext/lnc-web.mjs?lp=node_modules%252F%2540lightninglabs%252Flnc-web:4:672:
      4 │ ...){o(_())}}}if(__Process$||(__Process$={getuid:()=>-1,getgid:()=>...
        ╵                               ~~~~~~~~~~

  Imports are immutable in JavaScript. To modify the value of this import, you must export a setter function in the imported file (e.g. "set__Process$") and then import and call that function here instead.

The package has code that looks like this:

	if (!globalThis.process) {
		globalThis.process = // ...custom shim
	}

Ref: https://github.com/lightninglabs/lnc-web/blob/ef487d8302ee33e6b2f96d5a1606142e6147ea34/lib/wasm_exec.js#L62

It's treating process as a global, and assigning it if it doesn't exist, which is valid JavaScript code.

But esm.sh treats it as an import of node:process, and rewrites the code similarly to this:

import process from "node:process";
if (!process) {
  process = // custom shim
}

This causes the esbuild error: Imports are immutable in JavaScript, and it refuses to bundle the code.

EDIT: This happens because of Go's wasm implementation and affects any package that deals with wasm files compiled with Go. https://go.dev/wiki/WebAssembly

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions