How to declare an "install script" in a Node.js application? - node.js

I'm new to Node.js and I'm building a little application that connects to a MySQL database.
I wrote a script that initialize the database (well, for now it only creates a table), I invoke it with the command node storage/init_storage.js and when the task is done, the script does a process.exit() to terminate itself.
It works but I'm pretty sure there is a cleaner way to do that? In the future I will have more tasks to do when I'm reinstalling the app (maybe create folders, run tests...), is there a right place to do those things? Maybe a way to do them when npm install is launched?
Thank you for your help.

Related

Do I have to stop the react native server everytime I want to install a new node module

React noobie here.
I have a question, I am working on a react native project, and I search for extensions I can add for my applications, whenever I want to install a new module, I have to stop the server and let the installation finish, then run the server again. This process takes too much time, is there a way I can circumvent that?
Yes it is good because the restart assures that all new files are added at the start of the app. I think it is a good thing even though it can be annoying but installing dependecies when the app is running would only make it harder to code imo.

Cannot find module 'nw.gui' - node webkit updater

I'm trying to use node webkit updater for my aplication,but when i'm trying to test is(using npm test) or start it(using nmp start) i'm receiving this error .Both commands are used from Nodejs command prompt .
The js file used is updaterClientABC.js and the error comes from this line: var gui = require('nw.gui');
This is my folder structure :
Can you please tell me what should i do ? I admit that i am a beginner i've never worked with node-webkit.Some advices will be very helpful :)
I noticed this thread:
node module 'nw.gui' not found
So yes, this kind of thing is due to how NW.js gets called (directly or from within a project). I've run into this problem as well. In this case, in the above, you call updaterClientABS.js directly from node.js. To run a node-webkit project you need to load it via the NW.js binary, which in turn requires node.js.
It's also possible, as was my problem with this, that I was trying to access the nw.gui within the Node context (vs browser context). My solution was to access it via the browser context (which has more global access, not just Node.js objects).

How to test NodeJs CLI javascript

I need to put some tests around a nodejs command line utilities\modules. No browser involved and I'm using a lot of the "fs" module to work the file system, so i'm not sure a browser based test mechanism would work (sandboxing).
any modules that would help here?
Check out Vorpal.js. This lets you create an interactive CLI in node, in which you can then run custom commands to test the various things you want to test.
Disclaimer: I am its author.

Start app on mac os directly after installation

I'd like to know if it is possible to create a dmg file which can be installed by Drag&Drop - and which starts directly after this installation without having to be called manually again.
And if this is possible, I'd like to know whether I can pass arguments to the (nodejs) process which will be started directly after the installation.
I managed to pass parameters to the process when I call it manually after the installation, but I want to run it directly.
Any help will be appreciated!
Thanks!
I solved it: Instead of creating an app and wrapping it into a dmg, I created an app and wrapped it into a pkg. For pkgs it is possible to use postinstall scripts, so I started the application through this script.

Can I make nodejs work after each modification without restarting server

I'm new to Node.js. For each modification in code, I have to restart server to make it work. I wonder if there are any ways to make it work without restarting server. Thanks.
Check out this npm plugin: https://npmjs.org/package/devreload
There is also a very cool NodeJS based framework called Meteor which does all of this automatically. It is very easy to learn, and does realtime code & content updates. I suggest looking into that as well.
Good luck.
I have not come across any which deploys server side scripts without restarting the server. There are node packages like Forever, Nodemon which listens to changes and deploys automatically.

Resources