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

@sveltejs/adapter-node: Unable to find certain packages in built app #2139

Closed
sridharraman opened this issue Aug 9, 2021 · 5 comments · Fixed by #2724
Closed

@sveltejs/adapter-node: Unable to find certain packages in built app #2139

sridharraman opened this issue Aug 9, 2021 · 5 comments · Fixed by #2724
Labels
documentation Improvements or additions to documentation pkg:adapter-node

Comments

@sridharraman
Copy link

Describe the bug

Certain packages don't seem to get exported into the build when using the node adapter. npm run preview for the built app works fine, but if the build folder is run as a stand-alone node server using a barebones package.json and with the command node index.js, the following error appears for some packages:

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

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'pg-promise' imported from /home/sridhar/work/occ/data-dashboard/workspace/builds/node_v4/index.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. (internal/modules/esm/module_job.js:56:40)
at link (internal/modules/esm/module_job.js:55:36) {
code: 'ERR_MODULE_NOT_FOUND'
}

This doesn't happen for all packages though. In my demo app, there are three packages in production dependencies. While svelte-frappe-charts, dotenv work fine, pg and pg-promise don't.

Due to this, deployment to an environment like AWS fails, with the same Error [ERR_MODULE_NOT_FOUND] error. Is this an issue with adapter-node related to the packages?

Reproduction

I've created a demo repo here - https://github.com/sridharraman/sveltekit-api-chart

If you want to test the api endpoint, you can run the following SQL statements in your database:

create table orders(
id integer primary key,
name text
);

insert into orders(id, name) values (1, 'Lorem');
insert into orders(id, name) values (2, 'Ipsum');
insert into orders(id, name) values (3, 'Scriptum');

In the dev environment, these are the links that work fine:
localhost:3000/api/orders
localhost:3000/chart

They show that the database and chart packages work fine.

Logs

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

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'pg-promise' imported from /home/user/work/build/index.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'
}

System Info

System:
    OS: Linux 5.11 Ubuntu 20.04.2 LTS (Focal Fossa)
    CPU: (4) x64 Intel(R) Core(TM) i5-3337U CPU @ 1.80GHz
    Memory: 250.76 MB / 7.65 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 14.15.3 - ~/.nvm/versions/node/v14.15.3/bin/node
    Yarn: 1.22.5 - /usr/bin/yarn
    npm: 7.16.0 - ~/.nvm/versions/node/v14.15.3/bin/npm
  Browsers:
    Chrome: 92.0.4515.107
    Firefox: 90.0.2
  npmPackages:
    @sveltejs/adapter-node: ^1.0.0-next.39 => 1.0.0-next.39 
    @sveltejs/adapter-vercel: next => 1.0.0-next.27 
    @sveltejs/kit: next => 1.0.0-next.142 
    svelte: ^3.34.0 => 3.42.1

Severity

blocking all usage of SvelteKit

Additional Information

No response

@ignatiusmb
Copy link
Member

Dependencies aren't bundled into the final build. Did you copy the package.json and installed the dependencies on the prod server?

@sridharraman
Copy link
Author

@ignatiusmb I assumed that the build folder was a stand-alone node server and had all the dependencies included in it. Let me try with the full package.json. I was only using a basic one (like below) for deployment.

{
  "type": "module", 
  "name": "sveltekit-0",
  "version": "0.0.1",
  "scripts": {
    "start": "node index.js"
  }, 
  "engines": { "node" : "14.16.0" }
}

But, why does the svelte-frappe-charts dependency work fine?

@ignatiusmb
Copy link
Member

You should make sure the dependencies in the original package.json exists in the server.

svelte-frappe-charts exposes a "svelte" key in its package file, which SvelteKit uses to determine if it's a Svelte package and bundles them regardless where we put it.

@sridharraman
Copy link
Author

svelte-frappe-charts exposes a "svelte" key in its package file, which SvelteKit uses to determine if it's a Svelte package and bundles them regardless where we put it.

Ah understood. But for packages that expose a "svelte" key, they don't get bundled. That makes sense.

You should make sure the dependencies in the original package.json exists in the server.

So, if I am using AWS EB to deploy the build app, how do I do it? Do I have to install the dependencies in the package.json within the build folder? And then push the folder to AWS? Or is there a better approach?

@ignatiusmb
Copy link
Member

You're free to do it however you like, I suggest you seek help on either the Discord or Stack Overflow.


From the relevant PR #1648

This means that, in production, you should be able to copy over your package.json and build/, run npm install --production, and run node build - which is what you'd expect to be able to do.

It seems this hasn't been properly documented in the package readme, which is pretty important as we had similar issues with deploying Sapper app back then. Marking as a documentation issue instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation pkg:adapter-node
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants