kit: SvelteKit v1.0.0-next.146 SyntaxError: Cannot use import statement outside a module
Describe the bug
After upgrade to SvelteKit v1.0.0-next.146 (from 144) Getting the error in both dev & prod
Reproduction
cd /tmp
npm init svelte@next ttt
all defaults
cd ttt
rm -rf src/routes/*
npm install
npm install sveltestrap @sveltejs/adapter-node@next
src/routes/index.svelte
:
<script>
import Button from 'sveltestrap/src/Button.svelte';
import Popover from 'sveltestrap/src/Popover.svelte';
const placements = ['top', 'right', 'left', 'bottom'];
</script>
{#each placements as placement}
<div class="mt-3">
<Button id={`btn-${placement}`}>Click for {placement}</Button>
<Popover
target={`btn-${placement}`}
{placement}
title={`Popover ${placement}`}
>
This is a Popover on the {placement} of the trigger.
</Popover>
</div>
{/each}
svelte.config.js
:
import node from '@sveltejs/adapter-node';
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
adapter: node({
out: 'build',
}),
// hydrate the <div id="svelte"> element in src/app.html
target: '#svelte'
}
};
export default config;
Logs
6:22:04 PM [vite] Error when evaluating SSR module /node_modules/sveltestrap/src/Popover.svelte:
/tmp/ttt/node_modules/@popperjs/core/dist/esm/popper.js:1
import { popperGenerator, detectOverflow } from "./createPopper.js";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:988:16)
at Module._compile (internal/modules/cjs/loader.js:1036:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Module.require (internal/modules/cjs/loader.js:961:19)
at require (internal/modules/cjs/helpers.js:92:18)
at nodeRequire (/tmp/ttt/node_modules/vite/dist/node/chunks/dep-c1a9de64.js:73479:17)
at ssrImport (/tmp/ttt/node_modules/vite/dist/node/chunks/dep-c1a9de64.js:73431:20)
at eval (/node_modules/sveltestrap/src/Popover.svelte:9:31)
in prod
> Using @sveltejs/adapter-node
> Cannot find module '/tmp/ttt/node_modules/@popperjs/core/dist/esm/popper' imported from /tmp/ttt/.svelte-kit/output/server/app.js
Did you mean to import @popperjs/core/dist/esm/popper.js?
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/tmp/ttt/node_modules/@popperjs/core/dist/esm/popper' imported from /tmp/ttt/.svelte-kit/output/server/app.js
Did you mean to import @popperjs/core/dist/esm/popper.js?
at finalizeResolution (internal/modules/esm/resolve.js:285:11)
at moduleResolve (internal/modules/esm/resolve.js:708:10)
at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:819:11)
at Loader.resolve (internal/modules/esm/loader.js:89:40)
at Loader.getModuleJob (internal/modules/esm/loader.js:242:28)
at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:73:40)
at link (internal/modules/esm/module_job.js:72:36)
System Info
System:
OS: Linux 5.11 Ubuntu 21.04 (Hirsute Hippo)
CPU: (6) x64 Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
Memory: 19.67 GB / 31.23 GB
Container: Yes
Shell: 5.1.4 - /bin/bash
Binaries:
Node: 14.17.4 - /usr/bin/node
npm: 6.14.14 - /usr/bin/npm
Browsers:
Chrome: 92.0.4515.131
Firefox: 90.0.2
npmPackages:
@sveltejs/adapter-node: ^1.0.0-next.39 => 1.0.0-next.39
@sveltejs/kit: next => 1.0.0-next.146
svelte: ^3.34.0 => 3.42.1
Severity
blocking an upgrade
Additional Information
It works fine in SvelteKit v1.0.0-next.144
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 2
- Comments: 17 (5 by maintainers)
on vite.config.js, inside the config variable, add something like this:
at least this works for me
Hey, configuring the option within svelte.config.js is no longer supported as all vite configurations need to go in vite.config.js (when I added
const config = { kit: { adapter: adapter(), vite: { ssr: { noExternal: ['@popperjs/core'] } } }, };
It says that config.kit.vite has been removed and to use vite.config.js instead. However when I add the sir configuration to vite.config.js the same issue appears with popper.js. Is there an updated way to resolve this issue?If someone’s still facing this issue after upgrading svelteKit version to
1.0.0-next.348
and if the error says something aboutpopperjs
, then I recommend adding thessr: { noExternal: ['@popperjs/core'] }
option insidesvelte.config.js
as mentioned here. Adding this fixed my issue and made the popperjs error go away. Thanks @Rich-Harris for the workaround!AFAIK, the order doesn’t matter, it would act like a JSON obj. But my vite.config.ts on my other sveltekit project looks something like this:
My vite.config.ts looks like this:
Where do I insert your example, @ahmadshiddiqn? Do I insert
ssr: { noExternal: ['@popperjs/core'] }
afterplugins: [sveltekit()]
?EDIT: Yep, that seemed to work. Thanks for the tip!
@ethanmichel0 Same issue, but I found this https://github.com/bestguy/sveltestrap/issues/463#issuecomment-1207351497 works.
Add
"type": "module"
tonode_modules/@popperjs/core/package.json
I have the same issue when I start a new demo project. The error:
Cannot use import statement outside a module
in vscode.And here is the repo that has the problem: https://github.com/tcoopman/sveltekit-repro-bug-vscode
To be clear,
npm run dev
doesn’t give any issues, but the problems show in vscode withSvelte for VS Code
.