Search code examples
node.jsnode-modulessveltekitsveltekit-adapter-node

Sveltekit run build = Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension


I wrote a simple Sveltekit Application with the Skeleton UI. I created the build folder using npm run build.

If I locally run this via node using node index.js it works fine, and the site is accessible. When I create a fresh VPS running with Ubuntu 22.04, install node.js 22.10.0 and will test the application with node index.js, I am getting this error:

(node:33314) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension. /www/test/index.js:1 import { handler } from './handler.js'; ^^^^^^

SyntaxError: Cannot use import statement outside a module at wrapSafe (internal/modules/cjs/loader.js:915:16) at Module._compile (internal/modules/cjs/loader.js:963:27) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10) at Module.load (internal/modules/cjs/loader.js:863:32) at Function.Module._load (internal/modules/cjs/loader.js:708:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12) at internal/main/run_main_module.js:17:47

The package.json for the project looks like:

{
    "name": "test",
    "version": "0.0.1",
    "private": true,
    "type": "module",
    "scripts": {
        "dev": "vite dev",
        "build": "vite build",
        "preview": "vite preview",
        "lint": "prettier --check . && eslint .",
        "format": "prettier --write ."
    },
    "devDependencies": {
        "@skeletonlabs/skeleton": "2.6.0",
        "@skeletonlabs/tw-plugin": "0.3.0",
        "@sveltejs/adapter-auto": "^3.0.0",
        "@sveltejs/kit": "^2.0.0",
        "@sveltejs/vite-plugin-svelte": "^3.0.0",
        "@tailwindcss/forms": "0.5.7",
        "@tailwindcss/typography": "0.5.10",
        "autoprefixer": "10.4.16",
        "eslint": "^8.28.0",
        "eslint-config-prettier": "^9.1.0",
        "eslint-plugin-svelte": "^2.30.0",
        "postcss": "8.4.32",
        "prettier": "^3.1.1",
        "prettier-plugin-svelte": "^3.1.2",
        "svelte": "^4.2.7",
        "tailwindcss": "3.3.6",
        "vite": "^5.0.3",
        "vite-plugin-tailwind-purgecss": "0.2.0"
    },
    "dependencies": {
        "@floating-ui/dom": "^1.5.3",
        "@prisma/client": "^5.7.0",
        "@supabase/ssr": "^0.0.10",
        "@supabase/supabase-js": "^2.39.0",
        "@sveltejs/adapter-node": "^2.0.2",
        "prisma": "^5.7.0"
    }
}

and the svelte.config.js:

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

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

    kit: {
        adapter: adapter()
    }
};
export default config;

Would be great if someone could kick me in the right direction.. Some weeks ago it worked just fine and even the old project that worked on the other VPS, gets the same error on the new VPS, even everything is the same.

I also created a project with the Demo application, to be sure nothing in the application is wrong. Same error on the Server.


Solution

  • I've had the same problem, the solution can be found here

    In short, just create a package.json file in the build/deployment folder.

    {
        "type": "module"
    }
    

    Best,
    Zsolt