Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SvelteKit / Vite support (works out of the box now, site and examples will be updated soon) #41

Closed
techniq opened this issue Mar 17, 2021 · 46 comments
Labels
advisory Notice of something

Comments

@techniq
Copy link
Contributor

techniq commented Mar 17, 2021

I recently moved to the latest SvelteKit based on Vite (1.0.0-next.50) and Layer Cake throws an error when imported:

500
Unexpected token 'export'

/Users/techniq/Documents/Development/playground/svelte-kit-layercake/node_modules/layercake/src/index.js:1
export { default as LayerCake } from './LayerCake.svelte';
^^^^^^

SyntaxError: Unexpected token 'export'
    at wrapSafe (internal/modules/cjs/loader.js:979:16)
    at Module._compile (internal/modules/cjs/loader.js:1027:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at nodeRequire (/Users/techniq/Documents/Development/playground/svelte-kit-layercake/node_modules/vite/dist/node/chunks/dep-efe32886.js:68907:17)
    at ssrImport (/Users/techniq/Documents/Development/playground/svelte-kit-layercake/node_modules/vite/dist/node/chunks/dep-efe32886.js:68865:20)
    at eval (/src/lib/Chart.svelte:7:31)

It looks like it's being detected as commonjs instead of esm. I'm not 100% sure if the issue is with Layer Cake, Vite (as it worked with older SvelteKit based on Snowpack), SvelteKit recently adding type: module to package.json (I was on an older SvelteKit/Snowpack before this change) or something else.

I see Layer Cake has main and module defined in package.json, but not an export map which from my limited understanding is the best tell for ESM for Vite/Snowpack/etc.

I've created a repo to demonstrate the issue.

I just ran npm init svelte@next, added layercake and a simple chart example. When I initially ran this example last night the issue didn't present itself, but when I tried to identify what else was different between this example and my project just now, it show'd up. I've cleared and re-installed node_modules as well as the .svelte directly (trying to clear any cache) but the issue persists. I'm using node v14.14.0.

@mhkeller
Copy link
Owner

I haven't used Svelte Kit or Vite yet – I was waiting for it to stabilize a bit. It's stumbling over the index.js file, though, which makes me think that the library needs to be compiled, similar to how the SSR build process works.

The index.js file is just a convenience reference file so maybe what you could do as a workaround is require the files directly like import Svg from 'layercake/src/layouts/Svg.svelte'. I haven't worked with this build system though so that may also be completely wrong!

@techniq
Copy link
Contributor Author

techniq commented Mar 18, 2021

Thanks @mhkeller. Sadly, I tried the direct import as well but ran into other issues. If I change LayerCake to:

import LayerCake from "layercake/src/LayerCake.svelte";

I would then get the following error:

7:48:49 AM [vite] Error when evaluating SSR module /node_modules/d3-format/src/formatSpecifier.js:
ReferenceError: formatSpecifier is not defined
    at /node_modules/d3-format/src/formatSpecifier.js:21:1
    at instantiateModule (/Users/techniq/Documents/Development/playground/svelte-kit-layercake/node_modules/vite/dist/node/chunks/dep-efe32886.js:68893:166)
7:48:49 AM [vite] Error when evaluating SSR module /node_modules/d3-format/src/formatSpecifier.js:
ReferenceError: formatSpecifier is not defined
    at /node_modules/d3-format/src/formatSpecifier.js:21:1
    at instantiateModule (/Users/techniq/Documents/Development/playground/svelte-kit-layercake/node_modules/vite/dist/node/chunks/dep-efe32886.js:68893:166) (x2)
7:48:50 AM [vite] Error when evaluating SSR module /node_modules/d3-time-format/src/defaultLocale.js:
ReferenceError: defaultLocale is not defined
    at /node_modules/d3-time-format/src/defaultLocale.js:9:1
    at instantiateModule (/Users/techniq/Documents/Development/playground/svelte-kit-layercake/node_modules/vite/dist/node/chunks/dep-efe32886.js:68893:166)
7:48:50 AM [vite] Error when evaluating SSR module /node_modules/d3-color/src/color.js:
ReferenceError: color is not defined
    at /node_modules/d3-color/src/color.js:170:15
    at instantiateModule (/Users/techniq/Documents/Development/playground/svelte-kit-layercake/node_modules/vite/dist/node/chunks/dep-efe32886.js:68893:166)

I also tried adding an exports by directly modifying LayerCake's package.json in node_modules.

image

If I left main and module, I will get the SyntaxError: Unexpected token 'export' and if I removed both of them, I would get an error that it couldn't find the module (maybe I was declaring it wrong).

7:47:28 AM [vite] Error when evaluating SSR module /Users/techniq/Documents/Development/playground/svelte-kit-layercake/src/routes/index.svelte:
Error: Cannot find module 'layercake' from '/Users/techniq/Documents/Development/playground/svelte-kit-layercake/src/routes'
    at Function.resolveSync [as sync] (/Users/techniq/Documents/Development/playground/svelte-kit-layercake/node_modules/resolve/lib/sync.js:102:15)
    at resolveFrom$2 (/Users/techniq/Documents/Development/playground/svelte-kit-layercake/node_modules/vite/dist/node/chunks/dep-efe32886.js:3883:29)
    at resolve (/Users/techniq/Documents/Development/playground/svelte-kit-layercake/node_modules/vite/dist/node/chunks/dep-efe32886.js:68928:22)
    at nodeRequire (/Users/techniq/Documents/Development/playground/svelte-kit-layercake/node_modules/vite/dist/node/chunks/dep-efe32886.js:68907:25)
    at ssrImport (/Users/techniq/Documents/Development/playground/svelte-kit-layercake/node_modules/vite/dist/node/chunks/dep-efe32886.js:68865:20)
    at eval (/Users/techniq/Documents/Development/playground/svelte-kit-layercake/src/routes/index.svelte:7:31)
    at instantiateModule (/Users/techniq/Documents/Development/playground/svelte-kit-layercake/node_modules/vite/dist/node/chunks/dep-efe32886.js:68893:166)

Lastly, I decided to pull in LayerCake's source directly into my lib directly, and initially it gave me the same error as direct importing...

8:07:37 AM [vite] new dependencies found: d3-scale, updating...
8:07:37 AM [vite] Error when evaluating SSR module /node_modules/d3-format/src/formatSpecifier.js:
ReferenceError: formatSpecifier is not defined
    at /node_modules/d3-format/src/formatSpecifier.js:21:1
    at instantiateModule (/Users/techniq/Documents/Development/playground/svelte-kit-layercake/node_modules/vite/dist/node/chunks/dep-efe32886.js:68893:166)
8:07:37 AM [vite] Error when evaluating SSR module /node_modules/d3-format/src/formatSpecifier.js:
ReferenceError: formatSpecifier is not defined
    at /node_modules/d3-format/src/formatSpecifier.js:21:1
    at instantiateModule (/Users/techniq/Documents/Development/playground/svelte-kit-layercake/node_modules/vite/dist/node/chunks/dep-efe32886.js:68893:166) (x2)
8:07:38 AM [vite] Error when evaluating SSR module /node_modules/d3-time-format/src/defaultLocale.js:
ReferenceError: defaultLocale is not defined
    at /node_modules/d3-time-format/src/defaultLocale.js:9:1
    at instantiateModule (/Users/techniq/Documents/Development/playground/svelte-kit-layercake/node_modules/vite/dist/node/chunks/dep-efe32886.js:68893:166)
8:07:38 AM [vite] ✨ dependencies updated, reloading page...
8:07:38 AM [vite] Error when evaluating SSR module /node_modules/d3-color/src/color.js:
ReferenceError: color is not defined
    at /node_modules/d3-color/src/color.js:170:15
    at instantiateModule (/Users/techniq/Documents/Development/playground/svelte-kit-layercake/node_modules/vite/dist/node/chunks/dep-efe32886.js:68893:166)

but after another browser refresh, it works:

image

my .vite cache then has just d3-scale

image

needless to say I'm still perplexed, and my workaround isn't optimal, but allows me to progress for now. Thanks for getting back to me so quickly.

@techniq
Copy link
Contributor Author

techniq commented Mar 18, 2021

Coincidently as soon as I submitted the last comment I did another search and came across this Vite issue posted 40 minutes ago that looks related...

I'll keep an eye on it :)

@mhkeller
Copy link
Owner

I'm glad you found a workaround. Strange that importing it directly doesn't fix it since I would think that is the same thing as copying it into the project. I should maybe add a Troubleshooting section to the site. Since LayerCake is just a collection of Svelte components, I think most build-type issues would be solved by copying it directly into the project.

I'll keep this issue open for a little bit in case there are updates. When SvelteKit is stable I was going to redo the layercake.graphics site using it and work out any issues then.

@techniq
Copy link
Contributor Author

techniq commented Mar 18, 2021

I still have some chart examples and more foundational components to share with you at some point :). I still have more to write/port from React (as well as the overall project which has been my evening project since last summer). I'm hoping to go live with the new Svelte-based project this summer and should have some components to share. Maybe we can hop on a video chat and discuss when I'm closer. I've considered releasing a companion library on top of LayerCake if not done directly in LayerCake. They are not full chart components, more building blocks that you currently have to copy/paste from the examples. Very similar to visx (which I contributed too).

