Next.js : Refresh page after modifying a file, without rerunning 'npm run' - javascript

I've been building websites using a normal LAMP stack with javascript (and jQuery) for the frontend for quite a while. But I wanted to try using javascript for the backend as well. I'm just starting to learn next.js.
On the old way, if I have modified a php file, to see the effect I can just refresh the web browser. But I noticed that with next.js you can't see the change immediately. I have to stop the npm script, rerun the "npm run xxx" command, then refresh the browser. It's kind of time consuming.
Is there a way to automate this process?

#Rudi's answer is correct, and I'll add to that that you want to make sure the command you're ultimately running is next, not next start. The difference is that next is for development mode, whereas next start is for production mode. In production mode, it doesn't watch your files for changes.
Typically, you have these commands referenced in the scripts section of package.json:
{
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
}
}
And then the command you would actually type in and run would be npm run dev for local development or npm run build; npm run start for production mode.
If this doesn't hold for your usage, and you have to restart the server even in development mode, then there may be something wrong with your setup.

One common issue that causes this has to do with accidentally importing a component and making a minor typo with lowercase/uppercase naming conventions.
For example, you import a component named Navigation, as navigation.
This will still import Navigation, but the live reloading will be broken.

In development mode, Next.js by default will hotreload any changes, you don't even need to refresh the browser.
But if you add a custom server, it doesn't hotreload changes to that. You can use nodemon to watch and restart the server: https://github.com/remy/nodemon

Related

parcel always caches no matter

