xstate icon indicating copy to clipboard operation
xstate copied to clipboard

Bug: xstate/actors in v5 alpha can't be imported from .mjs

Open tivac opened this issue 3 years ago • 3 comments

Description

Trying to import xstate/actors from an ESM context in node leads to an error. Using require("xstate/actors") works fine but is obviously weird in ESM mode.

Here's an example file t

Expected result

I expect to be able to load all the xstate entrypoints no matter if I'm using require() or import.

Actual result

Here's the .mjs file I created to test (also available as a gist)

import * as actors from "xstate/actors";

console.log(actors);

and the resulting error from node

$ node xstate-actors.mjs
node:internal/process/esm_loader:94
    internalBinding('errors').triggerUncaughtException(
                              ^

Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import 'C:\Users\pcavi\Documents\GitHub\xstate-component-tree\node_modules\xstate\actors' is not supported resolving ES modules imported from C:\Users\pcavi\Documents\GitHub\xstate-component-tree\xstate-actors.mjs
Did you mean to import xstate-component-tree/node_modules/xstate/actors/dist/xstate-actors.cjs.js?
    at new NodeError (node:internal/errors:372:5)
    at finalizeResolution (node:internal/modules/esm/resolve:433:17)
    at moduleResolve (node:internal/modules/esm/resolve:1009:10)
    at defaultResolve (node:internal/modules/esm/resolve:1218:11)
    at ESMLoader.resolve (node:internal/modules/esm/loader:580:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:294:18)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:80:40)
    at link (node:internal/modules/esm/module_job:78:36) {
  code: 'ERR_UNSUPPORTED_DIR_IMPORT',
  url: 'file:///C:/Users/pcavi/Documents/GitHub/xstate-component-tree/node_modules/xstate/actors'
}

Reproduction

https://gist.github.com/tivac/b6f58329ba3a90db30dd9e30873a221a

Additional context

npx envinfo --system --npmPackages xstate --binaries --markdown

System:

  • OS: Windows 10 10.0.22000
  • CPU: (24) x64 AMD Ryzen 9 5900X 12-Core Processor
  • Memory: 20.48 GB / 31.92 GB

Binaries:

  • Node: 16.15.0 - C:\Program Files\nodejs\node.EXE
  • npm: 8.9.0 - ~\AppData\Roaming\npm\npm.CMD

npmPackages:

  • xstate: ^5.0.0-alpha.0 => 5.0.0-alpha.0

tivac avatar May 28 '22 07:05 tivac

ESM support will come (hopefully) but later on. We need to carefully evaluate how to best approach this topic.

Andarist avatar May 28 '22 09:05 Andarist

Oh, ok. I saw module definitions and esm versions of the sub packages that made me think this was expected to work.

The main package works fine as an import, fwiw.

tivac avatar May 28 '22 15:05 tivac

Ye, the problem is that, unfortunately, node has decided to use different loader semantics than what a lot of people were already used to in the wild. This often makes "traditional" setups not compatible with node's requirements.

That being said - this should get mostly fixed once this lands in Preconstruct: https://github.com/preconstruct/preconstruct/pull/435

Andarist avatar Jun 01 '22 09:06 Andarist

@Andarist Looks like it landed; let's update preconstruct in the next branch 🔀

davidkpiano avatar Dec 27 '22 05:12 davidkpiano

closed by https://github.com/statelyai/xstate/pull/3727

Andarist avatar Dec 28 '22 11:12 Andarist