4

I'm working on an updater and I wish there is some way for the updater to update itself (remove and copy the new file from which the script is run).

  • Is it possible?
  • Is it a good practice?
  • Have you got an advice for such a situation?
3
  • 1
    There's certainly nothing stopping you from deleting the source file, once it has been required in a module it wont be read from disk again (unless you invalidate the cache, but that is a hack). Is it bad practice? In general, probably, but it sounds reasonable for your use case. Commented Jun 12, 2019 at 21:38
  • Self updating modules, especially those in the context of a package manager like npm, rub me the wrong way because they violate predictable state. If I install [email protected], I want it be that and always be that until I purposefully install the next version. Commented Jun 12, 2019 at 21:40
  • @zero298 this is not a self updating module, but just a command to update the main application. However thanks for pointing this out :) Commented Jun 13, 2019 at 8:01

2 Answers 2

4
  • Is it possible ? Yes...
  • Is it a good practice ? It depends, if you are updating an application, it could be a good approach...
  • Have you got an advice for such a situation ? Not actually because if you need a new version of your app you must update the files but I would suggest a parallel app who is in charge of updating the main app...

Example:

var fs = require('fs'); console.log("I'm about to delete myself..."); console.log('clonning myself...'); fs.copyFileSync('./selfDelete.js', './selfDelete_bkp.js'); console.log('removing myself...'); fs.unlinkSync('./selfDelete.js'); console.log('new version of me...'); fs.renameSync('./selfDelete_bkp.js', './selfDelete.js'); 
Sign up to request clarification or add additional context in comments.

Comments

1

Not a good practice.

And you cannot update while you running. What I can suggest is you can create a new file somewhere with updates and then replace the current file with the new file from a scheduled task which runs sometime later. Not sure that answered your question but this is what I can think of.

2 Comments

Thank you for the effort but can you provide more explanations on why it s a bad practice ? I don't have access to system files or cannot be sudo thus making a scheduled task is impossible for me actually. Thanks :)
If you working on a app which let user create their own js files an some environment and let them run instantly, then you might not have much of a choice. Otherwise I would suggest to use a DB for manage the app customisations else the changes could crash your app. I might be wrong by the way :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.