@mhkeller
Copy link
Owner

Sounds good! Send me an email when you want to go through it.

@mhkeller mhkeller added the awaiting-something-else Needs some other thing to happen before it can be done label Mar 19, 2021
eraviart added a commit to leximpact/leximpact-socio-fiscal-ui that referenced this issue Mar 30, 2021
@zakkor
Copy link

zakkor commented Apr 3, 2021

It seems like it needs to be inside the src/lib directory specifically, otherwise it won't work.

@Wolfr
Copy link

Wolfr commented Apr 4, 2021

I ran into this as well trying to integrate LayerCake into a SvelteKit project.

Basically tried to integrate the front-page example.

Repo here: Wolfr/sveltekit-jui@097aff8

Keeping my eye on the this thread over at the vite repo .

@anilbhattaraitoronto
Copy link

[vite] Error when evaluating SSR module /node_modules/better-sqlite3/lib/index.js:
ReferenceError: module is not defined
at /node_modules/better-sqlite3/lib/index.js:2:1
at instantiateModule (/Users/username/projects/sveltekits/kitpostgres/node_modules/vite/dist/node/chunks/dep-66eb515d.js:69030:166)

@anilbhattaraitoronto
Copy link

Being a newbie programmer, I am trying to understand what is going on. Thank you all.

