This Meteor code uses fs.writeFileSync to save a file to disc with the html extension, it saves the file OK but I get the following server error
While processing files with templating-compiler (for target web.browser): screen_shots/page.html:1: Can't set DOCTYPE here. (Meteor sets for you)
And the following browser console message
Uncaught SyntaxError: Unexpected token < in JSON at position 1
Any idea how to fix this error? Thanks
import { Meteor } from 'meteor/meteor';
const puppeteer = require('puppeteer'); //maybe change const to let
const fs = require('fs')
//.... SOME CODE HERE...
let searchLink = await page.$('input[alt="Search"]')
await searchLink.click()
await page.waitForNavigation()
const html = await page.content()
fs.writeFileSync("/screen_shots/page.html", html, 'utf8') //removing utf8 did not eleminate the error
The location of /screen_shots/page.html is in your source tree, which causes Meteor to start rebuilding, and abort what it's doing. You definitely don't want this. Meteor watches all source files that are explicitly imported, but also anything in client and server folders.
When running meteor or meteor run in this mode, all files excepted of those in the folder /imports are loaded automatically. There are also a few other rules to it, all can be found on this page: https://guide.meteor.com/structure.html#load-order
There is more information here: https://stackoverflow.com/a/64116026/517914
The .png files you also wrote in the same folder will also be loaded automatically, but won't cause errors.
Now that you understand what's going on, you may want to avoid writing to the file system. There are a few reasons for this