EmilTholin / svelte-routing

A declarative Svelte routing library with SSR support
MIT License
2.02k stars 178 forks source link

No ES module support #237

Open ajmeese7 opened 1 year ago

ajmeese7 commented 1 year ago
svelte-routing doesn't appear to be written in CJS, but also doesn't appear to be a valid ES module (i.e. it doesn't have "type": "module" or an .mjs extension for the entry point). Please contact the package author to fix.

Any chance you're willing to push an update for this?

EmilTholin commented 1 year ago

Hi @ajmeese7!

This sounds like a reasonable addition. To my understanding adding "type": "module" to package.json would be a breaking change, so the alternative non-breaking way forward is to rename the entry file index.js to index.mjs.

{
  "name": "svelte-routing",
  "version": "1.6.0",
  "description": "A declarative Svelte routing library with SSR support",
- "main": "src/index.js",
+ "main": "src/index.mjs",

Does that sound right to you?

ajmeese7 commented 1 year ago

Hey @EmilTholin! That sounds like it should work in theory, I can try it locally by modifying my node_modules package and see if the message goes away if you'd like.

atsepkov commented 1 year ago

I'm running into this too, the .mjs approach didn't work for me, giving me the following error:

[webpack-watch]     Module build failed: Error: ENOENT: no such file or directory, open '/Users/alex/work/personal/svelte-ssr-webpack-example/node_modules/svelte-routing/src/index.js'
[webpack-watch]      @ ./components/pages/Home.svelte 25:0-47 99:14-20
[webpack-watch]      @ ./client-entry-scripts/Home.js
[2] [nodemon] restarting due to changes...
[2] [nodemon] starting `node dist/bundle.js`
[2] node:internal/modules/cjs/loader:1094
[2]     throw new ERR_REQUIRE_ESM(filename, true);
[2]     ^
[2]
[2] Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/alex/work/personal/svelte-ssr-webpack-example/node_modules/svelte-routing/src/index.mjs not supported.
[2] Instead change the require of /Users/alex/work/personal/svelte-ssr-webpack-example/node_modules/svelte-routing/src/index.mjs to a dynamic import() which is available in all CommonJS modules.

Which I don't understand, since there is no sign of original index.js file in the repo anymore since it's been renamed, nor do I see any require statements in the code that should be changed. The import in my own module uses ESM syntax as well: import { Router, Route } from 'svelte-routing'

krishnaTORQUE commented 4 months ago

This has been fixed in the latest version. (MAYBE) Please confirm so that I can close this issue.

timotheus90 commented 2 months ago

I'm having issue importing the project from node modules, while running jest tests with --experimental-vm-modules:

    import { jwtVerify } from 'jose'
    // only works like this (relative import):
    import { navigate } from '@/../node_modules/svelte-routing'

Whats the difference between jose and svelte-routing:

turns out jose specifies those exports

  "exports": {
    ".": {
      "import": "./dist/node/esm/index.js",
      "require": "./dist/node/cjs/index.js"
    },

Maybe you can add those in your package.json as well:

  "exports": {
    ".": {
      "import": "./src/index.js",
      "require": "./src/index.js",
      "types": "./types/index.d.ts",
      "svelte": "./src/index.js"
    }
  },

(this fixes the issue with jest esm imports)

webJose commented 2 months ago

Maybe this helps: https://publint.dev/svelte-routing@2.13.0

Also: https://arethetypeswrong.github.io/?p=svelte-routing%402.13.0

This last one looks weird: It has a check next to "node 16 (from ESM)" but next to the check has (CJS). May be an indication of something not quite right.