@anilbhattaraitoronto
Copy link

For now changing 'module.exports' to export default in 'svelte.config.cjs' solved the problem for me. Thank you once again.

@anilbhattaraitoronto
Copy link

Sorry, it did not work. My bad. Apologies.

@anilbhattaraitoronto
Copy link

What worked is this: https://github.com/techniq/svelte-kit-layercake/blob/master/vite.config.js
Separating the vite part from svelte.config.cjs

That looks a bit hacky but works for now.

@seans84
Copy link

seans84 commented Apr 13, 2021

Just to be ultra clear on this, what you need to do is update your svelte.config.js file to be:

const sveltePreprocess = require('svelte-preprocess');
const node = require('@sveltejs/adapter-node');
const pkg = require('./package.json');

/** @type {import('@sveltejs/kit').Config} */
module.exports = {
	// Consult https://github.com/sveltejs/svelte-preprocess
	// for more information about preprocessors
	preprocess: sveltePreprocess(),
	kit: {
		// By default, `npm run build` will create a standard Node app.
		// You can create optimized builds for different platforms by
		// specifying a different adapter
		adapter: node(),

		// hydrate the <div id="svelte"> element in src/app.html
		target: '#svelte'
	}
};

And then you need to add an additional vite.config.js file with the contents of this:

