Absolute path to file of my project #4614
Replies: 5 comments 6 replies
-
I guess my question boils down to: Is it possible to host a file in Vercel outside of a Serverless function? As I said in another question, I'm new to Vercel, so this might not be the way it's meant to be used. But that would be a bummer, because it's the last piece of the puzzle for me. I just need access to a file (which is not a serverless function, meaning there is no export of (req, res)) as said above. I hope I can find a solution to this :/ |
Beta Was this translation helpful? Give feedback.
-
Hey Marc, The easiest way to do this is to place the file inside of the As a heads up, I've sent the same reply to your ticket also - please let me know the best place for you to discuss this 👍 |
Beta Was this translation helpful? Give feedback.
-
Here is far better, I didn't know about this forum before 👍 About your suggestion: the However, I would I be able to access is in the file system? (N.B: as you see on the above snippet, it needs to be accessed from the file system and not by an http call, so https://my-domain/public/myscript.js won't work) |
Beta Was this translation helpful? Give feedback.
-
Try the following: // File is api/example.js
const { fork } = require('child_process')
const { join } = require ('path')
const child = fork(join(__dirname, '../child.js')) That being said, I think forking a child process is an anti-pattern for serverless. Think of the parent process as the http client and the child process as the serverless function. "Fork" should really be "HTTP GET" and the function itself should not be running multiple processes. As the traffic increases, the function will scale accordingly automatically. |
Beta Was this translation helpful? Give feedback.
-
Here is what works for me:
Note that path.resolve is required to dynamically set the path in vercel environment |
Beta Was this translation helpful? Give feedback.
-
Hi,
In my code, I fork the main node process into a child process using
It works great locally, but not in production because "some-script.js" is not found at path
/var/task/some-script.js
. My guess is that locally I run node from my project's root folder, whereas in Vercel, node is ran from somewhere else.My question is: What is the absolute path to my "some-script.js" file (knowing that it sits at the root of my project)?
I have already tried
__dirname
,process.cwd
and__filename
.Thank you to anyone for their help!
Beta Was this translation helpful? Give feedback.
All reactions