Open external file with OS' default application (docx with Word, etc.) using NodeJS and Electron

Viewed 15248

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.

2 Answers

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'));

Related