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

Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@sveltejs/kit' on production build #1257

Closed
sphinxc0re opened this issue Apr 28, 2021 · 13 comments · Fixed by #1648
Closed
Labels
bug Something isn't working pkg:adapter-node
Milestone

Comments

@sphinxc0re
Copy link
Contributor

Describe the bug
When I create a new SvelteKit project with the node adapter and try to use the files in the build directory, I get the following error:

Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@sveltejs/kit' imported from [...]

Logs

internal/process/esm_loader.js:74
    internalBinding('errors').triggerUncaughtException(
                              ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@sveltejs/kit' imported from [REDACTED]/app.js
    at packageResolve (internal/modules/esm/resolve.js:655:9)
    at moduleResolve (internal/modules/esm/resolve.js:696:18)
    at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:810:11)
    at Loader.resolve (internal/modules/esm/loader.js:86:40)
    at Loader.getModuleJob (internal/modules/esm/loader.js:230:28)
    at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:56:40)
    at link (internal/modules/esm/module_job.js:55:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}

To Reproduce

  • create a new SvelteKit skeleton project
  • add adapter-node to it
  • make sure all the deps have "next" as version
  • yarn && yarn build
  • isolate the build directory (I only want the production files in my production environment) by moving it somewhere else
  • add a package.json file to the directory with the contents: { "type": "module" } (for ESM)
  • node . <- This is what triggers the bug

Expected behavior

I expect the build directory to contain everything that is needed to run the app in isolation. Adding the small package.json file is another thing that I do not want to do myself.

When running node . the server should just run.

Information about your SvelteKit Installation:

Diagnostics
  System:
    OS: macOS 11.2.3
    CPU: (8) x64 Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
    Memory: 5.37 GB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.16.0 - ~/.volta/tools/image/node/14.16.0/bin/node
    Yarn: 1.22.10 - ~/.volta/tools/image/yarn/1.22.10/bin/yarn
    npm: 7.7.0 - ~/.volta/tools/image/npm/7.7.0/bin/npm
  Browsers:
    Chrome: 90.0.4430.85
    Firefox: 87.0
    Safari: 14.0.3
  npmPackages:
    @sveltejs/kit: ^1.0.0-next.74 => 1.0.0-next.91 
    svelte: ^3.29.0 => 3.37.0 
    vite: ^2.2.2 => 2.2.3 
  • Your adapter: Node

Severity
It's kind of annoying as I really want my production setup to be ready, but it doesn't stop me from development.

Additional context
All the above steps are done in a Dockerfile for production but I was able to reproduce it outside as well

Dockerfile
FROM node:14-alpine as build

ARG VITE_API_URL

RUN mkdir /svelte-build
WORKDIR /svelte-build

ENV VITE_API_URL=${VITE_API_URL}

COPY . .
RUN yarn && yarn build

FROM node:14-alpine as production

RUN addgroup -S app && adduser -S app -G app

RUN mkdir /app && chown app:app /app

USER app

WORKDIR /app

RUN echo '{ "type": "module" }' > /app/package.json
COPY --chown=app:app --from=build /svelte-build/build/ /app

EXPOSE 3000

CMD node index.js
@Conduitry Conduitry added the bug Something isn't working label May 25, 2021
@Conduitry
Copy link
Member

