Skip to content

Got `$state(...) can only be used as a variable declaration initializer or a class field` error in docker build, but not local PC #14185

@ninyawee

Description

@ninyawee

Describe the bug

This is a build time error. i found in similar to #12852 . This prevent me from serve it in production.

It is so weird that build in my local PC works

npm run dev #works

npm run build #works
 npm run preview #works

, but

running it in docker is not.

My workaround is to copy the build folder from local pc into the docker.

Reproduction

# https://khromov.se/dockerizing-your-sveltekit-applications-a-practical-guide/
# Use the official Node.js image as the base image

FROM node:22-alpine AS builder

ARG FONTAWESOME_NPM_AUTH_TOKEN
ARG COMMIT_HASH="unknown"
ENV COMMIT_HASH=${COMMIT_HASH}

WORKDIR /app
COPY package*.json .

COPY .npmrc .
RUN npm ci
# RUN npm install
COPY . .

RUN npm run build
RUN npm prune --production

FROM node:22-alpine
WORKDIR /app
COPY --from=builder /app/build build/
COPY --from=builder /app/node_modules node_modules/
COPY package.json .
EXPOSE 3000
ENV NODE_ENV=production
CMD [ "node", "build" ]

#  docker --debug build --no-cache --progress=plain  -f Dockerfile.suppose --build-arg FONTAWESOME_NPM_AUTH_TOKEN=$FONTAWESOME_NPM_AUTH_TOKEN .

Logs

#12 3.387 x Build failed in 2.10s
#12 3.387 error during build:
#12 3.387 [vite-plugin-svelte-module] [plugin vite-plugin-svelte-module] src/lib/components/map/map-state.svelte.ts (11:23): src/lib/components/map/map-state.svelte.ts:11:23 `$state(...)` can only be used as a variable declaration initializer or a class field
#12 3.387 file: /app/src/lib/components/map/map-state.svelte.ts:11:23
#12 3.387 
#12 3.387   9 |  export class MapState {
#12 3.387  10 |    constructor(mapNotes = void 0, mode = MAP_MODES.NONE) {
#12 3.387  11 |      this.currentView = $state({ lng: 0, lat: 0, zoom: 0 });
#12 3.387                                                                  ^
#12 3.387  12 |      this.baseMapStyle = $state(OurMapStyle.xxx);
#12 3.387  13 |      this.selectedRasterLayers = $state([]);
#12 3.387 
#12 3.387 
#12 ERROR: process "/bin/sh -c npm run build" did not complete successfully: exit code: 1
------
 > [builder 8/9] RUN npm run build:
3.387 file: /app/src/lib/components/map/map-state.svelte.ts:11:23
3.387 
3.387   9 |  export class MapState {
3.387  10 |    constructor(mapNotes = void 0, mode = MAP_MODES.NONE) {
3.387  11 |      this.currentView = $state({ lng: 0, lat: 0, zoom: 0 });
3.387                                                                  ^
3.387  12 |      this.baseMapStyle = $state(OurMapStyle.xxx);
3.387  13 |      this.selectedRasterLayers = $state([]);
3.387 
3.387 
------


### System Info

```shell
$ npx envinfo --system --npmPackages svelte,rollup,webpack --binaries --browsers
Need to install the following packages:
[email protected]
Ok to proceed? (y) y


  System:
    OS: Linux 6.8 Ubuntu 24.04.1 LTS 24.04.1 LTS (Noble Numbat)
    CPU: (48) x64 AMD Ryzen Threadripper 3960X 24-Core Processor
    Memory: 82.13 GB / 125.63 GB
    Container: Yes
    Shell: 5.2.21 - /bin/bash
  Binaries:
    Node: 20.17.0 - ~/.local/share/mise/installs/node/latest/bin/node
    npm: 10.8.2 - ~/.local/share/mise/installs/node/latest/bin/npm
    bun: 1.1.29 - ~/.local/share/mise/installs/bun/latest/bin/bun
  Browsers:
    Brave Browser: 130.1.71.121
    Chrome: 130.0.6723.91
  npmPackages:
    svelte: ^5.0.0-next.264 => 5.1.11

Severity

blocking all usage of svelte

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions