Reputation: 81
I've tried a few different npm packages that open files and I have the same issue with all of them.
https://www.npmjs.com/package/opn
https://www.npmjs.com/package/open
the problem: I am not able to open multiple html files on my hard drive at once. but when I try to open just one of them it works fine. any idea what's causing this issue?
This DOESN'T work (the web pages don't open in my browser):
var fs = require('fs');
var opn = require('opn');
fs.writeFile('open.html', 'hello');
fs.writeFile('open1.html', 'hello1');
fs.writeFile('open2.html', 'hello2');
fs.writeFile('open3.html', 'hello3');
fs.writeFile('open4.html', 'hello4');
fs.writeFile('open5.html', 'hello5');
console.log('files created');
opn('open.html');
opn('open1.html');
opn('open2.html');
opn('open3.html');
opn('open4.html');
opn('open5.html');
BUT the code below WORKS (the one web page opens fine in my browser):
var fs = require('fs');
var opn = require('opn');
fs.writeFile('open.html', 'hello');
console.log('file created');
opn('open.html');
Upvotes: 0
Views: 268
Reputation: 3434
If you're aware of the side effects, you can use fs.writeFileSync
instead of fs.writeFile
: https://nodejs.org/api/fs.html#fs_fs_writefilesync_file_data_options
fs.writeFile
is asynchronous and will return immediately (even if the file is not created yet). You can use it with a callback if you want to wait for the operation to finish.
fs.writeFileSync
blocks the main thread until the file is written, and returns only then. If you don't want to change your code too much, you can probably just use that.
Upvotes: 1