Nestjs/swagger open browser automatically like in REACT - browser

I'd like my Nestjs/swagger application to start up as soon as bootstrap is finished
Initially I thought of using the callback of
async function bootstrap(): Promise<void> {
console.clear();
console.log("Starting and validating");
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
cors: true,
});
await app.listen(PORT, () => someOpenBrowserFuncion("/docs")`));
}
bootstrap();
But I didn't find anything like that, so I thought
When we start a REACT app, as soon as it is compiled, it opens the default browser automatically.
and
This option can be disabled with the following command:
"scripts": {
"start": "env BROWSER=none react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
Is there a similar function in the Nestjs/swagger framework?
"scripts": {
"build": "nest build",
"dev": "nest start --watch",
"start": "env BROWSER=true nest start",
"production": "node dist/main",
},
Or some configuration to launch browser on certain endpoint?

You can install a npm package cross-env : npm install cross-env
And update this command in the package.json file under 'scripts';
"start": "cross-env BROWSER='chrome' nest start"
BROWSER is an environment variable, and you can use the cross-env package to properly handle it.
Linux:
BROWSER='google-chrome-stable'
Windows:
BROWSER='chrome'
OS X:
BROWSER='google chrome'
If these don't work, you can update the script:
Windows:
"start": "start http://localhost:3000 & nest start"
Mac:
"start": "open http://localhost:3000 && nest start"
Linux:
"start": "xdg-open http://localhost:3000 && nest start"
You can change your own port number.

Related

how to change the path in dotenv with various script commands (in package.json) such as : start , test , etc

well my problem is when I want to switch my script command in package.json like from "start" to "test" for running my Jest test which its commands like :
"scripts": {
"start": "nodemon express/***",
"serve": "node express/***",
"dev": "node express/***",
"test": "jest --watch"
},
and I call dotenv in my project like this
require("dotenv").config({
path: "express/config/.env",
});
The code above, help my to using my environment file like .env
but the problem is that when I want to test my project and I want to switch my script command (in package.json) from like "start" to "test" and change the main path of dotenv environment to something like test.env
You could pass the environment type as an environment variable into your program like so. Note: you will need to use cross-env if you require multi-platform support.
Unix version:
"scripts": {
"start": "NODE_ENV=production nodemon express/***",
"serve": "NODE_ENV=production node express/***",
"dev": "NODE_ENV=dev node express/***",
"test": "NODE_ENV=test jest --watch"
}
cross-env version:
"scripts": {
"start": "cross-env NODE_ENV=production nodemon express/***",
"serve": "cross-env NODE_ENV=production node express/***",
"dev": "cross-env NODE_ENV=dev node express/***",
"test": "cross-env NODE_ENV=test jest --watch"
}
And then access them using the normal method of process.env.NODE_ENV
const envVariablePaths = {
"production": "/path/here",
"dev": "path/here",
"test": "path/here",
}
require("dotenv").config({
path: envVariablePaths[process.env.NODE_ENV],
})
More documentation can be found here

How to set env variables to react scripts

I'm building a project that has a backend, and a frontend.
I had the idea that'd be cool to have both folders in the same directory with another npm script that runs them.
The problem is that I'm unable to set the port in one of the two packages, that being the frontend.
In my main package.json I have this:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start-app": "cross-env SERVER_PORT=8080 npm start --prefix ./frontend",
"start-server": "cross-env SERVER_PORT=8080 npm start --prefix ./backend"
},
And it works for the backend, but doesn't for the frontend.
The frontend has the base configuration.
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
The project runs, but when I look for the process.env, there's no trace of the SERVER_PORT in the FrontEnd but it exists in the BackEnd.
(Backend scripts are the default for a new npm package).
"scripts": {
"start": "node main.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
CRA uses PORT to define the port variable. Run it like:
"start": “PORT=8000 react-scripts start"
Additionally since CRA is essentially a static site it doesn't have in itself a concept of env variables - these are NodeJS specific. A few such as PORT and NDOE_ENV are defined for you and used in the dev server. If you wanted to use custom env vars within your React components you can make them available by defining the with the prefix: REACT_APP_ such as REACT_APP_SITENAME for example.
Check out the docs here: https://create-react-app.dev/docs/adding-custom-environment-variables/

React command on yarn start and also init server with same command

I'm trying to attach another command to yarn start. I'm not sure if it is possible but when I run command yarn start I want my react app to start and I also want to fire up my server at the same time.
What I do know is use 2 terminals one with react app directory and call on yarn start
C:\Users\ivanr\Documents\GitHub\bees\business-scheduler>
and one with server directory (which is inside react app directory) and call on node src/index.js
C:\Users\ivanr\Documents\GitHub\bees\business-scheduler\server>
"scripts": {
"start": "react-scripts start", // is it possible that I can say in server directory run node src/index.js here?
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
You can use concurrently
First install it
$ npm install concurrently
Then use it in your command
"scripts": {
"start": "concurrently \"yarn start-client\" \"yarn start-server\"",
"start-client": "react-scripts start",
"start-server": "cd .\server && node src/index.js"
}
You can use npm-run-all
"scripts": {
"clean": "rimraf dist",
"lint": "eslint src",
"build": "babel src -o lib"
}
npm-run-all clean lint build

npm stuck on input loop after updating from react-scripts-ts to react-scripts

Not really any code for this, essentially used this blog as reference: https://vincenttunru.com/migrate-create-react-app-typescript-to-create-react-app/
basically, the scripts look like this
"scripts": {
"watch": "npm-watch",
"build-css": "lessc src/main.less src/index.css",
"start-js": "react-scripts start",
"start": "npm-run-all -p watch start-js",
"build": "npm run build-css && react-scripts build",
"test": "react-scripts test --env=jsdom",
"test:staged": "echo 'write some tests'",
"eject": "react-scripts eject"
}
it runs fine up until "npm start" where everything is fine up until this moment:
[nodemon] clean exit - waiting for changes before restart
? We're unable to detect target browsers.
Would you like to add the defaults to your package.json? (Y/n) n
Unrecognized input: n
Unrecognized input:
where it's this weird loop because input isn't parsing input properly or something, as in I can't even exit because it's detected as an input, so the only way to stop is to shut down the terminal
In the package.json
"browserslist": [
"defaults"
]

Nodejs Express - how to set max-old-space-size in package.json?

I was getting Javascript heap out of memory error.
So I made this change to my package.json file.
"scripts": {
"start": "node --max-old-space-size=4096 app.js && node ./bin/www"
},
which used to be
"scripts": {
"start": "node ./bin/www",
},
Now if I run npm start, the server is not up and running. If I try to access any page, I get unable to connect to server error message.
What am I doing wrong?
Thanks.
This works for you:
"scripts": {
"start": "node ./bin/www",
},
You need to pass the --max-old-space-size=4096 param like this:
"scripts": {
"start": "node --max-old-space-size=4096 ./bin/www",
},

Resources