How do install dependencies in the tmp directory?

Hi team,

My goal is to write a js file to the /tmp folder, install some dependencies, then run the js file from a worker thread. However I’m getting stuck installing in the /tmp folder.

Here is some of my code, not sure if this even the correct approach.

I run the following code to execute some js code on a worker thread
import { Worker } from ‘worker_threads’;

const worker = new Worker(workerFilePath, { workerData: { imagePath1, imagePath2, imagePath3 } });

This below causes an error for “unexpected require statement”
console.log(“installing dependencies”);
await new Promise((resolve, reject) => {
exec(‘cd /tmp && npm install’, (Ïerror: any) => {
if (error) {
console.error(‘Error installing dependencies:’, error);
reject(error);
}
resolve(null);
});
});

Deployment URL or Custom Domain: https://file-converter-ai1ztc0j1-malikelates-projects.vercel.app/
Environment (local, preview, production): Production 
Project Framework: Astro JS
Build Settings:
  Framework Preset: Astro js 
  Build Command (if not default): 
  Output Directory (if not default):
  Install Command (if not default):
Node/Runtime Version: v20.15.1
Package Manager: npm 10.8.3
Relevant Packages:

This is how I got it working

await mkdir(workersDir, { recursive: true });

        const workerPackageJson = {
            "type": "module",
            "dependencies": {
                "sharp": "latest"
            }
        };
        await writeFile(path.join(workersDir, 'package.json'), JSON.stringify(workerPackageJson, null, 2));

        // Run npm install with specific flags for restricted environments
        console.log("Installing worker dependencies...");
        await new Promise((resolve, reject) => {
            exec(
                'npm install --no-audit --no-fund --prefix /tmp --cache /tmp/npm-cache',
                {
                    env: {
                        ...process.env,
                        HOME: '/tmp',
                        npm_config_cache: '/tmp/npm-cache'
                    }
                },
                (error: any, stdout: string, stderr: string) => {
                    if (error) {
                        console.error('Error installing dependencies:', error);
                        console.error('stderr:', stderr);
                        reject(error);
                        return;
                    }
                    console.log('stdout:', stdout);
                    resolve(null);
                }
            );
        });

The /tmp directory in a Serverless Function has a limit of 512 MB.

However, a better solution is to upload files directly from the client to the S3 bucket using a presigned url.

Then you don’t need to proxy the function req body or write temporary files at all.

1 Like

Hey Swarnava. Normally yes, in this case though I need to edit the images on the server before uploading them to S3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.