Where should a Netlify helper function be located (using Sveltekit) - this was an ENV variable issue!

Site: https://63cf40958883845fee83dadc--peppy-rolypoly-dd9af0.netlify.app
Netlify.toml:
[build]
command=“svelte-kit build”
publish = “build”

I have an app with several netlify functions that all use a db-helper.js file which runs fine locally using ntl dev and Sveltekit 1.0.

After running npm build and ntl deploy it is telling me it cant find the $lib/db-helper.js file., hence the header of this topic.

In case it was the “$lib”, I moved the file and changed all references to $lib to ‘…/…/src/constants/db-helper’ and now when I run npm run build and ntl deploy and click “Projects”, which calls the first netlify function, it crashes with the attached error, referring to dotenv, which IS installed and I reinstalled and reran and it gives the same message.

I move the db_helper.js file into the lib folder but give it a path ‘…/…/src/lib/db-helper’. Run build again. ntl deploy. Click on “projects” and it now gives a 502 error, but doesnt provide any detail on the error :
"start-dc4a7274.js:1
GET https://63cf4406a0f50b62db2240c2--peppy-rolypoly-dd9af0.netlify.app/.netlify/functions/getProjects 502
"

I moved the helper file into root of src, rebuilt, redeployed. This time it failed to deploy with error:
“$ ntl deploy
» Error: request to https://api.netlify.com/api/v1/sites?filter=all failed, reason: getaddrinfo ENOTFOUND api.netlify.com

Moved it to root of application. Failed to deploy - same error as above.

Four different locations. Three different errors! It seems to matter where this file is located but I have not found the right location.

Is there a specific location for netlify function helper files or is there a way in the netlify.toml file to tell Netlify that this helper file is to be used with the netlify functions?

My netlify functions all import a “client” (Mongodb) from db-helper. In the Netlify functions it creates and connection to the client. It all works locally and it all works in Svelte. But now I am using Sveltekit.

I will try another function tomorrow and see if I can figure out what is happening.

I am using adapter-netlify in svelte.config.js and when i run npm run build it shows “Using @sveltejs/adapter-netlify” with a tick (correct) done. The rest of the app, not using netlify functions is all working.

I would appreciate it if you do not close this request (again) until a resolution is found. Thank you.

This is an issue with DOTENV. No solution yet but it looks like an environment variable handling issue and not the location of the db_helper file.

I added the environment variables to netlify site and removed the "require(‘dotenv’).config() from my db-helper.js file. npm run build, ntl deploy. Works! There are “solutions” documented on Svelekit docs on setting up different types of env variables but for now this simple fix works.
This can be closed.

Hi @kreollc :wave:t6: ,

Thanks so much for coming back and letting us know what got you unstucked.

1 Like

After solving another issue with trying to load jsonwebtoken and getting a similar error “cannot find module jsonwebtoken” and hitting upon the solution to that problem which was to

import * as jwt from ‘jsonwebtoken’;

instead of

const jwt = require(‘jsonwebtoken’)

I revisited this issue because I was previously using “require” for dotenv also and it didnt cause any issue locally, only when deployed.

So, it appears that this is also the issue with dotenv. I removed the environment variables from Netlify site, and added back dotenv to my db-helper file but this time as an import and it worked!

So while removing dotenv and adding the environment variables to netlify site was a solution it was really a workaround because dotenv was failing.

So another solution, instead of

require(‘dotenv’).config();

use …

import dotenv from ‘dotenv’;

dotenv.config();

Glad to hear it! Thanks @kreollc :blush: