I'm currently diving into a project that involves Svelte-Kit (my first venture into svelte), Vite, TypeScript, and Pixi. Whenever I attempt to execute vite build
, the dreaded error
Cannot use import statement outside a module
rears its ugly head. Despite having "type": "module"
specified in my package.json and feeling confident about the rest of my setup, importing from both pixi.js/dist/esm/pixi.js
and pixi.js/dist/cjs/pixi.js
results in more error messages. I seem to be missing a crucial step here, so any guidance would be warmly welcomed!
It's worth noting that I've opted for pnpm
for managing my packages.
(node:42972) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
{path}/node_modules/.pnpm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="20504958490e4a5360160e140e12">[email protected]</a>/node_modules/pixi.js/dist/esm/pixi.js:8
import '@pixi/polyfill';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1033:15)
at Module._compile (node:internal/modules/cjs/loader:1069:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:170:29)
at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
[SyntaxError: Cannot use import statement outside a module]
ELIFECYCLE Command failed with exit code 1.
My suspicion is that there might be something amiss in my vite.config.js file:
// vite.config.js
import { sveltekit } from '@sveltejs/kit/vite';
import adapter from '@sveltejs/adapter-netlify';
/** @type {import('vite').UserConfig} */
const config = {
plugins: [sveltekit()],
kit: {
adapter: adapter({ split: true })
},
optimizeDeps: {
exclude: ['@apollo/client', 'svelte-apollo'],
include: ['pixi.js']
},
ssr: {
noExternal: ['@apollo/client', 'svelte-apollo']
}
};
export default config;
In this project, I have a component named Canvas.svelte
that is imported into the index.svelte
file. Inside the Canvas.svelte
, I bring in Pixi like this:
// Canvas.svelte
<script lang="ts" type="module">
...other imports
import * as PIXI from 'pixi.js';
...rest of file
</script>
package.json
...rest of file,
"devDependencies": {
"@apollo/client": "^3.6.8",
"@contentful/rich-text-types": "^15.12.1",
"@sveltejs/adapter-netlify": "1.0.0-next.70",
"@sveltejs/kit": "next",
"@types/cookie": "^0.5.1",
"@types/gsap": "^3.0.0",
"@types/tinycolor2": "^1.4.3",
"@typescript-eslint/eslint-plugin": "^5.27.0",
"@typescript-eslint/parser": "^5.27.0",
"apollo": "^2.34.0",
"cookie": "^0.4.1",
"graphql": "14.0.0",
"prettier": "^2.6.2",
"prettier-plugin-svelte": "^2.7.0",
"sass": "^1.53.0",
"svelte": "^3.49.0",
"svelte-apollo": "^0.5.0",
"svelte-check": "^2.8.0",
"svelte-preprocess": "^4.10.7",
"tslib": "^2.4.0",
"typescript": "^4.7.4",
"vite": "^3.0.2"
},
"type": "module",
"dependencies": {
"@fontsource/poppins": "^4.5.8",
"@pixi/filter-kawase-blur": "^4.1.5",
"@pixi/polyfill": "^6.4.2",
"gsap": "^3.10.4",
"pixi.js": "^6.4.2",
"svelte-pixi": "^0.1.3",
"svelte2tsx": "^0.5.11",
"tinycolor2": "^1.4.2"
}