Wednesday, 31 May 2017

Run node fs command as root

I am making a small NPM plugin(plugin is global, trying to make a small CLI tool), I am cloning repo using simple-git. I want to delete the directory if the repo cloned before or there is a directory with the same name as repo. Here is my code:

if (files.directoryExists(clonePath)) {
  fs.unlink(clonePath, (err) => {
    if (err) {
      console.log(chalk.red('#######################'));
      console.log(err);
      console.log(chalk.red('#######################'));
      return false;
    }

    cloneFlash.cloneFlash(clonePath);
  })
}

I am getting following error when fs.unlinkis executed:

{
  Error: EPERM: operation not permitted, unlink '/Users/bharatsoni/Documents/projects/flash-generator/test'
    at Error (native)
  errno: -1,
  code: 'EPERM',
  syscall: 'unlink',
  path: '/Users/bharatsoni/Documents/projects/flash-generator/test' 
}

I also have changed the permission of /use/local directory. Now I can install my package globally without sudo but still the error persists.

I was wondering if there is a way to run all node commands as root user or somehow bypass the error.



via Bharat Soni

No comments:

Post a Comment