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

Window is not defined #163

Open
309631 opened this issue Jul 2, 2022 · 6 comments
Open

Window is not defined #163

309631 opened this issue Jul 2, 2022 · 6 comments

Comments

@309631
Copy link

309631 commented Jul 2, 2022

While trying to set up chart from the README.md, I have received an error:
ReferenceError: window is not defined
I've tried all of ideas here: https://kit.svelte.dev/faq#integrations
I had same problem with d3, but I resolved it with adding onMount and typeof window, here is not working

`<script>
import { onMount } from 'svelte';
import { chart } from "svelte-apexcharts";

onMount(() => {
if (typeof window !== "undefined") {
let options = {
chart: {
type: "bar",
},
series: [
{
name: "sales",
data: [30, 40, 35, 50, 49, 60, 70, 91, 125],
},
],
xaxis: {
categories: [1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999],
},
};
}
});
</script>`

`

`import adapter from '@sveltejs/adapter-static';
const dev = process.env.NODE_ENV === 'development';

It may be the problem with my config.svelte.js, so I am pasting it here as well. I am pretty new to Svelte and webdesign, so I may made an mistake there
`
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit:
{
adapter: adapter({
// default options are shown
pages: 'build',
assets: 'build',
fallback: null,
precompress: false
}),
trailingSlash: 'always',
paths: {
base: dev ? '' : '/app',
},
prerender: {
// This can be false if you're using a fallback (i.e. SPA mode)
default: true
},
vite: {
resolve: {
dedupe: ['@fullcalendar/common'],
browser: true,
},
optimizeDeps: {
include:['@fullcalendar/common']
}
}
},
paths: {
base: 'app/'
},
ssr:false,
};

export default config;`

Thank you in advance

@309631
Copy link
Author

309631 commented Jul 7, 2022

I resolved the problem by adding vite-plugin-iso-import plugin like in last answer:
https://stackoverflow.com/questions/69874742/sveltekit-console-error-window-is-not-defined-when-i-import-library

@Basir-PD
Copy link

I had same error in html2pdf and svelte-apexcharts and here is how to solve it in 3 simple steps:

  • Install vite plugin: yarn add -D vite-plugin-iso-import
  • Add plugin in svelte.config.js as
    import { isoImport } from 'vite-plugin-iso-import';
    
     kit: {
              vite: {
                plugins: [
                    isoImport(),
                ],
    
  • then add ?client in import as below
    import { chart } from 'svelte-apexcharts?client';
    

@Eudritch
Copy link

Eudritch commented Aug 4, 2022

The adapter static may not be installed in your dev dependency, so make sure to run npm install —save-dev '@sveltejs/adapter-static' then re-run your sveltekit app to see if it helped

@Creative-Difficulty
Copy link

Creative-Difficulty commented Sep 30, 2022

The adapter static may not be installed in your dev dependency, so make sure to run npm install —save-dev '@sveltejs/adapter-static' then re-run your sveltekit app to see if it helped

Thats not how it works. This is the correct command (for nodejs 18): npm i -D @sveltejs/adapter-static

@strenkml
Copy link

I had same error in html2pdf and svelte-apexcharts and here is how to solve it in 3 simple steps:

* Install vite plugin: `yarn add -D vite-plugin-iso-import`

* Add plugin in svelte.config.js as
  ```
  import { isoImport } from 'vite-plugin-iso-import';
  
   kit: {
            vite: {
              plugins: [
                  isoImport(),
              ],
  ```

* then add `?client` in import as below
  ```
  import { chart } from 'svelte-apexcharts?client';
  ```

I am getting the following error:

error when starting dev server:
Could not load svelte.config.js: Unexpected option config.kit.vite

This is my svelte.config.js:

import adapter from '@sveltejs/adapter-node';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
import { isoImport } from 'vite-plugin-iso-import';

/** @type {import('@sveltejs/kit').Config} */
const config = {
  preprocess: vitePreprocess(),

  kit: {
    adapter: adapter(),
    vite: {
      plugins: [isoImport()]
    }
  }
};

export default config;

@AndreaDev237
Copy link

AndreaDev237 commented Jan 7, 2024

@strenkml
To solve the error do these steps:

-- Remove :

import { isoImport } from 'vite-plugin-iso-import';  

and

vite: { 
plugins: [isoImport()]
}

from svelte.config.js

-- Go to vite.config.ts and add
import { isoImport } from 'vite-plugin-iso-import';
and
isoImport()
to plugins

example of my vite.config.ts:

import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vitest/config';
import { isoImport } from 'vite-plugin-iso-import';

export default defineConfig({
	plugins: [sveltekit(), isoImport()],
	test: {
		include: ['src/**/*.{test,spec}.{js,ts}']
	}
});

after that in the page that use the apex chart include the following line:

import { chart } from 'svelte-apexcharts?client';

However this solution make you lose the intellisense of chart library, so using directly apexchart js library with OnMount() is a better solution
ERRATA CORRIGE:
The iso plugin has a custom typescript plugin to solve the intellisense problem,
Just add this to the tsconfig.json
{ "compilerOptions": { "plugins": [{ "name": "vite-plugin-iso-import" }] } }
Details about the fix HERE

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

No branches or pull requests

6 participants