// Consult https://vitejs.dev/config/ to learn about these options
import { join } from 'path';
import { readFileSync } from 'fs';
import { cwd } from 'process';

const pkg = JSON.parse(readFileSync(join(cwd(), 'package.json')));

/** @type {import('vite').UserConfig} */
export default {
	ssr: {
		noExternal: Object.keys(pkg.dependencies || {})
	}
};

@Wolfr
Copy link

Wolfr commented Apr 13, 2021

Woohoo, a solution! This will help me to integrate Layercake as the default charting package in my project sveltekit-jui.

@quantuminformation
Copy link

@seans84 It says

Please remove vite.config.js and put Vite config in svelte.config.cjs: https://kit.svelte.dev/docs#configuration-vite

@Wolfr
Copy link

Wolfr commented Apr 13, 2021

I tested the solution @seans84 but don't really think you are supposed to do that, also based on @quantuminformation 's comment. You are supposed to put Vite specific config in the vite object within the "kit" object of svelte.config.cjs.

@enzojs
Copy link

enzojs commented Apr 15, 2021

Solved it temporarly by using createRequire vitejs/vite#2890 (comment)

@benjaminpreiss
Copy link

I had an error with d3 when I installed it as a dependency and not as a development dependency. After installing d3 with npm install --save-dev d3 I didn't get an error.

@sbutler-gh

This comment has been minimized.

@sbutler-gh
Copy link

Continued experimenting, I'm now getting this:

[vite] Error when evaluating SSR module /Users/sam/blog/src/routes/thoughts/countingcarbon.svelte:
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/sam/blog/node_modules/d3-format/src/index.js
require() of ES modules is not supported.
require() of /Users/sam/blog/node_modules/d3-format/src/index.js from /Users/sam/blog/node_modules/vite/dist/node/chunks/dep-66eb515d.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/sam/blog/node_modules/d3-format/package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1080:13)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at nodeRequire (/Users/sam/blog/node_modules/vite/dist/node/chunks/dep-66eb515d.js:69044:17)
    at ssrImport (/Users/sam/blog/node_modules/vite/dist/node/chunks/dep-66eb515d.js:69002:20)
    at eval (/Users/sam/blog/src/routes/thoughts/countingcarbon.svelte:19:31)
    at instantiateModule (/Users/sam/blog/node_modules/vite/dist/node/chunks/dep-66eb515d.js:69030:166)

I've tried all the solutions recommended in the error (changing index.js to .cjs, changing require to import, removing type: module from the d3-format package.json) ... and the error has persisted throughout.

Could this have to do with the fact that I initially configured sveltekit to use TypeScript? If it would help, I'd be glad to switch that initial configuration setting back to just javascript.

Anyway, any input or guidance would be greatly appreciated

@a-luna
Copy link

a-luna commented May 7, 2021

Since SvelteKit is really unstable at this point, the solutions provided earlier in this thread may no longer be working simply due to changes in the structure/behavior of the svelte.config.js/svelte.config.cjs/vite.config.js file(s).

Recently (3 days ago as I write this), svelte.config.cjs was changed to svelte.config.js, converting the config to ESM from CommonJS.

The @sveltejs/kit documentation has been updated to reflect this change, the following FAQ suggests adding a dependency to vite.optimizeDeps.include in svelte.config.js as a potential workaround (emphasis mine):

The second most commonly-encountered issue is having a Svelte component that imports a CommonJS library. In this case, you should try to work with the library authors to distribute an ESM version of the dependency. However, in the meantime, you can workaround this issue by adding the dependency to vite.optimizeDeps.include in svelte.config.js.

With the latest version (@sveltejs/kit 1.0.0-next.101), applying this change has allowed me to use layercake. Here is the entire svelte.config.js from my project that is currently working for me:

