User Agents

Web browsers and everyday web tech, explained

Open Local Files with Relative References in Puppeteer Firefox

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Opening local files with relative references in Puppeteer Firefox means loading an HTML file from your disk and having its linked CSS, JavaScript, and images resolve correctly without absolute paths. This is useful when you want to test a static site or a local build exactly as it would behave from a web server, but without setting one up. The key is to use file:// URLs and ensure your relative paths are correct from the HTML file's location. You'll need Node.js, Puppeteer with Firefox support, and a local project folder.

What You'll Need

  • Node.js (version 14 or later) installed
  • Puppeteer installed with Firefox support (npm install puppeteer and set PUPPETEER_PRODUCT=firefox during install, or use puppeteer-firefox package)
  • A local HTML file with relative references (e.g., ./style.css, ./script.js)

Step-by-Step

  1. Set up your project folder. Create a directory containing your HTML file and its assets. For example, my-site/index.html, my-site/css/style.css, my-site/js/app.js.
  2. Install Puppeteer with Firefox. In your terminal, run: PUPPETEER_PRODUCT=firefox npm install puppeteer. This downloads Firefox for Puppeteer. Alternatively, use the older puppeteer-firefox package if you prefer.
  3. Write your script. Create a file, e.g., open-local.js, and require Puppeteer: const puppeteer = require('puppeteer');
  4. Launch Firefox. In your script, add: const browser = await puppeteer.launch({product: 'firefox'}); (if you installed with the env variable, you can also just use puppeteer.launch()).
  5. Create a page. const page = await browser.newPage();
  6. Build the file URL. Use Node's path module to convert your file path to a proper URL: const path = require('path'); const fileUrl = 'file://' + path.resolve('my-site/index.html'); On Windows, you may need to add an extra slash: 'file:///' + path.resolve(...).
  7. Navigate to the file. await page.goto(fileUrl);
  8. Wait for assets to load. Since file:// loads synchronously, but scripts may be deferred, use await page.waitForSelector('body'); or a more specific selector to ensure your content is ready.
  9. Interact or test. You can now evaluate JavaScript, take screenshots, or extract data. For example: const title = await page.title();
  10. Close the browser. await browser.close();

Common Mistakes

  • Forgetting to set the product to Firefox. If you don't set PUPPETEER_PRODUCT=firefox during install, Puppeteer will download Chrome and your launch({product: 'firefox'}) will fail.
  • Using absolute paths that don't exist. Relative references like src="/js/app.js" (leading slash) are interpreted as from the root of the file system, not your project folder. Use ./js/app.js or js/app.js.
  • Wrong file URL format on Windows. Windows paths need file:///C:/... with three slashes. Using file://C:/... will fail.
  • Not waiting for dynamic content. If your page loads scripts that modify the DOM, page.goto() may resolve before those scripts run. Use waitForSelector or waitForFunction.

Tips

  • Use path.join(__dirname, 'my-site', 'index.html') to build paths that work regardless of your current working directory.
  • If you have many assets, consider running a simple static server (e.g., npx serve) and navigate to http://localhost:3000 instead. This avoids file:// restrictions like CORS for modules.
  • For ES modules (type="module"), file:// often triggers CORS errors. Use a local server for those.
  • To debug, add page.on('console', msg => console.log(msg.text())) and page.on('pageerror', err => console.log(err)) to see errors.

Troubleshooting

  • Error: Could not find Firefox. Reinstall with the env variable: PUPPETEER_PRODUCT=firefox npm install puppeteer.
  • Page loads blank. Check the console for errors using the tip above. Common causes: missing files, incorrect relative paths, or JavaScript errors.
  • Relative references not loading. Open the file in a regular browser (double-click) and see if it works. If it does, your file URL is correct; if not, fix your paths.
  • CORS errors. This happens with modules or fetch requests. Switch to a local HTTP server.
  • Timeout on page.goto(). Increase the timeout: await page.goto(fileUrl, {timeout: 60000}).

Frequently Asked Questions

Can I use relative references without a server?

Yes, for simple HTML, CSS, and classic scripts, file:// works. But for ES modules or fetch calls, you'll hit CORS restrictions.

How do I make Puppeteer use Firefox instead of Chrome?

Set the environment variable PUPPETEER_PRODUCT=firefox when installing, and then use puppeteer.launch({product: 'firefox'}) in your script.

What is the correct file URL for Windows?

Use file:///C:/path/to/file.html (three slashes). For example, file:///C:/Users/me/my-site/index.html.

Why do my images not show up?

Check that your image paths are relative to the HTML file, not to the current working directory. Also, ensure the image files exist and are not blocked by file permissions.

Can I navigate to a local file with query parameters?

Yes, you can append ?foo=bar to the file URL, but it may not be processed by the browser in the same way as a server. It's better to use a server for that.

Is there a way to avoid CORS with file://?

No, browsers block cross-origin requests from file://. Use a local server like http-server or live-server to avoid these issues.