Search code examples
node.jsnode-modules

How to open a notepad using node


I am creating a project that my node project can open a notepad.exe

const openyeah = "notepad.exe";
const fs = require("fs");

fs.open(openyeah,"r",(err,fd)=>{
    if(err){
        console.log('errors')
    }else{
        console.log("correct")
    }
})

Solution

  • You need to use the Child Process module to get this done. The child_process module provides the ability to spawn child processes which enable us to open window programs like notepad,exe

    If you look at the below example once we create a spawnObj, we can pass the pass the program name which needs to be executed as a first argument (in our case the notepad.exe) and the relevant input as the second input (in our case the .txt file name. Please check and replace the C:/Users/YOUR_USER_NAME/Desktop/somefile.txt in the below example with a valid path/filename in your PC).

    var spawnObj = require('child_process').spawn,
    progToOpen = spawnObj('C:\\windows\\notepad.exe', ["C:/Users/YOUR_USER_NAME/Desktop/somefile.txt"]);
    

    Hope this helps!