Apify Discord Mirror

Updated 5 months ago

Injecting local script tag onto page

At a glance
The community member is trying to inject local JavaScript files into web pages, but is encountering "No Such File Or Directory" errors when running the code on the Apify platform. A comment suggests that the issue may be caused by the way the Dockerfile is set up, and that copying the necessary files to the final image may help resolve the problem.
I'm trying to inject a few local .js files onto pages so they can do some tidying before I save the page HTML. Locally, it works well like this:
Plain Text
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const SINGLE_FILES = [
    'single-file-bootstrap.js',
    'single-file-frames.js',
    'single-file-hooks-frames.js',
    'single-file.js'
];

export const SINGLE_FILE_PATHS = SINGLE_FILES.map(file => path.join(__dirname, 'single-file', file));

export async function addScriptsToPage(page: Page) {
    try {
        for (const scriptPath of SINGLE_FILE_PATHS) {
            await page.addScriptTag({ path: scriptPath });
        }
    } catch(e) {
        console.error('Error adding scripts to page', e);
        return { success: false, error: e };
    }
    return { success: true };
}


But when i try to run this on the apify platform, I get "No Such File Or Directory" errors. How can i reference files from my package on the apify platform so I can inject them into a page?
v
1 comment
Hi ,

I assume this is caused by the way your Dockerfile is setup - files that should be injected are not copied to the final image.

Adding something like this under the build command in your Dockerfile should help:
Plain Text
COPY --chown=myuser ./src/single-file ./dist/single-file
Add a reply
Sign up and join the conversation on Discord