chrome-launcher
chrome-launcher copied to clipboard
Issue determining temp dir path
I think there is a strong chance this is not an issue with chrome-launcher, and instead one with my environment, but I'm unsure how to make that determination.
I have an mjs file that when run standalone works fine.
import lighthouse from 'lighthouse';
import * as chromeLauncher from 'chrome-launcher';
(async () => {
const chrome = await chromeLauncher.launch({
chromeFlags: ['--headless', '--quiet'],
chromePath: '/usr/bin/google-chrome-stable', // Specify the path to the Chrome executable in WSL
});
// Check if a URL is provided as a command line argument
if (process.argv.length < 3) {
console.error('Please provide a URL as a command line argument.');
process.exit(1);
}
const url = process.argv[2]; // Get the URL from the command line argument
const options = { logLevel: 'info', output: ['html', 'json'], port: chrome.port, tmpdir: '/tmp/' };
const runnerResult = await lighthouse(url, options);
// Output the JSON result to stdout
console.log(JSON.stringify(runnerResult.lhr));
await chrome.kill();
})();
I also have a python wrapper that invokes the above script, that when run standalone runs fine.
import subprocess
import sys
import json
class LighthouseScanner:
def scan(self, url):
try:
lighthouse_command = f"/home/rowc/.nvm/versions/node/v20.9.0/bin/node sites/lighthouse/LighthouseScanner.mjs {url}"
# Run the Lighthouse command and capture the output and error
result = subprocess.run(lighthouse_command, shell=True, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# Check the exit status
if result.returncode != 0:
# If the exit status is non-zero, print the standard error
print(f"Error running Lighthouse:\n{result.stderr}")
return None
# print(result.stdout)
# Parse the standard output (JSON) into a Python object
lighthouse_data = json.loads(result.stdout)
# Print a success message
print("Lighthouse audit completed successfully.")
# Return the JSON object
return lighthouse_data
except subprocess.CalledProcessError as e:
# Print an error message if the command fails
print(f"Error running Lighthouse: {e}")
return None
# Usage
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python lighthouse_scanner.py <URL>")
sys.exit(1)
url = sys.argv[1]
scanner = LighthouseScanner()
print("Scanning URL:", url)
lighthouse_result = scanner.scan(url)
if lighthouse_result:
# Use the lighthouse_result JSON object as needed
print("Accessibility score:", lighthouse_result["categories"]["accessibility"]["score"])
However, when i run the python code from within its container django application, i get an error. From the bit of reverse engineering I attempted, it appears to be failing to parse the correct user name and mount info from the PATH.
In my particular case I am running pycharm in windows, and my application is using WSL.
Error: EACCES: permission denied, mkdir '/mnt/undefined/Users/undefined/AppData/Local/lighthouse.74073726'
at mkdirSync (node:fs:1379:3)
at makeWin32TmpDir (file:///home/rowc/source/repos/mgmt/node_modules/chrome-launcher/dist/utils.js:117:5)
at makeTmpDir (file:///home/rowc/source/repos/mgmt/node_modules/chrome-launcher/dist/utils.js:66:20)
at Launcher.makeTmpDir (file:///home/rowc/source/repos/mgmt/node_modules/chrome-launcher/dist/chrome-launcher.js:130:16)
at Launcher.prepare (file:///home/rowc/source/repos/mgmt/node_modules/chrome-launcher/dist/chrome-launcher.js:137:53)
at Launcher.launch (file:///home/rowc/source/repos/mgmt/node_modules/chrome-launcher/dist/chrome-launcher.js:197:18)
at Module.launch (file:///home/rowc/source/repos/mgmt/node_modules/chrome-launcher/dist/chrome-launcher.js:33:20)
at file:///home/rowc/source/repos/mgmt/sites/lighthouse/LighthouseScanner.mjs:7:41
at file:///home/rowc/source/repos/mgmt/sites/lighthouse/LighthouseScanner.mjs:27:3
at ModuleJob.run (node:internal/modules/esm/module_job:217:25) {
errno: -13,
syscall: 'mkdir',
code: 'EACCES',
path: '/mnt/undefined/Users/undefined/AppData/Local/lighthouse.74073726'
}
I was thinking it might make sense to accept a parameter to specify a temp directory, but I was also wondering if there was an existing solution to this that I was unaware of.