svelte-websocket-store icon indicating copy to clipboard operation
svelte-websocket-store copied to clipboard

Error on import?

Open arcfide opened this issue 5 years ago • 7 comments
trafficstars

I get the following error when trying to import this module.

internal/modules/cjs/loader.js:984
    throw new ERR_REQUIRE_ESM(filename);
    ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Users\...\node_modules\svelte-websocket-store\src\index.mjs
    at Module.load (internal/modules/cjs/loader.js:984:11)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Module.require (internal/modules/cjs/loader.js:1026:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (C:\Users\...\__sapper__\dev\server\server.js:10:38)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) {
  code: 'ERR_REQUIRE_ESM'
}

I'm not sure what's going on here? I'm using this line in my Sapper route code:

import websocketStore from 'svelte-websocket-store'
```

arcfide avatar Jun 18 '20 22:06 arcfide

Are You trying this without bundling (rollup, webpack,...) ? Which version of node You are using ?

To import the module natively with node, a recent 12.x or any 14.x node version is required

arlac77 avatar Jun 28 '20 17:06 arlac77

I'm trying to import this into a Svelte/Sapper application derived from the rollup template that Sapper provides.

I'm calling yarn run dev to run the application for development. I'm using node 12.x.

By copying the actual index.mjs file into my repository and then renaming it to use a .js extension instead of a .mjs extension, the problem goes away and I can use the code, but I had hoped to be able to use it directly after doing a yarn add.

arcfide avatar Jun 28 '20 22:06 arcfide

My specific node version is 12.17.0

arcfide avatar Jun 28 '20 22:06 arcfide

I can reproduce the problem with sapper. Looks like sapper has problems with nodes flavor of esm modules

arlac77 avatar Jun 29 '20 07:06 arlac77

Same here.

I can reproduce the problem with sapper. Looks like sapper has problems with nodes flavor of esm modules

ashwinvasudevan avatar Sep 22 '20 07:09 ashwinvasudevan

The problem is that svelte-websocket-store only works for with ESM not CJS type of modules. If you force sapper to product ESM modules on the server site, it should work.

arlac77 avatar Jan 07 '21 13:01 arlac77

You can try to import the full path:

import websocketStore from "svelte-websocket-store/src/index.mjs";

lucaspontoexe avatar Jun 22 '21 12:06 lucaspontoexe