import preprocess from 'svelte-preprocess';
import node from '@sveltejs/adapter-node';

/** @type {import('@sveltejs/kit').Config} */
const config = {
  preprocess: [
    preprocess({
      defaults: {
        style: 'postcss'
      },
      postcss: true
    })
  ],

  kit: {
    adapter: node(),
    target: '#svelte',

    vite: {
      ssr: {
        noExternal: []
      },
      optimizeDeps: {
        include: ['layercake']
      }
    }
  }
};

export default config;

@aslak01
Copy link

aslak01 commented May 7, 2021

@a-luna I've just tried this, and I'm getting the same error that the thread started with. I get the sense from your post that you're using layercake normally (as in imported from node_modules), but maybe I'm missing something?

export { default as LayerCake } from './LayerCake.svelte';
^^^^^^
SyntaxError: Unexpected token 'export'

@cryptodeal
Copy link

cryptodeal commented May 8, 2021

@a-luna I've just tried this, and I'm getting the same error that the thread started with. I get the sense from your post that you're using layercake normally (as in imported from node_modules), but maybe I'm missing something?

export { default as LayerCake } from './LayerCake.svelte';
^^^^^^
SyntaxError: Unexpected token 'export'

I followed the steps in @a-luna post and had the same issue, but was able to resolve it by installing layer cake as a dependency rather than a devDependency.

@a-luna do you layercake installed as a devDependency or a dependency?

Edit: working with @aslak01 on discord, we were able to get layercake working with the latest version of SvelteKit for them as well; requirements seem to be: ensure layercake is installed as a dependency, any d3 dependencies need to be installed as devDependencies, and in svelte.config.js config.kit.vite.optimizeDeps.include needs to be an array containing 'layercake'

@rdreyer-godaddy
Copy link

@cryptodeal thanks for that comment. I got the first example in the layercake guide to work with the following in my devDependencies:

"d3-color": "^2.0.0",
"d3-format": "^2.0.0",
"d3-interpolate": "^2.0.1",
"d3-time-format": "^3.0.0",

@Wolfr
Copy link

Wolfr commented May 22, 2021

Based on the advice in this thread I made a template:
https://github.com/Wolfr/layercake-sveltekit-template

Normally if you clone this repo, npm install and run npm run dev -- --open (who came up with this command?!) then you should see a page linking to two example charts (bar and line+area).

This is based on the latest version of SvelteKit I believe (not sure, seems they are not versioning SvelteKit for now).

It works but the dev experience is terrible. It seems the HMR gets into some kind of infinite loop and you have to restart the compiler to see your changes.

@mhkeller
Copy link
Owner

Thanks @Wolfr and for everyone dropping their nuggets of wisdom into this thread. Hopefully it stabilizes soon! I'm keeping my eye on it 👀

@mhkeller mhkeller changed the title SvelteKit / Vite support SvelteKit / Vite support (awaiting SvelteKit 1.0) Jun 10, 2021
@davidcallanan
Copy link

davidcallanan commented Jul 7, 2021

I'm also getting this problem and the proposed solution is not working.

The following error occurs due to adding in node() adapter in the configuration file:

config.kit.adapter should be an object with an "adapt" method. See https://kit.svelte.dev/docs#adapters

@pierreminik
Copy link

I'm also getting this problem and the proposed solution is not working.

The following error occurs due to adding in node() adapter in the configuration file:

config.kit.adapter should be an object with an "adapt" method. See https://kit.svelte.dev/docs#adapters

I remember seeing this error in a project unrelated to this. I think updating sveltekit and its node-adapter fixed it.

Does npm run dev work?

@aslak01
Copy link

aslak01 commented Jul 7, 2021

I don't remember if node is the default adapter supplied by svelte kit? But I remember having that error after installing an adapter with npm, and the solution being going into package.json and changing whatever version number ended up there to "latest".

@davidcallanan
Copy link

@pierreminik

This didn't work for me. npm run dev is the command that is giving me this error, I haven't even used any other commands yet.

