Injecting local script tag onto page

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:
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 };
}
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?
1 Reply
ambitious-aqua
ambitious-aqua12mo ago
Hi @kennysmithnanic, 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:
COPY --chown=myuser ./src/single-file ./dist/single-file
COPY --chown=myuser ./src/single-file ./dist/single-file

Did you find this page helpful?