I'm using NodeJS/Electron for a desktop app.
What I wanna do, is to open a file with it's OS' default application, like .docx with Word.
What I tried so far are approaches using child_process.spawn, .exec or .execFile but I don't get anything.
Here is my actual code:
var fs = require('fs'),
cp = require('child_process');
cp.spawn(__dirname + '/test.docx');
Thanks in advance.
Adding here the snippet for newer electron versions (9+) and imports:
import { shell } from 'electron';
import path from 'path';
shell.openPath(path.join(__dirname, 'test.docx'));
Use the openItem()
function provided by Electron's shell
module, for example:
const shell = require('electron').shell;
const path = require('path');
shell.openItem(path.join(__dirname, 'test.docx'));
According to the docs the shell
module should be available in both the main/browser and renderer processes.
Note: Electron 9.0.0 The
shell.openItem
API has been replaced with an asynchronousshell.openPath
API. shell.openPath docs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With