# Redwood Deployment Serverless Function Error

I have been working on website for 3 weeks now, and I am way pass the deadline. I have been stuck with one error for 2 weeks. If anyone has seen this before and has a answer, it would help me lot, I have already seen this [post](https://answers.netlify.com/t/error-loading-images-on-deployed-site-runtime-importmoduleerror-error-cannot-find-module-utils/69526) error loading images on deployed site runtime, basically you could make a argument that the images I have on the website are too big I will be moving that CDN or make them smaller somehow.

I really don’t think it s the images, its something to do with Serverless function and its folder structure. Here’s the really error.

And the Function Log outputs the same

Aug 21, 12:27:06 PM: 2022-08-21T16:27:06.179Z	undefined	ERROR	Uncaught Exception 	{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module '..\\..\\lib\\auth'\nRequire stack:\n- /var/task/api/dist/directives/requireAuth/requireAuth.js\n- /var/task/api/dist/functions/graphql.js\n- /var/task/graphql.js\n- /var/runtime/index.mjs","stack":["Runtime.ImportModuleError: Error: Cannot find module '..\\..\\lib\\auth'","Require stack:","- /var/task/api/dist/directives/requireAuth/requireAuth.js","- /var/task/api/dist/functions/graphql.js","- /var/task/graphql.js","- /var/runtime/index.mjs","    at _loadUserApp (file:///var/runtime/index.mjs:726:17)","    at async Object.module.exports.load (file:///var/runtime/index.mjs:741:21)","    at async file:///var/runtime/index.mjs:781:15","    at async file:///var/runtime/index.mjs:4:1"]}
Aug 21, 12:27:08 PM: 619d13cc Duration: 2272.38 ms	Memory Usage: 95 MB	Aug 21, 12:27:08 PM: Unknown application error occurred
Runtime.ImportModuleError


this is my netlify.toml file

[build]
command = "yarn rw deploy netlify"
publish = "web/dist"
# base = "trashremover"
functions = "api/dist/functions"
included_files = ["api/dist/**/*.js"]
# included_files = ["api/dist/lib/*"]
[dev]
# To use [Netlify Dev](https://www.netlify.com/products/dev/),
# install netlify-cli from https://docs.netlify.com/cli/get-started/#installation
# to connect your local project to a site already on Netlify
# then run netlify dev and our app will be accessible on the port specified below
framework = "redwoodjs"
# Set targetPort to the [web] side port as defined in redwood.toml
targetPort = 8910
# Point your browser to this port to access your RedwoodJS app
port = 8888

[[redirects]]
from = "/*"
to = "/200.html"
status = 200


Your netlify.toml is incorrect:

That needs to be under [functions] block, for example:

[functions]
directory = "api/dist/functions"
included_files = ["api/dist/**/*.js"]


However, I don’t think that’s the cause of the problem here - could be, but can’t say without looking at the source code? Do you have a repo to share? Or maybe a screenshot of your file structure?

I will give that shot and get back to you if it works.

I recreated the exact same issue in a public repo

here’s the link to the original error
https://diamondhaulingpa.com/.netlify/functions/graphql

I recreated the error here
https://testingredwooddeployment.netlify.app/.netlify/functions/graphql

heres the repo thank you for checking this out

it didn’t work got the same error? any thoughts rrelated to error like the file structure in the serverless functions??

Are you using backslashes in your code, @Gresliebear ? I think we might have missed that right at the beginning:

…whereas on Netlify while webuild, and while your lambda runs, we will be on unix

If you do have any code referring to backslash-delimited directories, could you try without it?

@fool Good Afternoon fool!,

Ya so update on this issue if I use the Netlify CLI to build my zip it and ship it that deployment clearly failes due to the file pathing, E.I. the serverless function fails and on the Mac also this build fails. The netlify CLI build steps don’t account for this to be honest I don’t get it its a black box to me.

I cannot try backslash-delimited directories, because windows and even macOS does it different from Unix right. its the compiler as it was explain to me by another developer.

So I was told that the builder on windows will not create the correct files path for Netlify servers. So I cannot do anything to fix unless I move to Linux or abandoned the CLI overall.

I had a idea to hack a solution is run docker of container unix then build it with Netlify CLI and then ship it to the Netlify server but honestly I don’t have time. I have other issues and projects I need to work on.

I fixed my issue by reverting back to Netlify default UI deployment pipeline. So we can close the issue now.

Hey @Gresliebear

Windows is different to Linux/Unix, but macOS is built on (and is accredited as) Unix, so it uses the same file paths as Netlify build, i.e. /path/to/some/file unlike Windows which uses \path\to\some\file.

I did try building the repository you shared, but it requires a DB which I don’t have (let alone one configured the same as you.) Is it possible you can replicate the issue as a standalone thing without the requirement of a DB?

The problem is that these functions that were deployed use the DB no matter what, I don’t really know to untie that relationship in redwoodJS.

I will show you how to set up the DB I used Supabase it creates a free temp DB with its own keys and ip port password you can easily login with github account.

macOS is built on Unix right but another developer told me the same issue occured that the pathing was still messed up he just didn’t document or screenshot it.

Hey @Gresliebear,

Not sure what I’m missing, but when I checked your repo, I don’t see the folder api/dist. I only see api/src and some other files in the api folder. Are you building the functions yourself?

Yes, I am building with Netlify CLI, Let me update the readme with these steps.

git clone https://github.com/Gresliebear/TestingRedwoodDeployment


we get clone, then set up redwood app

yarn install


run app

yarn rw dev


So where netlify comes in redwood has its own setup process you need to run to create a netlify.toml file

yarn rw setup deploy netlify


So to deploy the app correctly you need to follow these steps in the doc Deployment | RedwoodJS Docs but I used Supabase instead of railway, works the same as railway. This means you need a DATABASE URL to the ip address and port to database. Just make sure .env is correct. You can test this with yarn rw dev to make sure the connection is good before deploying.

You can access a free database at supabase https://app.supabase.com/ or railway here https://railway.app/account/usage you can login via github account. They both work the same you just need, the connection string in .env file that was generated from the redwood set up.

postgres://postgres:[YOUR-PASSWORD]@db.fxmqshrlefcjjatkgywq.supabase.co:6543/postgres

Anyways once you get netlify.toml file created you can deploy via’s Netlify UI that works correctly, but when I use Netlify CLI it doesn’t work, I understand thats it in beta but I am trying to help here by identify issues with using the Netlify CLI on Windows and MacOS.

so lets try to deploy via the Netlify CLI.

we only need to run

netlify deploy --build --prod


This command generates api/dist
this will ask some questions like link this directory. and then it will build according to netlify.toml

Unfortunately you will need to make a Database account, and make a Supabase account with your github account. You will to access the connection string in the settings page

Place the connection string in the .env

# THIS FILE SHOULD NOT BE CHECKED INTO YOUR VERSION CONTROL SYSTEM

#

# Environment variables set here will override those in .env.defaults.

# Any environment variables you need in production you will need to setup with

# your hosting provider. For example in Netlify you can add environment

# variables in Settings > Build & Deploy > environment

#

# DATABASE_URL=postgres://user:pass@postgreshost.com:5432/database_name

# TEST_DATABASE_URL=postgres://user:pass@postgreshost.com:5432/test_database_name

#

# Sets an app-specific secret used to sign and verify your own app's webhooks.

# For example if you schedule a cron job with a signed payload that later will

# then invoke your api-side webhook function you will use this secret to sign and the verify.

# Important: Please change this default to a strong password or other secret

# Used to encrypt/decrypt session cookies. Change this value and re-deploy to log out all users of your app at once.

SESSION_SECRET=[SECRET KEY]

# connection serect for netliofy h3HmA8F7seHeFCfmLheTmFgyXuierS6FwYvGmr8v8PJ8tnias2Ck93P7VYFt2WZ9


you will to generate a secret key also

yarn rw g secret

Wow, that’s a lot of detail, thank you!

This is starting to sound rather like a redwoodjs problem, not a netlify problem. What’s happened when you asked their team for guidance, since nobody here in Netlify Support has much experience with it?

A redwood developer already reach out to Netlify, he says its not a Redwood Problem because its directly related to how it builds and complies on the Windows and MacOS. I think.

Like redwoodJS works fine on Netlify its just deploying with Netlify CLI is not going to work.

I am not saying its not Netlify’s problem or RedwoodJS problem but the Developer experience has been one big game of hot potato of who’s open source code responsibility is it to solve these issues?

So is its redwoodjs problem? well I spent over 10+ hours and I can tell you its not that, its could the compiler or the default builder on netlify.toml file. if you change to esbuild will it work? I don’t know.
RedwoodJS is strictly using what Netlify has provided that UI works but CLI doesn’t work its in beta.

At the end of day the Netlify is a business and that business sells a Developer Experience if that Experience is not smooth or seemless. As a customer I move on, I only stick to Netlify because I love it.

its up to Netlify if they want to resolve it or not I don’t even know what truly is causing the build to fail because I cannot see how you guys build on Serverless function in the first place.

My solution was completely word of mouth and asking questions. I spent hours on it. I just don’t want the next person to spend hours on the same problem etc.

If the functions require esbuild, you can specify this in the netlify.toml

[functions]
node_bundler = "esbuild"


If there wasn’t the need to create a specify database for testing (i.e. if you could provide a public DB for to access for testing purposes) I would have spend more time testing as I too use macOS.

its a blackbox, I cannot debug this issue, I need netlify dev to tell me what’s breaking TDLR

[build]
command = "yarn rw deploy netlify"
publish = "web/dist"
[functions]
node_bundler = "esbuild"
directory = "api/dist/functions"



I will run this right 2 minutes please

Ok so the results are here

 yarn rw deploy netlify

Running:
yarn rw build --verbose && yarn rw prisma migrate deploy && yarn rw data-migrate up

[20:31:43] Generating Prisma Client... [started]

✔ Generated Prisma Client (3.15.2 | library) to .\..\node_modules\@prisma\client in 296ms
You can now start using Prisma Client in your code. Reference: https://pris.ly/d/client


import { PrismaClient } from ‘@prisma/client’
const prisma = new PrismaClient()

[20:31:49] Generating Prisma Client... [completed]
[20:31:49] Verifying graphql schema... [started]
[20:31:49] Verifying graphql schema... [completed]
[20:31:49] Building API... [started]
[20:31:51] Building API... [completed]
[20:31:51] Cleaning Web... [started]
[20:31:51] Cleaning Web... [completed]
[20:31:51] Building Web... [started]
assets by path static/js/*.js 721 KiB
assets by chunk 160 KiB (id hint: vendors)
asset static/js/759.14a48152.chunk.js 97 KiB [emitted] [immutable] [minimized] (id hint: vendors) 1 related asset
asset static/js/857.f09cf427.chunk.js 33.4 KiB [emitted] [immutable] [minimized] (id hint: vendors) 1 related asset
asset static/js/745.f20b6673.chunk.js 13.9 KiB [emitted] [immutable] [minimized] (id hint: vendors) 1 related asset
asset static/js/656.f2691b9f.chunk.js 8 KiB [emitted] [immutable] [minimized] (id hint: vendors) 1 related asset
asset static/js/532.46b9f7cf.chunk.js 7.43 KiB [emitted] [immutable] [minimized] (id hint: vendors) 1 related asset
+ 15 assets
asset static/css/app.a635e010.css 6.72 KiB [emitted] [immutable] [minimized] (name: app)
asset favicon.png 1.7 KiB [emitted] [from: public/favicon.png] [copied]
asset build-manifest.json 1.54 KiB [emitted]
asset index.html 464 bytes [emitted]
asset robots.txt 24 bytes [emitted] [from: public/robots.txt] [copied]
Entrypoint app [big] 495 KiB = static/js/runtime-app.ec5f3d1a.js 4.06 KiB static/css/app.a635e010.css 6.72 KiB static/js/app.108ac436.js 484 KiB
orphan modules 851 KiB (javascript) 4.98 KiB (runtime) [orphan] 239 modules
runtime modules 10.5 KiB 15 modules
modules by path ../node_modules/ 1.45 MiB 692 modules
modules by path ./src/ 117 KiB (javascript) 7.69 KiB (css/mini-extract)
modules by path ./src/pages/ 107 KiB
modules by path ./src/pages/Post/ 22.4 KiB 4 modules
+ 10 modules
modules by path ./src/*.css 7.69 KiB
css ../node_modules/css-loader/dist/cjs.js??ruleSet[1].rules[0].oneOf[3].use[1]!./src/scaffold.css 6.86 KiB [built] [code generated]
css ../node_modules/css-loader/dist/cjs.js??ruleSet[1].rules[0].oneOf[3].use[1]!./src/index.css 858 bytes [built] [code generated]
modules by path ./src/components/ 2.68 KiB
./src/components/Article/Article.js 587 bytes [built] [code generated]
./src/components/Post/PostForm/PostForm.js 2.11 KiB [built] [code generated]
./src/App.js + 5 modules 7.26 KiB [built] [code generated]

WARNING in asset size limit: The following asset(s) exceed the recommended size limit (244 KiB).
This can impact web performance.
Assets:
static/js/app.108ac436.js (484 KiB)

WARNING in entrypoint size limit: The following entrypoint(s) combined asset size exceeds the recommended limit (244 KiB). This can impact web performance.
Entrypoints:
app (495 KiB)
static/js/runtime-app.ec5f3d1a.js
static/css/app.a635e010.css
static/js/app.108ac436.js

webpack 5.73.0 compiled with 2 warnings in 29060 ms
Creating 200.html...
[20:32:36] Building Web... [completed]
[20:32:36] Prerendering Web... [started]
[20:32:36] Prerendering Web... [completed]

Running Prisma CLI...
\$ yarn prisma migrate deploy --schema E:\RedwoodApp\redwooddeploy\TestingRedwoodDeployment\api\db\schema.prisma

Datasource "db": PostgreSQL database "postgres", schema "public" at "db.fxmqshrlefcjjatkgywq.supabase.co:5432"

1 migration found in prisma/migrations

No pending migrations to apply.

No data migrations run, already up-to-date.

(build.command completed in 1m 44.4s)

────────────────────────────────────────────────────────────────
2. Functions bundling
────────────────────────────────────────────────────────────────

Packaging Functions from api\dist\functions directory:
- auth.js
- graphql.js

X [ERROR] Could not resolve "..\\lib\\db"

api/dist/functions/auth.js:23:24:
23 │ var import_db = require("..\\lib\\db");
╵                         ~~~~~~~~~~~~~

Use the relative path "./..\\lib\\db" to reference the file "api/dist/lib/db.js". Without the
leading "./", the path "..\\lib\\db" is being interpreted as a package path instead.

X [ERROR] Could not resolve "..\\lib\\db"

api/dist/functions/graphql.js:31:24:
31 │ var import_db = require("..\\lib\\db");
╵                         ~~~~~~~~~~~~~

Use the relative path "./..\\lib\\db" to reference the file "api/dist/lib/db.js". Without the
leading "./", the path "..\\lib\\db" is being interpreted as a package path instead.

X [ERROR] Could not resolve "..\\lib\\logger"

api/dist/functions/graphql.js:32:28:
32 │ var import_logger = require("..\\lib\\logger");
╵                             ~~~~~~~~~~~~~~~~~

Use the relative path "./..\\lib\\logger" to reference the file "api/dist/lib/logger.js". Without
the leading "./", the path "..\\lib\\logger" is being interpreted as a package path instead.

X [ERROR] Could not resolve "..\\lib\\auth"

api/dist/functions/graphql.js:35:26:
35 │ var import_auth = require("..\\lib\\auth");
╵                           ~~~~~~~~~~~~~~~

Use the relative path "./..\\lib\\auth" to reference the file "api/dist/lib/auth.js". Without the
leading "./", the path "..\\lib\\auth" is being interpreted as a package path instead.

X [ERROR] Could not resolve "..\\..\\lib\\db"

api/dist/services/posts/posts.js:27:24:
27 │ var import_db = require("..\\..\\lib\\db");
╵                         ~~~~~~~~~~~~~~~~~

Use the relative path "./..\\..\\lib\\db" to reference the file "api/dist/lib/db.js". Without the
leading "./", the path "..\\..\\lib\\db" is being interpreted as a package path instead.

X [ERROR] Could not resolve "..\\..\\lib\\auth"

api/dist/directives/requireAuth/requireAuth.js:25:26:
25 │ var import_auth = require("..\\..\\lib\\auth");
╵                           ~~~~~~~~~~~~~~~~~~~

Use the relative path "./..\\..\\lib\\auth" to reference the file "api/dist/lib/auth.js". Without
the leading "./", the path "..\\..\\lib\\auth" is being interpreted as a package path instead.

X [ERROR] Could not resolve "..\\..\\lib\\db"

api/dist/services/contacts/contacts.js:27:24:
27 │ var import_db = require("..\\..\\lib\\db");
╵                         ~~~~~~~~~~~~~~~~~

Use the relative path "./..\\..\\lib\\db" to reference the file "api/dist/lib/db.js". Without the
leading "./", the path "..\\..\\lib\\db" is being interpreted as a package path instead.

X [ERROR] Could not resolve "@magic-sdk/admin"

╵               ~~~~~~~~~~~~~~~~~~

You can mark the path "@magic-sdk/admin" as external to exclude it from the bundle, which will
remove this error. You can also surround this "require" call with a try/catch block to handle this
failure at run-time instead of bundle-time.

X [ERROR] Could not resolve "firebase-admin"

node_modules/@redwoodjs/api/dist/auth/decoders/firebase.js:13:24:
╵                         ~~~~~~~~~~~~~~~~

You can mark the path "firebase-admin" as external to exclude it from the bundle, which will
remove this error. You can also surround this "require" call with a try/catch block to handle this
failure at run-time instead of bundle-time.

X [ERROR] Could not resolve "@clerk/clerk-sdk-node/instance"

node_modules/@redwoodjs/api/dist/auth/decoders/clerk.js:17:24:
17 │   const Clerk = require('@clerk/clerk-sdk-node/instance').default;
╵                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can mark the path "@clerk/clerk-sdk-node/instance" as external to exclude it from the bundle,
which will remove this error. You can also surround this "require" call with a try/catch block to
handle this failure at run-time instead of bundle-time.

X [ERROR] Could not resolve "@clerk/clerk-sdk-node/instance"

node_modules/@redwoodjs/api/dist/auth/decoders/clerk.js:17:24:
17 │   const Clerk = require('@clerk/clerk-sdk-node/instance').default;
╵                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can mark the path "@clerk/clerk-sdk-node/instance" as external to exclude it from the bundle,
which will remove this error. You can also surround this "require" call with a try/catch block to
handle this failure at run-time instead of bundle-time.

X [ERROR] Could not resolve "@magic-sdk/admin"

╵               ~~~~~~~~~~~~~~~~~~

You can mark the path "@magic-sdk/admin" as external to exclude it from the bundle, which will
remove this error. You can also surround this "require" call with a try/catch block to handle this
failure at run-time instead of bundle-time.

X [ERROR] Could not resolve "firebase-admin"

node_modules/@redwoodjs/api/dist/auth/decoders/firebase.js:13:24:
╵                         ~~~~~~~~~~~~~~~~

You can mark the path "firebase-admin" as external to exclude it from the bundle, which will
remove this error. You can also surround this "require" call with a try/catch block to handle this
failure at run-time instead of bundle-time.

❯ Failed to bundle functions with selected bundler (fallback used):
- auth.zip
- graphql.zip

(Functions bundling completed in 1m 10.8s)

────────────────────────────────────────────────────────────────
Netlify Build Complete
────────────────────────────────────────────────────────────────

(Netlify Build completed in 2m 55.3s)
Deploy path:        E:\RedwoodApp\redwooddeploy\TestingRedwoodDeployment\web\dist
Functions path:     E:\RedwoodApp\redwooddeploy\TestingRedwoodDeployment\api\dist\functions
Configuration path: E:\RedwoodApp\redwooddeploy\TestingRedwoodDeployment\netlify.toml
Deploying to main site URL...
✔ Deploying functions from cache (use --skip-functions-cache to override)
✔ Finished hashing 47 files and 2 functions
✔ CDN requesting 1 files and 0 functions
✔ Deploy is live!

Logs:              https://app.netlify.com/sites/testingredwooddeployment/deploys/630d5b05716a0f4ed899ba64
Unique Deploy URL: https://630d5b05716a0f4ed899ba64--testingredwooddeployment.netlify.app
Website URL:       https://testingredwooddeployment.netlify.app
PS E:\RedwoodApp\redwooddeploy\TestingRedwoodDeployment>


I believe its related to this pathing so when we build with netlify CLI because I built on windows machine instead Debian OS the netlify serverless won’'t work because I didn’t built on Debian*

Use the relative path “./…\…\lib\auth” to reference the file “api/dist/lib/auth.js”. Without
the leading “./”, the path “…\…\lib\auth” is being interpreted as a package path instead.

So in the serverless function the server didn’t update as seen in this screen shot because that build command failed so this relates to

Wait a minute I am worng the devs told that prisma client was failing to build so its a redwoodJS issue but then it goes to Prisma I guess.

“Problems arise with some package locations and how the list in bundled … but the biggest issue is that the Prisma client will build using Windows or OSX and Netlfiy deploys need a linux variant.”

Basically they said RedwoodJS will make advisory notice not to use the Netlify CLI etc