does node have a place to put custom console methods like ~/.irbrc? - node.js

I was thinking it would be handy if I could reload! code in the Node console, something to the effect of:
reload('./path/to/my/file.js')
that would delete the code from the cache then load it again -- handy for exercising prototype code. Seemed like a natural function to place in node's equivalent of ~/.irbrc. But I can't find that. Does node have such a thing?

I don't know there's such a tool. But I believe what you need can be achieved by using nodemon.
Use sudo npm install nodemon -g to install it. Then launch your application by nodemon app.js instead of node app.js. Then it watches for changes to nearby .js files, and automatically restart when you save.

Related

Why server restart is needed in Node.Js for every change?

I am very new in Node.Js. I just started node.js basic tutorial. But when I change my code I have to restart the server all the times. But is there any way where no need to restart the server again and again.
Nodemon is the best solution for this.
Install nodemon like this "npm i nodemon"
Then restart your project with nodemon, "nodemon app"
You are good to go...
You can install node-supervisor to restart automatically your server when you change the code.
I'm not sure on the details of the compilation process. But I think it's correct to say that on app start, your source code is parsed into computer instructions represented in memory and executed. During runtime source code files are not re-parsed. And so changing the source code will have no effect on the running application. Unless the application re-parses a file prior to execution of the code in that file. Possibly a service worker... But I'm not sure and that would be an exception.
A good way of thinking of nodejs and javascript files (imo) is that the javascript files are configuration for nodejs. Which is a c++ app. So if the configuration changes you need to restart node to read the new configuration.
There are tools such as nodemon that will monitor the source code for file saves and trigger the node application to restart.
Check out Nodemon.
nodemon will watch the files in the directory in which nodemon was started, and if any files change, nodemon will automatically restart your node application.
nodemon does not require any changes to your code or method of development. nodemon simply wraps your node application and keeps an eye on any files that have changed. Remember that nodemon is a replacement wrapper for node, think of it as replacing the word "node" on the command line when you run your script.

How does NPM start an Angular and Typescript application?

Would it be possible to get an explanation of how npm start works?
I have an Angular 2 application written in Typescript. I enter npm start in the console and this both compiles and launches the application using Node's built in web server.
However what is the starting point for this? Which files does the npm start command read in order to compile and start the application? Also why do I use npm start and not e.g. node start? I understood that NPM was just the package manager for node itself.
What I understand so far:
I have a tsconfig.js file which tells the TypeScript compiler what to do.
I have a packages.json file which tells node which packages to download.
Where do the following files fit into this:
main.ts
app/app.module.ts - which I understand is the starting point for my application.
How do all of these components fit together to form the application?
npm start is just an alias for npm run start, which runs whatever command is in scripts.start in your package.json.
It neither knows nor cares about TypeScript, or Angular, or anything like that - it just executes whatever script it's given on the command line.

Sails.js - How to use forever and not start grunt automatically on server start?

I'm running into an issue using Sails.js framework (node.js).
It seems like it runs automatically grunt when the server starts. But I don't want that. In order to prevent this, it is possible to configure in a .sailsrc file that we don't want grunt to start. (http://www.sailsjs.org/documentation/concepts/assets/disabling-grunt)
But this specific configuration file is only taken into account when we run the server using sails lift command. It won't be used if we use node app.js.
Point is, I want to use forever with sails. But I don't know how to run forever and take into account the .sailsrc file so that Sails doesn't start grunt on its own.
I'm using BrowserSync and I need to startup things in a really specific order, it works fine when using sails lift and grunt separately, but I don't get any hot refresh that way.
I found a similar issue here, but it doesn't help me much. Error when use forever in sails
I assume you get the "Your .sailsrc file(s) will be ignored." error.
Running npm install rc --save in both the project root directory and node_modules/sails, and adding
"hooks": {
"grunt": false
}
to .sailsrc solved this issue for me.
Update: if you're using an older version of Sails, such as sails#0.10.5 you have to change the line "rc": "^0.5.5” to "rc": "^0.5.0" in node_modules/sails/package.json, remove rc and reinstall it with the older version of rc (which is used in the newer versions of Sails).
In the sails book from Manning:
Note: Grunt is also optional. If for some reason you enjoy doing manual repetitive tasks simply delete the Gruntfile.js from the root of your project. No more Grunt. When you restart Sails via sails lift warnings that the “Gruntfile could not be found” and that “no grunt tasks will be run” are displayed.
Give that a shot
Setting hooks.grunt = false keeps being valid for Sails v1.x

how can I debug a node app that is started via the command line (cli) like forever or supervisor?

I'm familiar with debugging my own node apps (usually with node-inspector). Today I'd like to debug someone else's program. I'm trying to track down an issue with supervisor. So naturally I just add a --debug (or debug-brk) to the command call, but it passes that to the code that it is supervising.
I've tried adding debugger lines to the js file for supervisor but that didn't work (probably because no debugger was attached at that time). There's a bit of a race here -- I need to start the debugger and attach it to the supervisor process after it starts but before it processes its arguments from the command line.
What I really want to do here is stop supervisor and debug it before it processes its command line arguments. How can I do this?
I had the same problem while developing my hexo blog. The documentation isn't all that complete yet so I find myself needing to reverse engineer at times.
The basic idea is that in Node.js even your cli apps are simply normal node apps that you are exposing to the OS command line interface. On Unix systems you are using this line:
#!/usr/bin/env node
To allow the environment to execute the script.
Many cli based node apps try to insist that you install them globally with the -g option.
npm install -g node-inspector
I personally prefer to have as much control of my development environment as I can get, so I prefer to break some conventions and check my node_modules in to source control along with installing everything I can locally by dropping the -g.
npm install node-inspector
Now you don't have to do this in order to make this work, I'm just describing this setup because it relates to your question. When I run node-inspector I can't simply use:
node-inspector
Instead I must explicitly invoke it from within my project. I do this by executing the symlink in my node_modules/.bin folder:
node_modules/.bin/node-inspector
Now I'm running node-inspector just like you.
Next all I need to do is start the cli process in debug and optionally pass params to it:
node --debug-brk node_modules/.bin/hexo generate
Note I am explicitly calling the symlink here and not simply:
node --debug-brk hexo generate
If I tried the line above I would get an error: "Error: Cannot find module".
I hope this helps.

How to edit and deploy code without restarting server?

I have node server which i run using forever. But each time if I edit my code I'll have to restart the server. I came across the module called hotnode which can perform live edits but will it have the same performance as the forever module or can I run my code using both the modules. I am confused. Any help wil be much helpful
Have a look at nodemon.
nodemon will watch the files in the directory that nodemon was started, and if they change, it will automatically restart your node application.
As an alternative to nodemon you can use node-supervisor.
I used to use nodemon, but for some reason it didn't detect code changes on my linux box, which supervisor did flawlessly.
The downside is that it doesn't (or at least didn't) give the colorful output nodemon gives.

Resources