I ran into this the other day. (cc @Rich-Harris) Rich agreed that this was probably actually a bug. The server-side output that's sent to the Node adapter should probably be re-bundled within the adapter itself, like is happening for some other adapters. @sveltejs/kit shouldn't be an external prod dependency of the built, adapted Node app. When this re-bundling happens, probably everything in devDependencies (that is, @sveltejs/kit and whatever other dependencies you've added) should be included in the server-side bundle.

In the meantime as a workaround, you can move @sveltejs/kit and any other dependencies your app needs at runtime from devDependencies to dependencies but this obviously isn't a great solution.

@Conduitry Conduitry added this to the 1.0 milestone May 25, 2021
@Discorainman
Copy link

Same issue and above workaround did NOT work for me. Now it complains about missing svelte module. Does not matter if svelte is in devDependencies or dependencies.

@katkrasinsk
Copy link

Same issue with me. Trying deploying to heroku using adapter-node
complete log:

2021-05-26T13:05:39.000000+00:00 app[api]: Build succeeded
2021-05-26T13:05:41.632730+00:00 heroku[web.1]: Starting process with command `npm start`
2021-05-26T13:05:48.381759+00:00 app[web.1]:
2021-05-26T13:05:48.381783+00:00 app[web.1]: > [email protected] start /app
2021-05-26T13:05:48.381784+00:00 app[web.1]: > node build
2021-05-26T13:05:48.381784+00:00 app[web.1]:
2021-05-26T13:05:48.727284+00:00 app[web.1]: internal/process/esm_loader.js:74
2021-05-26T13:05:48.727483+00:00 app[web.1]: internalBinding('errors').triggerUncaughtException(
2021-05-26T13:05:48.727485+00:00 app[web.1]: ^
2021-05-26T13:05:48.727485+00:00 app[web.1]:
2021-05-26T13:05:48.727486+00:00 app[web.1]: Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@sveltejs/kit' imported from /app/build/app.js
2021-05-26T13:05:48.727487+00:00 app[web.1]: at packageResolve (internal/modules/esm/resolve.js:655:9)
2021-05-26T13:05:48.727487+00:00 app[web.1]: at moduleResolve (internal/modules/esm/resolve.js:696:18)
2021-05-26T13:05:48.727488+00:00 app[web.1]: at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:810:11)
2021-05-26T13:05:48.727488+00:00 app[web.1]: at Loader.resolve (internal/modules/esm/loader.js:88:40)
2021-05-26T13:05:48.727489+00:00 app[web.1]: at Loader.getModuleJob (internal/modules/esm/loader.js:241:28)
2021-05-26T13:05:48.727489+00:00 app[web.1]: at ModuleWrap. (internal/modules/esm/module_job.js:56:40)
2021-05-26T13:05:48.727490+00:00 app[web.1]: at link (internal/modules/esm/module_job.js:55:36) {
2021-05-26T13:05:48.727490+00:00 app[web.1]: code: 'ERR_MODULE_NOT_FOUND'
2021-05-26T13:05:48.727491+00:00 app[web.1]: }
2021-05-26T13:05:48.783848+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-05-26T13:05:48.783850+00:00 app[web.1]: npm ERR! errno 1
2021-05-26T13:05:48.799210+00:00 app[web.1]: npm ERR! [email protected] start: `node build`
2021-05-26T13:05:48.799211+00:00 app[web.1]: npm ERR! Exit status 1
2021-05-26T13:05:48.799212+00:00 app[web.1]: npm ERR!
2021-05-26T13:05:48.799213+00:00 app[web.1]: npm ERR! Failed at the [email protected] start script.
2021-05-26T13:05:48.799214+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-05-26T13:05:49.642211+00:00 app[web.1]:
2021-05-26T13:05:49.654457+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-05-26T13:05:49.654641+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2021-05-26T13_05_48_797Z-debug.log
2021-05-26T13:05:49.805437+00:00 heroku[web.1]: Process exited with status 1
2021-05-26T13:05:49.911449+00:00 heroku[web.1]: State changed from starting to crashed
2021-05-26T13:14:45.371410+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=mbaw.herokuapp.com request_id=eea42cf2-42a7-4ebf-a32d-65d92f4f638b fwd="187.8.172.13" dyno= connect= service= status=503 bytes= protocol=https
2021-05-26T13:14:45.904804+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=mbaw.herokuapp.com request_id=1319728b-26fb-49c9-bd22-dadf50c29172 fwd="187.8.172.13" dyno= connect= service= status=503 bytes= protocol=https

@utkarshkukreti
Copy link
Contributor

@katkrasinsk is @sveltejs/kit in your dependencies or devDependencies? I'm using the following successfully to deploy in production for several weeks now (same as the workaround as @Conduitry mentioned):

  "dependencies": {
    "@sveltejs/adapter-node": "next",
    "@sveltejs/kit": "next"
  }

@katkrasinsk
Copy link

@utkarshkukreti tried with the 'workaround' and still, it then complains on svelte missing:
Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'svelte' imported from /app/build/app.js, as @Discorainman reported. By the way, I tried also adapter-begin and it fails even earlier in building in my machine. But I am still investigating any error on my part.

@utkarshkukreti
Copy link
Contributor

@katkrasinsk what if you add svelte to dependencies too?

@katkrasinsk
Copy link

@utkarshkukreti it does work with adding svelte as dep, but I got a much bigger build, anyway, I am with this solution in order to continue developing the app.

@ghost
Copy link

ghost commented Nov 13, 2021

I noticed that I didn't have "@sveltejs/adapter-node" in my dependencies. Now it's building fine. I wonder if others are doing this by mistake as well.

@bitdom8
Copy link

bitdom8 commented Jul 2, 2023

I have the same error after latest updates:

[ERR_MODULE_NOT_FOUND]: Cannot find module '/usr/local/lsws/nodes/bitfinicon/.svelte-kit/output/server/internal.js' imported from /usr/local/lsws/nodes/node_modules/@sveltejs/kit/src/exports/vite/preview/index.js

@helenawsu
Copy link

I encountered the same error: Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/helena/svelte/personal-website/.svelte-kit/output/routes/blog/0/+page.md' imported from /Users/helena/svelte/personal-website/.svelte-kit/output/server/chunks/index2.js

@geoffrich
Copy link
Member

@bitdom8 @helenawsu commenting on 2-year-old closed issues will not usually get a helpful response. If you're encountering a new bug with SvelteKit, please open a new issue with a minimal reproduction

@helenawsu
Copy link

I encountered the same error: Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/helena/svelte/personal-website/.svelte-kit/output/routes/blog/0/+page.md' imported from /Users/helena/svelte/personal-website/.svelte-kit/output/server/chunks/index2.js

The reason to this was I was using adaptor-static and used dynamic import. I can't build static with dynamic import.

@mdulc928
Copy link

Does quitting VSCode and restarting it back up work for anyone? It worked for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pkg:adapter-node
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants