What is the --watch and --debug option in nest start - javascript

I read following documentation described nest command.
https://docs.nestjs.com/cli/scripts
According to the document, following must be added to package.json
"build": "nest build",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
What are the --watch and --debug options?

According to the nestjs start docs the actual uses are as follows;
--watch
Run in watch mode (live-reload)
Alias -w
Source files which are saved with changes are automatically compiled without the need to manually run npm run start to trigger webpack compilation after every change.
For example, typescript files with changes (on save, or when using git) in src will be compiled to javascript files in dist (depending on your setup)
--debug
Run in debug mode (with --inspect flag)
Alias -d
The --debug flag actually runs the node process using the --inspect flag to allow native debugging using an IDE or otherwise. Once the node process is running, you can use an IDE to connect to to the node debug address and port (default 127.0.0.1:9229) and use breakpoints* to pause execution.
*However, do note that the above at present isn't completely accurate. IDEs usually need the --inspect-brk flag (for breakpoints) and it seems there is still a problem with the nestjs implementation.
Some IDEs (for example VS Code) can get around this with an auto-attach feature and it seems --debug is not even needed. Although very easy to set up, it is not as streamlined when developing multiple running node apps.

In general, --watch means the terminal will stay open and watch for any file changes and then reload the server. --debug means it will log more messages to the console (e.g. info or warnings), which can be helpful for debugging.

Related

webpack --watch gets killed while working (react), what does it mean and how to debug the problem?

I have a normal react setup, without CRA, which means I manually configured webpack and babel etc...
I currently have 2 scripts which run webpack :
package.json :
"scripts": {
"dev": "NODE_OPTIONS='--openssl-legacy-provider --max-old-space-size=8000' webpack --watch --config webpack.dev.js",
"prod": "NODE_OPTIONS='--openssl-legacy-provider' webpack --config webpack.prod.js"
},
when I run npm run prod, the issue does not happen and all the assets get compiled like normal. However, when i run npm run dev, after working for a while (less than an hour), the terminal kills webpack and shows the following:
I'm not sure what is happening here but I saw some recommendations to circumvent the issue by adding --max-old-space-size=8000 to the NODE_OPTIONS in the script.
However the issue persists. Any idea How to deal with it ?

Webpack build working on local but fails on server

I am trying to deploy an app that works on local but when I put it on the server I get this error that refers to a filter function on an array. Indicating that the compile ES5 JS is broken somehow.
I use the webpack build command and the output works on my local but when I am putting it on the server I get this on the browser:
`TypeError: s.filter is not a function vendor.bundle.9a44edfc.js:2:131513`
I don't understand why it would work on my local and not on the server, is the same node, npm and yarn version on both.
I run this command for build
"build": "webpack --config webpack/webpack.config.babel.js",
"dev": "webpack serve --env mode=dev --env isDevServer --env NODE_ENV=local --config webpack/webpack.config.babel.js"
It had nothing to do with webpack, I was getting some data by requesting a route to my express server and I used:
axios('api/projects')
Instead of
axios('/api/projects')
And just for that it failed to get the data, the funny thing is that on my computer I am using nginx 15 on mac, and on the server I am using nginx 14 with Ubuntu and on my computer it did not failed.

How to nest package.json variables?

I have two scripts prod and build as given below and I'm accessing them both as package.json vars for creating another script build_prod as follows:
"prod": "cross-env NODE_ENV=production BABEL_ENV=production",
"build": "webpack",
"build_prod": "npm run $npm_package_scripts_prod $npm_package_scripts_build"
Running npm run build_prod from the terminal works perfectly, as expected. But if I try to nest build_prod in another script, it gives an error - missing script.
ie, After changing build_prod
from npm run $npm_package_scripts_prod $npm_package_scripts_build
to $npm_package_scripts_prod $npm_package_scripts_build
and adding script: "build_final": "npm run $npm_package_scripts_build_prod --config prod.config.js"
and running npm run build_final from terminal.
I searched for some time about nested package.json vars but could not find any information, even in the official documentation of package.json vars.
Do npm support nested variables ? If so, what is the right way of creating nesting scripts ?

create-react-app: How do I "npm start" with a specific browser?

npm start
starts the react server on the default browser, which is Firefox for me. I like Firefox for browsing but prefer Chrome in web development for its developer tools. Is there a way to force "npm start" to start the server with Chrome, without changing my default browser to chrome? I am using Bash on Windows.
Edit: I used "create-react-app" to create my server and this adds a script to "packages.json" file for "npm start". The script starts the localhost server with the default browser. How do I modify the script added by "create-react-app" such that it starts with a different browser?
This is possible with the BROWSER environment variable.
You can also do it directly in the terminal:
BROWSER=chrome npm start
This is described in the Advanced Configuration docs:
By default, Create React App will open the default system browser, favoring Chrome on macOS. Specify a browser to override this behavior, or set it to none to disable it completely. If you need to customize the way the browser is launched, you can specify a node script instead. Any arguments passed to npm start will also be passed to this script, and the url where your app is served will be the last argument. Your script's file name must have the .js extension.
Also note that the browser names are different on different platforms:
The app name is platform dependent. Don't hard code it in reusable modules. For example, Chrome is google chrome on macOS, google-chrome on Linux and chrome on Windows.
As you have mentioned that you are using create-react-app for creating react app and you want chrome to open on hitting npm start. Set BROWSER variable in package.json present in your project in the following manner:
Replace:
"start": "react-scripts start"
With:
Linux:
"start": "BROWSER='google-chrome-stable' react-scripts start"
Windows:
"start": "BROWSER='chrome' react-scripts start"
OS X:
"start": "BROWSER='google chrome' react-scripts start"
Method by using .env file in the root of your NodeJS app.
BROWSER="firefox developer edition"
Using above technique, you may end up with error
'BROWSER' is not recognized as an internal or external command, operable program or batch file.
To over come this
Do an npm install of cross-env in your cloned repo:
npm install --save cross-env
Try to use this command in the package.json file
"start": "cross-env BROWSER=chrome react-scripts start"
BROWSER is an environment variable, and you can use the cross-env package to properly handle it.
I don't like to repeatedly create a new .env file or prepend npm start every time with an additional command. You can specify your browser of choice instead of none in your shell config file. Type in your terminal the following commands:
echo export BROWSER=none >> ~/.bashrc
source ~/.bashrc
At this point you can run npm start and be happy.
Simply add the env-cmd package as global
then create a .env file and write a variable with a specific Browsers path
after that add the env-cmd just in your start script
in the terminal
npm install -g env-cmd
in the .env file
BROWSER= "your browser path"
like => BROWSER= "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Edge"
in the package.json add the env-cmd
"scripts": {
"start": "env-cmd react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
that should work!
In Windows cmd, set env variable for desired browswer:
set BROWSER=chrome
Then just run npm start like normal
Change your default Browser setting in windows, if it does not work then open your visual studio code and change the script browser to:
"start": "BROWSER=chrome react-scripts start"
for Brave browser it is BROWSER=brave-browser npm start
If you want to change the default browser when you are running a npm start or yarn start, the simplest way to do that is edit your package.json file.
Many are not comfortable dealing with environment variables using the terminal.
This is what your scripts section should look like:
"scripts": {
"start": "BROWSER=none react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
In the above scenario it would not open any browser at all, you are free to choose your development browser and continue your work(I prefer this one). However, if you want a specific browser then you can replace BROWSER=none with any of the following:
BROWSER=firefox
BROWSER=google-chrome-stable
BROWSER=vivaldi
Suit yourself.
This is how I solved mine:
I opened the application on vsCode, then via the terminal I ran "BROWSER=Chrome npm start".
on windows, the easies way with create-react-app was to add BROWSER="C:\Program Files\Google\Chrome Dev\Application\chrome.exe"
to my .env.developmennt.local file in each CRA project. I use a different browser without dev extensions as the default one set in the system.
There is one package called set-default-browser https://www.npmjs.com/package/set-default-browser
just download package from there and add following code
var setDefaultBrowser = require('set-default-browser');
setDefaultBrowser("chrome");
Or you can just run this set-default-browser chrome
Thanks!
On Mac, this method:
"start": "BROWSER='firefox developer edition' react-scripts start"
works on 'react': '17.0.1' together with 'react-scripts': '4.0.1'
But it is not working on 'react': '18.0.1' together with 'react-scripts': '5.0.1'.
On 18.0.1 it continues to open in the default browser set on my computer. So I have sometimes reverted back to using the older React version but do not want to continue doing this as I do need the newer version in some cases and it's just easier to install the most recent version using create-react-app.
Any suggestions?
If you are using another browser like Brave, here is an example on how to modify the package.json file.
In Mac OS
"scripts": {
"start": "BROWSER='/Applications/Brave Browser.app' react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
To open in chrome we need to set it as a default browser.
Setting --> Default browser --> Make default -->
and choose chrome if any other browser is chosen.
It worked on windows 10.
Add script to your package.json file
"devserver": "live-server --browser=Chrome"
If you are a Windows user then go to Sitting -> Default apps -> Web browser then select your desired browser.If you are a Linux user then go to System-settings -> Details. (Note: In older versions of Ubuntu Details is called System Info)

JS unit testing: run tests on file changes (like nodemon)

I have two question on JS unit testing:
1) Is there some tool that allows to automaticaly run javascript unit tests when certain files are changed (like for example nodemon restarts node.js on js changes).
2) Is this strategy appropriate (efficient) way to run unit tests?
Thanks,
Alex
For those who are committed to using nodemon, nodemon -x "npm test" has worked for me.
A little explanation
nodemon --help says:
-x, --exec app ........... execute script with "app", ie. -x "python -v".
In our case npm test is set to run tests by configuring our package.json
For example:
"scripts": {
"test": "mocha"
},
When using jest, nodemon is not necessary. Simply set the test script command to jest --watchAll in package.json as follows:
"scripts": {
"test": "jest --watchAll"
}
Check out grunt build system and the watch task. You can setup grunt to watch for file changes and then run any tasks you want (test, lint, compile, etc...).
https://github.com/cowboy/grunt
Some of the ideas are covered in this tutorial. http://javascriptplayground.com/blog/2012/04/grunt-js-command-line-tutorial
Here's a snippet of my package.json:
"scripts": {
"develop": "nodemon ./src/server.js --watch src --watch __tests__ -x \"yarn run test\"",
"test": "mocha ./__tests__/**/*.js --timeout 10000" }
The develop script is my command line to run my express server (entry: ./src/server.js), watch the /src directory which has all my server/API code, watch the /__tests__ directory which as all my specs and lastly tells nodemon to execute the enclosed statement before each restart/run with -x \"yarn run test\"
yarn run test is no different than npm run test. I prefer yarn over npm so that part is up to you. What is important is the \" tags inside the JSON value... without it, it will fail since the argument will be tokenized incorrectly.
This setup allows me to trigger changes from either server/API code or writing/fixing specs and trigger full test run BEFORE restarting the server via nodemon.
Cheers!

Categories