@aslak01

That didn't work for me.

@aslak01
Copy link

aslak01 commented Jul 8, 2021

@davidcallanan Sorry I remembered wrong, it's supposed to be next, not latest.

@davidcallanan
Copy link

davidcallanan commented Jul 8, 2021

@aslak01 Oh well that's the way it was initially when it didn't work I think. Anyway I've moved on to some other solution to what I was doing so I don't need to fix this problem anymore.

@mhkeller
Copy link
Owner

mhkeller commented Sep 23, 2021

I put this demo together and it seems to work out of the box now.

I have a couple of questions.

  1. @Wolfr you set the following in your template
ssr: {
	noExternal: []
},
optimizeDeps: {
	include: ['layercake']
}

After reading the vite docs, it seems like it's a good idea to bundle layercake with optimizeDeps. What's the purpose of setting a truthy value for ssr.noExternal? Would this change for projects that import other libraries? Including or excluding this value didn't seem to have any effect on my demo.

2. Should layercake and any other required libraries be installed as dependencies or devDependencies? I didn't see any difference in behavior doing it one way or the other.

[edit: question answered, it will go in dependencies]

@aslak01
Copy link

aslak01 commented Sep 23, 2021

Runs fine here too.

@techniq
Copy link
Contributor Author

techniq commented Sep 24, 2021

@mhkeller Regarding number 2 (dependencies vs devDependencies), before vitejs/vite#4699 it used to matter, but now it doesn't so I'm guessing it comes down to just preference now (I use dependencies myself).

@mhkeller
Copy link
Owner

@techniq cool. that's the answer i got elsewhere also

@mhkeller
Copy link
Owner

I'm going to be rewriting the layercake.graphics website (https://github.com/mhkeller/layercake.graphics) to use svelte kit and then port all the examples over but good to know that this issue is no longer a blocker. I'll keep it up in case people have any questions.

@mhkeller mhkeller added advisory Notice of something and removed awaiting-something-else Needs some other thing to happen before it can be done labels Sep 29, 2021
@mhkeller mhkeller changed the title SvelteKit / Vite support (awaiting SvelteKit 1.0) SvelteKit / Vite support (works out of the box now, site and examples will be updated soon) Sep 29, 2021
@akiarostami
Copy link

I'm having the same issue with revogrid.

@mhkeller
Copy link
Owner

@akiarostami what issue are you having? are you using layercake?

@akiarostami
Copy link

I'm sorry @mhkeller, I left this note on a wrong list! I'm having this issue with revogrid, not Layercake.

@techniq
Copy link
Contributor Author

techniq commented Dec 15, 2021

See #54 if you are experiencing issues with SvelteKit 1.0.0-next.202 / Vite 2.7

@mhkeller
Copy link
Owner

This is pretty much done. There's a kit branch on layercake-template, the library is now built with svelte-kit and the site is now in that library. If anyone has any thoughts on the default template, let me know. I didn't include things like prerendering since I don't have a strong opinion about it and it's easy enough to add a context module if someone cares.

@mhkeller
Copy link
Owner

Mostly waiting to hear some thoughts on this issue before publishing the new version and updating everything: #57

@mhkeller
Copy link
Owner

mhkeller commented Dec 20, 2021

One thing I looked into was bundling d3-scale into layercake using vite's optimizeDeps option. But it seemed to have no effect? I assumed it would do something like add a minified js file and then change my import { whatever } from 'd3-scale' to import { whatever } from './d3-scale.bundled.min.js but the d3-scale import remained. Maybe I don't really understand what that option is meant to do. Open to any ideas on how to resolve since it would be nice to not have that dependency.

edit: altho the advantage is that you'll possibly be getting a new version of d3-scale on install...

@mhkeller
Copy link
Owner

This is now merged and released as 6.0.0 https://github.com/mhkeller/layercake/releases/tag/v6.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
advisory Notice of something
Projects
None yet
Development

No branches or pull requests