How to set cross env variables to start tests before commit? - javascript

I have configured to run tests before commit. But it only works in Ubuntu.
Here what I have now:
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"test:all": "CI=true react-scripts test"
},
"husky": {
"hooks": {
"pre-commit": "npm run test:all"
}
},
How to set cross env variables to run them in any operating system?

Use the cross-env package:
"test:all": "cross-env CI=true react-scripts test"

have you tried with a .env.test file? See React docs for info on .env in create-react-app create-react-app adding custom variables

Related

'.' is not recognized as a internal command (React Package.Json)

I cannot deploy my react project on github pages because the change that I did in my package json shows the error below:
I just added inside of Scripts: publicar
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"publicar": "npm run build && ./node_modules/bin/gh-pages -d build"
},
I'm using '.' because i need to access the node_modules that is located in a different place that my package json.
When I execute the command 'npm run publicar' shows me this error:
'.' is not recognized as a internal command
When I execute the 'npm run publicar' should not show me errors and should show 'published'
npm runs with node_modules/bin in its PATH. You should just be able to use gh-pages the same way you use react-scripts.
I would also advise using a pre-script instead of chaining commands
"prepublicar": "npm run build",
"publicar": "gh-pages -d build"

$ timeout <5> && react-scripts start && was unexpected at this time

"scripts": {
"build:css": "postcss src/styles/tailwind.css -o src/styles/app.css",
"watch:css": "postcss src/styles/tailwind.css -o src/styles/app.css --watch",
"react-scripts:start": "timeout <5> && react-scripts start",
"react-scripts:dist": "react-scripts build",
"start": "run-p watch:css react-scripts:start",
"build": "run-s build:css react-scripts:dist",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
This is how my scripts part in my package.json looks. I keep getting "$ timeout <5> && react-scripts start && was unexpected at this time". I tried using sleep instead of timeout but that is unrecognized as a command. I tried downloading Windows Resource Tools Kit but that download is now unavailable.
Any suggestions?
Something like this: timeout 5 /nobreak && react-scripts start
More info

prepending "REACT_APP_" for .env variable still returns undefined

My react app's environment variables still returns undefined after trying to prepend "REACT_APP_" in my variable. I'm using create-react-app, and I see a lot of the prepending of "REACT_APP_" as the common solution for this, but it doesn't work for me.
.env:
REACT_APP_UNSPLASH_KEY=my_api_key
REACT_APP_OWM_KEY=my_api_key
In my App.js:
const get_random_photo = () =>
`https://api.unsplash.com/photos/random/?orientation=landscape&w=1920&h=1080&query=${weather_query}&client_id=${process.env.REACT_APP_UNSPLASH_KEY}`;
const get_weather = () =>
`https://api.openweathermap.org/data/2.5/weather?q=${location}&units=metric&appid=${process.env.REACT_APP_OMW_KEY}`;
I also tried installing env-cmd, separating .env into a .env.production and .env.development, and editing scripts for package.json:
"scripts": {
"preinstall": "npx npm-force-resolutions",
"start:staging": "env-cmd -f .env react-scripts start",
"start:prod": "env-cmd -f .env react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
And still no luck.
If your .env files are named as .env.production and .env.development you are not using them in the package.json scripts.
It should be like env-cmd -f .env.development react-scripts start and your production build can be env-cmd -f .env.production react-scripts build

Connecting React frontend and Node.js backend at Heroku

I have deployed my simple React app to Heroku - https://projectslist.herokuapp.com It works just fine except for the server part which doesn't work at all. I've seen the logs and it says that my only server method - /sendmail - returns error 404. How can i fix this problem? I suppose that I should edit something in package.json file but I'm not sure what I should change.
Here's my github repo - https://github.com/VasVV/Projects-list
Try putting your server files in the root directory folder and everything else in a client folder within the root:
1.) Put your server.js into the root directory
2.) Add these commands to your root package.json...
"scripts": {
"client-install": "npm install --prefix client",
"start": "node server.js",
"server": "nodemon server.js",
"client": "npm start --prefix client",
"dev": "concurrently \"npm run server\" \"npm run client\"",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
},
"proxy": "http://localhost:4242/"
3.) Create a client folder and put your src and public folders in it.
4.) Add these commands to your client folders package.json...
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
Your file structure from root should look like this:
Root
client
Public
Src
package.json
server.js
package.json
5.) Move all your server-related stuff into the root folder, that way, you don't have to cd into any nested folders.
Add a Procfile to your root folder that says something like:
web: node server/index.js -port 8000
And a small addition to the scripts part of your package.json that tells Heroku that once it's fetched all the dependencies for your project it should build it, although that may not be necessary now.
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"heroku-postbuild": "npm run build"
},

How to get npm run script run the build:style script when CSS files are updated?

In my package.json I have run scripts that look like:
"scripts": {
"build:style":"tailwind build src/styles/index.css -o src/styles/tailwind.css",
"start": "npm run build:style && react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
I'm using this tailwindcss framework for CSS, and it uses PostCSS to update the CSS.
Currently if I update my CSS npm doesn't run the build, so my CSS changes are not visible until I stop and re-rerun:
npm run start
Is there a way for npm to watch the CSS files, and when changed also run the build:style command?
Unfortunately, I don't believe Tailwinds has a 'watch' feature. I believe one workaround would be to install the watch package.
npm install watch
You have a lot of scripts going on and without knowing how you've integrated PostCSS and the likes, it's tough to know exactly what to do. One option would be to create a new script that takes advantage of the new watch node package.
"scripts": {
"build:style":"tailwind build src/styles/index.css -o src/styles/tailwind.css",
"start": "npm run build:style && react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"watch": "watch 'npm run start'"
},
If that doesn't work, check out the blog below. It uses the same methodology, but may have insight that could help you figure it out!
https://flaviocopes.com/tailwind-setup/

Categories

Resources