WebStorm Node.js debug with Docker - node.js

I'm trying to debug a Node.js script with WebStorm 2019.3 and Docker as a remote Node interpreter. So far I can start the script, debug it, but any changes done on local do not trigger a nodemon restart of the script inside the Docker container (files inside the container ARE actually changing, I've checked).
Any ideas? I'll attach the WebStorm run config.
I think there is something wrong about the way that I'm using nodemon when starting the script, but I have no idea how to fix it for WebStorm config.

Looks like you might need to enable legacyWatch.
According to the documentation:
In some networked environments (such as a container running nodemon reading across a mounted drive), you will need to use the legacyWatch: true which enables Chokidar's polling.
Via the CLI, use either --legacy-watch or -L for short: nodemon -L

Related

Locally installed executable dependencies fail using npm run/yarn run on Windows

I am using a Windows machine as my primary development machine. I usually use WSL for most of my projects, but one of my electron projects requires me to work in the Windows environment. So I've set up node, npm, yarn & git on the Windows side as well. But I am having troubles with locally installed dependencies.
Say, I have added concurrently to my project using yarn add concurrently. Now if I do yarn run concurrently, it fails saying that 'C:\Users\Praneet\Projects\my-project\node_modules\.bin\concurrently' is not recognized as an internal or external command. But if I do yarn run concurrently.cmd, it works. Same thing happens for scripts in package.json. I have to change react-scripts start to react-scripts.cmd start to make it work. But I don't want to do this for every script, because there are other devs working with me who use MacOS. I will be really grateful if someone could help me with this.
Have you tried running these commands using Windows Powershell? It might work.
Another option (the one I use when I must code in a Windows environment) is to use Git Bash: https://gitforwindows.org/
It simulates a BASH for Windows, so the commands you are trying to run should work without the .cmd.

React create app hot reload is not always working on linux

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.

How to run a node.js file as a background process on a Windows server?

I was creating a node.js project and uploaded it to my Windows server to provide an API service for mobile application.
When I open command prompt and type
node app.js
It runs correctly, but when I close the command prompt my node.js server stopped running.
How to make it still running when I close the commend prompt?
For example on Ubuntu I use the command
nohup
How can I do this on Windows?
You can make the process run in background using pm2
pm2 start app.js --watch
This will start the process and will also look for changes in the file.
More about watch flag
Nodemon #ftw. On Windows, Forever doesn't really watch files so much as casually observe them, while pm2 restarts the server every time you load a page.
Each of these tools works similarly, and each installs just as in the accepted answer. E.g.:
npm install nodemon -g
This installs nodemon globally, and to use you can simply navigate to your project folder and enter:
nodemon
(Assuming your project has an app.js file). You can add the -w switch, just as in Forever and pm2, however if you're just wanting to watch all files, you can omit that. To run nodemon in the background with Forever, you would run this:
forever nodemon --exitcrash
Nodemon is good for development, especially on Windows, while Forever and pm2 are more for production, on Linux.
Here is a simpler answer that cuts right to the chase without any added libraries or overhead like in the other two answers described above. To run your Node.js application as a windowless startup program in the background (this would be analogous to "nohup" in Linux), modify this template to suit and copy it into a .VBS script file. Then copy that file to your Start Menu startup folder (for all users, that would be C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup) and it will automatically run. The techniques you are using here in Visual Basic are (1) preparing to run the Node.js application by first changing the working directory of the shell object and (2) informing the shell to run the Node.js application in a hidden window by adding a “, 0” immediately after the run function:
Dim objShell
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.CurrentDirectory = "C:\path-to-your-node-js-app\"
objShell.Run("""node"" your-app.js"), 0
Set objShell = Nothing
References:
https://keestalkstech.com/2016/07/start-nodejs-app-windowless-windows/
https://devblogs.microsoft.com/scripting/how-can-i-change-the-working-folder-of-a-script/
No, you can't.
Even if you make a GUI program you'll need to run it via console.
And as soon as you close the command prompt. Your service would be stopped/ terminated that moment only. Because node creates a server itself while running : http.createServer().listen(port) or app.listen(port). So this this makes it independent in nature.
So, as soon as you close the command prompt on which server was running all the services would stop at that moment.

Docker : How to run grunt-open?

I have been using a grunt-open package for open my browser when i build my project. Recently I begin to use docker and this works perfectly, But the grunt-open task don't works anymore.
Exist some way to create a bridge between my docker and my local machine for opens my browser using grunt-open?
There is no way to open an external browser if you are running or building your project inside a docker container. The idea of using docker is to have all the tools you need inside the container.
You can use an gui less browser like PhantomJS and run grunt-open task inside the docker container.
There is no "automatic" way - you would need to have some kind of listener on your local machine. So you can't really use grunt-open from the container but there are any number of ways you could have the grunt task in the container send a call to your local machine which could use grunt-open (or npm-open which it's a wrapper for, or opn which npm-open is a wrapper for) -- or a simple shell script.

How to keep MEAN (stack) running?

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.

Resources