I'm trying to run the example from an npm package that uses parcel.
The example uses a url that makes an api call.
To run the example I do: npm test
Below are the 2 attempts that I made to stop the caching. I modify the index.js, kill the local server and restart and it caches no matter what. I'm just looking to run the example and make changes and see the results. It is obviously using the dist folder, but I keep deleting things, but the issue still persists. Any suggestions would be appreciated. Feel free to ask why would I ever want to do this.
package.json
"scripts": {
"test": "parcel example/index.html --no-cache",
"patch": "npm version patch --no-git-tag-version",
"minor": "npm version minor --no-git-tag-version",
"major": "npm version major --no-git-tag-version"
}
It seems that although --no-cache technically does disable Parcel's aggressive caching, in practice it requires a few more awkward steps to actually see any new changes you've made.
In addition to running parcel with the --no-cache option, you also need to close or completely kill the terminals running any current instances of the parcel local server, and also need to bypass the browser cache by doing a hard refresh (CtrlShiftR in Firefox).
The docs for the --no-cache flag say:
Caching can also be disabled using the --no-cache flag. Note that this only disables reading from the cache – a .parcel-cache folder will still be created.
So my hunch is that it's working as expected. In most contexts it's totally fine to allow parcel to create a .parcel-cache folder (although it's best practice to add this folder to .gitignore). Is there something about your context that makes this a problem?

node.js JavaScript file changes do not take effect in frontend

There is a test and a production environment installed on a unix server. I got SSH and FTP access. I changed some *.js files in the test directory but the changes do not take effect in the frontend. Unfortunately I don't know so much about node.js and unix server but I now how to code JavaScript.
So here is my question: How can I "refresh" the test environment only (without affecting the productive environment and vice versa) so my changes take effect?
I searched many websites, read lots of articles and nodejs beginner guides but I don't know how to deal with this system who has been implemented by someone else I can't ask anymore.
Any help or hint will be highly appreciated. Thanks in advance!
EDIT1: All local browser caches have been cleared (Chrome development console no cache active and console open - refresh)
EDIT2: JavaScript files are executed on the server as part of a node.js application
You have stated that you are running server-side scripts, so try restarting Node.js. If you still have access to the terminal you are running from, press CtrlC, but if you don't, run pkill node or killall node (depending on your system and preferences).
To automatically restart your server when a file changes in the future, use nodemon. To start using it, it is recommended to set up your command line in an npm script named start. To do this, add this to your package.json:
"devDependencies": {
"nodemon": "^2.0.20"
},
"scripts": {
"start": "nodemon index.js"
}

Github Pages and React App not working regardless of methods

I've been trying to add my React App project to Github Pages for a week. After all the prep work and everything that need to be done, I finally got it to show up, well kinda. It actually doesn't show anything, nor does it give me errors. Which leads me to believe that my script
<script src="../dist/bundle.js"></script>
is the culprit. However, the problem is I've never once used bundle.js. I wanted to learn how to use React without using create-react-app, and in doing so I seem to have gotten to far along in the game I'm not even sure how this is supposed to work.
Here is the Github Pages in it's current form
https://kevin6767.github.io/redux-api-opendota2/
EDIT: It now seems to be showing a 404. I'm not even sure why this is not working at this point. I've tried to many different methods.
I am not sure how you uploaded your React application to GitHub Pages, but, here, I will be mentioning the correct way to do it:
Step 1: Install gh-pages via the terminal, making sure you are in the correct file directory.
$ npm install gh-pages --save -dev (We are saving it as a dev dependency)
Step 2: Go to your package.json and add
"homepage": "<YOUR_GITHUB_USERNAME>.github.io/<REPO_NAME>," above the "name"
Step 3: In the "scripts" section of package.json, add
"predeploy": "npm run build", "deploy": "gh-pages -d build",
Step 4: Assuming that you have already committed and pushed your code to your Git Repo, now, in the terminal, use the command npm run deploy to see the changes in your GitHub Pages.
Also, make sure that in your source code, you have set the same route for the homepage as in the package.json.

Can webpack-dev-server watch for changes and write on filesystem for iisexpress?

A big ASP.NET MVC application serves on :80. A part of it that is written in vue. Currently we are running a manual "npm run build" after each change. It takes too long.
webpack-dev-server seems as the perfect solution. It watches for change and caches in memory! But it will conflict with iisexpress. There are no easy solutions making them work together.
I think I have a better idea:
When starting to work "npm run start" setups webpack-dev-server for the vue parts from "./src/vue/". Serving all of the "localhost:8080/dist/bundle.[chunk].js".
When changes are made in any of the files in "./src/vue/" on save "npm run refresh" is being ran. A small script that requests all chunks "localhost:8080/dist/.[chunk].js" and places in "./dist/".
Developer F5s our ASP.NET MVC app and iisexpress serves the updated "./dist/".
My question is how do I request all of the chunks easily both for .js and .css files from webpack-dev-server?
I could have to write out the output from the first "npm run start" into a config file which is then read by "npm run refresh". Is this even reasonable? I hoped webpack-dev-server could write to file system itself.
Hot-module-reloading would be nice, but the ASP app is a mess of iframes. Currently just trying make our build process a little faster.
Thanks
If anybody finds this thread:
The answer was to use webpack --watch! Does exactly what I described and without the mess.
https://webpack.js.org/configuration/watch/

Development server of create-react-app does not auto refresh

I am following a tutorial on React using create-react-app.
The application is created by create-react-app v1.3.0
create-react-app my-app
The dev server is run by
npm start
After changing the code several times, the browser is not updated live / hot reload with the changes. Refreshing the browser does not help. Only stopping the dev server and starting it over again capture the new changes to the code.
I was reading a lot of unnecesary stuff.
I am using React 17.
And my problem was that the page just add new components but the browser was not refreshing the page.
If your terminal is Compiling... and then you don't see changes on the browser, you should try adding a .env file in the root path of the project and add FAST_REFRESH=false.
Hot refresh was replaced by Fast refresh by default.
Have you seen the “Troubleshooting” section of the User Guide?
It describes a few common causes of this problem:
When you save a file while npm start is running, the browser should refresh with the updated code.
If this doesn’t happen, try one of the following workarounds:
If your project is in a Dropbox folder, try moving it out.
If the watcher doesn’t see a file called index.js and you’re referencing it by the folder name, you need to restart the watcher due to a Webpack bug.
Some editors like Vim and IntelliJ have a “safe write” feature that currently breaks the watcher. You will need to disable it. Follow the instructions in “Disabling swap files creation in vim”.
If your project path contains parentheses, try moving the project to a path without them. This is caused by a Webpack watcher bug.
On Linux and macOS, you might need to tweak system settings to allow more watchers.
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.
If none of these solutions help please leave a comment in this thread.
I hope this helps!
Adding a .env file in the base path of the project and inside add FAST_REFRESH=false.
This disables fast refresh and returns to hot reload.
If you don't want to add a .env file to your base path you can choose these options:
"start": "FAST_REFRESH=false react-scripts start", in the package.json.
FAST_REFRESH=false npm run start, on the command line.
FAST_REFRESH=false, exporting it as an environment variable.
React 17
React-scrits 4
Have you tried npm start with super user permissions? I had the issue with my project and I solved it like this.
$sudo bash
#npm start
In WSL2 work for me, "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."
Or just run:
$ CHOKIDAR_USEPOLLING=true npm start
Spent few hours fixing this:
1 . Create a file .env (beside package.json file) add below contents:
File: .env
FAST_REFRESH=false
2 . Now, stop & start the server
(Ctrl + C to start, if in CMD, on Windows OS)
npm start
4 . Now, change some text in the App.js
File: App.js
from "Learn React"
to "Learn React And it's working"
NOTE:
1 . Server restart is important.
2 . Refresh browser tab if you dont see changes.
In my case, it was there are not enough number of file watchers. I have to change the configurations manually.
See active file watchers limit using below command on terminal.
cat /proc/sys/fs/inotify/max_user_watches
Add below line to the /etc/sysctl.conf file.
fs.inotify.max_user_watches = 524288
Apply changes using the command below.
sudo sysctl -p
I had this problem while running npm within WSL. I had the project folder in my windows Desktop folder from which npm cannot recompile automatically in WSL.
After moving the project folder to user home directory of WSL solved the issue.
just create .env file in the root of your app
and add the following to it
.env
FAST_REFRESH=false
On win10 in WSL2, I had to create the .env in the root folder, and include both
FAST_REFRESH = false
CHOKIDAR_USEPOLLING=true
My setup doesn't include any virtual machine (unless WSL2 is considered a VM?). Nevertheless the .env with the above two entries got it up and running.
Find your index.js and change something in this file, for example add a white space, then save.
It should show "compiling..." in your console.
Then you can modify other files and react will refresh on save.
It seems that npm is looking for changes in the index.js at the very first time, if you refactor your folder structure the index.js could be missed. Force an update in index.js file get the problem solved.
At least this has worked to me
For users of WSL2, be mindful that if your project is in your Windows System(ie C: or D:) then it won't work.
Solution 1:
access these files through the WSL share, \wsl$\DISTRO_NAME from Windows.
Solutions 2:
Inside your package.json
find
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
and replace it with
"scripts": {
"start": "CHOKIDAR_USEPOLLING=true react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
Credits to https://github.com/facebook/create-react-app/issues/10253
As of react-scripts version 5.x.x CHOKIDAR_USEPOLLING=true no longer works in a .env file. You now need to use WATCHPACK_POLLING=true in the .env file in the root of your project. Per this thread.
If you are using visual studio code, you can create a new file and save it as .env. Inside, .env write FAST_REFRESH=false and save. Then, run npm start and this worked for me.
Edit: This might not be a recommended solution. The solution worked for Docker.
If using docker with a volume mount, you need to add an .env file in the src folder with the command CHOKIDAR_USEPOLLING=true in it. However, for me this threw an error
/app/src/App.js: Cannot find module '#babel/parser'
. To resolve this new error, changing the "react-scripts": "3.4.3" to "react-scripts": "3.4.0" in the package.json file worked. So you depending on your situation you may need to add the .env file and also change the react-scripts version.
Note: To put a little more context, I was working with docker and the react app files were mounted as a volume in the docker image (so that making changes in the app are directly reflected in the app without rebuilding a docker image). The above solution is based on other solutions posted in the community for docker where people had suggested changing the react scripts version. I don't think this should be a recommended solution. However, since I was doing a tutorial series I wanted to save time and focus on other things.
In case people come here looking for a better solution, my issue was resolved by moving my files inside the WSL2 filesystem. Hot reloading worked straight away with no need to add an .env file.
If you are running your app behind a reverse proxy / nginx (e.g. to enable https locally) you also need to enable websockets so it can detect the refresh:
location /sockjs-node {
proxy_pass http://dockerhost:5000/sockjs-node;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
In package.json, use "react-scripts": "3.4.4"
Delete package-lock.json
Run "rm -rf node_modules && npm install"
npm start
I was confused about how to create a .env file and even when i did it still didn't work. I was able to fix the issue by running this command on my terminal.
npm run start -FAST_REFRESH=false
Hopes it helps someone.
I ended up here seeking answers to a similar issue. Although mine was specific to one page that wasn't hot reloading. I do want to point out this is using Nextjs and it was a page under /pages named subscribe.js (/pages/subscribe.js)
It was very frustrating and admittedly took a while to figure out.
A lot of technical answers here, but interestingly, my issue was that my functional component did not start with a capital letter.
import React from 'react'
export default function subscribe() {
return (
<div>
</div>
)
}
Where the function name needed to start with a capital like so
import React from 'react'
export default function Subscribe() {
return (
<div>
</div>
)
}
As they say, you learn something new every day. In this game it's more like 10 things, the trouble is remembering what you learn ;)
push your commited changes to the branch and then delete the local repo folder then clone the repo again and run npm install or yarn install whichever you prefer.
this workaround solved my issue
Try using this command
echo fs.inotify.max_user_watches=524288
sudo tee -a /etc/sysctl.conf && sudo sysctl -p
If still, the error is there then you need to remove your node modules and again
npm install
and then
npm start
You might wanna add -w to {"start": "react-scripts start -w"}.
I had the same issue, fixed by adding --watch.
After creating a new project using create-react-app toolchain
Make sure to run
npm install, then
npm start
"dependencies": {
"react": "^17.0.1",
"react-scripts": "4.0.1",
}
if you are using these version then create .env file on the root directory of your project and type FAST_REFRESH=false and save it. Then yarn start OR npm start.
for linux first check the number of the files allowed using:
cat /proc/sys/fs/inotify/max_user_watches
in my, it was 8192 so I just change it to 524288 and it worked perfectly.
commands use to change is:
cd /proc/sys/fs/inotify
sudo nano max_user_watches
and then change no to 524288
save it and then use this command to apply the changes
sudo sysctl -p
reference from https://blog.jetbrains.com/idea/2010/04/native-file-system-watcher-for-linux/
Watch out where you add your .env file to. When you call create-react-app app-name, React will add following folder structure:
./root
./app-name
./node_modules
./public
./src
package.json
README.md
My problem was, I added .env file (with FAST_REFRESH=false inside) under the ./root directory. Placing the file in my ./app-name folder solved the issue.
I tried all the above suggestions, but still my react app does not refresh on code changes. Then I copied the previously working react project folder (only frontend) pasted it into the new project I am starting. Removed all the code related to old project and started using it. This solved my problem.
If this is feasible for you, you too can adopt the same method. If anyone discover a new simple solution, please post it here.
I had to run npm build everytime I wanted to refresh the app. Reinstalling node and npm didn't help. The project used a build tool called Parcel. Deleting the auto generated files such as .parcel-cache and dist resolved the issue for me.
My solution was to delete auto generated untracked/ignored files. The following git command might help:
git status --ignored
If you are on linux, check if it works with root access. If it does, stop your server and disable enforcement (for more details, man selinux).
sudo setenforce 0
Start your server again (without root), it might work.

Categories