I'm new to node and have many things unclear.
Like, for php, I just need a index.php file on server's root dir and it can work by itself.
However, for a node.js file, we need to "node" command it in terminal right?
So what if we close that terminal? How can I keep it running to accept my requests?
You are correct in saying that the 'node' command will start a node process with whatever script you supply to it.
As far as keeping it running, there are several ways to do it. There are plenty of CLI libraries that will help you. For example, this one is called Forever
If you are using linux, you can simply run the node process as a background task:
node server.js &
To run node without terminal, you might want to check out one of these modules depending on your platform:
node-mac
node-windows
node-linux
Related
I have a web app project, and it uses Mongo, so one of my NPM scripts is "start-mongo-dev": "mongod", for spinning up the Mongo daemon during development.
I used to use OSX, but just got a new computer and run Linux Ubuntu. Now the command to begin the daemon is sudo service mongod start, so it would seem I should change the NPM script to that.
But that is a red flag. What if I go back to the old computer at all? What if I collaborate on this project with someone who uses OSX?
In short, how is one expected to handle developing with multiple OS's?
Ian's solution may be better but one alternative would be to create a bash script inside your project that detects the OS and runs the appropriate command. You can find more info on how to do that here How to detect the OS from a Bash script?
In some packages, I see divisions like this indicated with a :. For example:
"scripts": {
"start-mongo-dev:osx": "mongod",
"start-mongo-dev:ubuntu": "sudo service mongod start"
}
I am not aware of a canonical solution, but that doesn't mean there isn't one. I happen to like this one, though. #opinion
I created a react application using create-react-app boilerplate, which seems to be very popular, hot reload some times updates when any of the files changes and some times not, seems like there is a minimum duration or something like that, I'm using Ubuntu, node version 7.0, the script in package.json is npm:'react-script start' what I am missing ?
When npm start doesn’t detect changes, below are the common troubleshooting steps provided in the create-react-app documentation - link.
While an app is running with npm start and updating code in the editor should possibly refresh the broswer with the updated code.
If this doesn’t happen, try one of the following workarounds:
If the project files are directly synced to your local system from a cloud storage like Dropbox or Google Drive and you're trying to run the app in them directly, try moving it out.
Due to Webpack bug, you may need to restart the watcher. If the watcher doesn’t detect the index.js and you’re referencing it by the folder name.
Safe write feature in editors like Vim and IntelliJ currently breaks the watcher. You will need to disable it.
Due to Webpack watcher bug, projects with path contains parentheses causes issue, try moving the project to a path without them. .
To allow more watchers in Linux and macOS, you might need to tweak system settings.
If the project runs inside a virtual machine such as (a Vagrant provisioned) VirtualBox, create an .env file in your project directory if it doesn’t exist, and add CHOKIDAR_USEPOLLING=true to it. This ensures that the next time you run npm start, the watcher uses the polling mode, as necessary inside a VM.
Could try increasing max_users_watches- link
More references:
Issue Tracker 1
Troubleshooting webpack
Issue Tracker 2 - Webpack polling
Try these:
Turn off safe write in your IDE
Increase max_user_watches
Your path should not have parentheses
as last resort try to use this as your env variable:
CHOKIDAR_USEPOLLING=true npm start
Sources:
https://github.com/facebookincubator/create-react-app/issues/659
https://github.com/facebookincubator/create-react-app/issues/1049#issuecomment-261731734
I was able to make this work using:
sudo npm start
run this command
sudo -i
echo 1048576 > /proc/sys/fs/inotify/max_user_watches
exit
try deleting the node_modules folder and reinstall
by using cmd npm install
worked for me ( ubuntu 18.04.3 LTS )
In unbuntu, i basically kill all the process running on port (for react app default is :3000).
List all the process running on port 3000.
lsof -i :3000
This command will show something like this.
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 7429 yipl 19u IPv4 1081760 0t0 TCP localhost:3000->localhost:35762 (ESTABLISHED)
chrome 26448 yipl 177u IPv4 1080082 0t0 TCP localhost:35762->localhost:3000 (ESTABLISHED)
Now kill the process by PID.
kill -9 7429
kill -9 26488
Start your react app again.
Apparently hot module reloading only works out-of-the-box if you eject your app.
But if you haven't ejected your app, then you can follow these instructions to get it working.
Find the ReactDOM.render(...) at the top of your app, and add these lines below it:
ReactDOM.render(...);
if (module.hot) {
module.hot.accept('./App', () => {
// --- Copy-paste your usual ReactDOM.render(...) call here: --- //
ReactDOM.render(...);
});
}
The instructions linked above also show how to hot reload things outside of the component tree, such as redux reducers.
One additional case I just experience is when using multiple version of nodejs with NVM in parallel. Basically, I have two terminal windows, one run node 10.x, other on node 9.x, and Webpack watcher stops seeing change.
The solution is to bring both to the same node version
sudo npm start
i dont know more about linux, but it is probably due to firewall settings, as i have done the same in windows and there to start react dev server we need to give the access through firewall, so to do the same here we need to give it the admin permision(sudo in case of a ubuntu linux distribution).
If you start your app with a bunch of errors it seems to stop watching your files and thus will not detect your fixes to the broken files. It seems as if you need to initiate "start" with not too many/severe errors for the file watcher to work.
I want to write a node script that detects if another node script is running.
My question is (all running on node in Windows 7), within a node script, how do I detect if another node script is running?
I'm new to node and I couldn't find an answer that matched my question exactly.
I'd be tempted to use nodemon...
Once installed (with npm install -g nodemon), instead of starting your server with say node app.js - start it with nodemon app.js.
Nodemon will listen for any changes to your files, and restart your server automatically for you. Perfect for development!
If this, for some reason, is not an option though, you could use something like the taskkill command to kill all the node processes with something like taskkill /F /IM node.exe
I'm not a Windows Guru though, so there's will likely be a better way of doing this with something like a batch script or windows service (rather than just killing ALL node apps)
Perhaps someone on StackOverflow with more Windows experience can help out here.
Good luck!
I'm using grunt to run the MEAN project on Ubuntu, but when I close the putty (I use putty to connect Ubuntu server from my PC), it would close the program too.
My question is how can I keep MEAN running?
Update: nohub grunt & stops after I close putty
There are various node based process managers which can serve your task. My favorite is pm2 (http://pm2.keymetrics.io/)
Package managers allow your program to keep running even in case of hiccups. They can watch your project directories for any changes that you might push to them and restart servers based on those changes.
Other favorite is forever (https://www.npmjs.com/package/forever).
you need to run the command in background and I would also recommend to use nohup so:
nohup grunt &
should do the trick.
https://en.wikipedia.org/wiki/Nohup
NODE_ENV=staging nohup node appStag.js &
You can use the above command to run node server
and you can get the above environment using process.env.NODE_ENV
I found a npm package called forever is a good solution, I use forever to run the program right now; and it works perfect with putty.
Maybe someone can tell me what I missed installing nodeJS to jenkins, because when I want to execute shell script npm update I get error npm: not found.
I installed jenkins nodejs plugin then added installer:
After I created job with prams:
What I need to do more, to have accessible npm in any jenkins job?
It works for me. The only difference is that where you have a text box that reads "0.12.7", I have a popup menu that reads "Node.js 0.12.7". Maybe something has gone wrong and you should delete the installer and add it back?
Windows users, did you restart?
Do keep in mind that you need to restart active processes after installing npm.
During installation the folder of your npm.cmd is added to the PATH variable. However, active applications don't refresh their PATH variable automatically. That also holds for your active Jenkins service (and its child processes). They are still running with an old version of your PATH variable.
In other words, you need to restart the Jenkins service (or restart your entire system). Once restarted, Jenkins will have an updated PATH variable, and it will be able to find the npm.cmd processes and others.
What's wrong with using absolute paths?
Using the full path is not a solid solution. A package.json file can contain references to other cli processes (e.g. node, ng, react-scripts, npm-run-all, ...). You will probably have similar issues there. There is no way to keep up and reference all of them with full paths.