How to disable file watching in a react app - node.js

EDIT:
My question is now whether it is possible to launch a react app with react-scripts in a production-like environment where I don't necessarily want changes to files to affect the running react server. check below in Solutions section under C for a brief description on what I have already tried.
I have been struggling with this question for the past two weeks and I am unsure how to proceed. I would like to host my React App on a Node Azure Web App (Linux).
The problem I am seeing:
Starting the development server...
events.js:170
throw er; // Unhandled 'error' event
^
Error: ENOSPC: System limit for number of file watchers reached, watch 'xxx'
at FSWatcher.start (internal/fs/watchers.js:165:26)
at Object.watch (fs.js:1274:11)
at createFsWatchInstance (/home/u/work/some-repo/node_modules/chokidar/lib/nodefs-handler.js:37:15)
at setFsWatchListener (/home/u/work/some-repo/node_modules/chokidar/lib/nodefs-handler.js:80:15)
...
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! some-repo#0.1.0 start: `react-scripts start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the some-repo#0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/u/.npm/_logs/2019-08-30T09_50_59_595Z-debug.log
Solutions attempted:
After reading up on this issue online I have understood that the solution might involve
A: no space on the system. I have checked using df -h and looks like there is plenty of space (at least 50% for my machine).
B: file watcher limit reached, this seems to be the problem but unfortunately the Azure Web App is a read-only file system, and running something like echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p did not help.
C: Disable watching with React. It doesn't appear that I can do this however, if anyone is aware of how to Disable watching with React it would be very much appreciated. So far I have tried CI=true npm start to no avail (when I ran command and made a change to a file, the change was actively reflected on my browser: localhost:3000
I have also tried adding a multitude of options inside of node_modules/react-scripts/webpack.config.js such as:
watch: false and ignore:/node_modules/.
Thanks,
Nik

I suggest you to reconsider your goal to launch a react app with react-scripts in a production-like environment.
As #adamz4008 mentioned, you shouldn't run development server on production. Therefore, you won't hit the watching issue, and you don't need to solve it.
Documentation / best practices
According to software development best practices there are different phases of the development.
Facebook React documentation covers them as well.
Develop locally
npm start / yarn start
Runs the app in development mode.
https://github.com/facebook/create-react-app#npm-start-or-yarn-start
Build
npm run build / yarn build
Builds the app for production to the build folder.
It correctly bundles React in production mode and optimizes the build for the best performance.
The build is minified and the filenames include the hashes.
Your app is ready to be deployed.
https://github.com/facebook/create-react-app#npm-run-build-or-yarn-build
Installing NPM dependencies
BTW, usually people run npm install before npm run build to install all necessary dependencies.
But I recommend to use npm ci. It has deterministic stable results and higher performance.
https://docs.npmjs.com/cli/ci.html
Release / Deploy
As your application is just a bunch of static files, you can simply deploy it as static HTML web site to any web server / platform.
My experience
In my practical experience with Azure, I deploy React app to a storage account as a static web site (without using/configuring any web server explicitly).
https://learn.microsoft.com/en-us/azure/storage/blobs/storage-blob-static-website
You can choose any other option.

So something you could do is create the build file and then serve the static files in the directory with a tiny local server.
You could run the yarn build then have a script that runs this code yarn node ./server.js (or whatever you name it)
This could allow for the performant builds and the server similar to the DevServer that react-scripts start server.
I also don't know if you need profiling in production but I added a feature on react-scripts build --profile that allows classnames and functions names to persist through the terser.
const express = require(`express`);
require(`dotenv`).config();
express()
.use(express.static(`${__dirname}/build`))
.get(`*`, (req, res) => res.sendFile(`${__dirname}/build/index.html`))
.listen(process.env.PORT || 1234, () => console.log(`__SERVER_RUNNING__`, process.env.PORT),
);

For the simple case (your app resides in the server root, you have no client-side routing, you don't have to proxy backend calls):
For the first time, install http-server with yarn add --dev http-server
Build the application with yarn build
Serve the built application with yarn http-server ./build
For more complex cases, either use a more advanced webserver like local-web-server or implement the desired behavior yourself using express as described in other answers.

Related

Package and run nodejs application in an offline server

I'm currently using NextJS and to run a production build, I use npm run build followed by npm run start. This all works well on my local machine.
However, I need to deploy this app on an offline machine where I may not have an internet connection to install all the node packages.
I've taken a look at npm pkg and npm pack utilities, but not quite sure which is the best one to use in the context of a nextjs app.
Would appreciate any advice on the best way to do this.
Edit: thinking along the lines of how I would build my maven project and have a .jar output which I can use to deploy to any other machine as a single deployable file.

Strapi: Warning an error occurred while requesting the API

I've been struggling for hours trying to install Strapi on my Ubuntu server.
Ubuntu: 20.04
nodejs: v14.19.0
npm: 6.14.16
strapi: 4
npx create-strapi-app#latest my-project --quickinstall
All the installation process goes seamlessly but when I go to http://mydomain:1337/admin in order to create a first user I get this warning:
an error occurred while requesting the API.
I know this problem has been encountered several times but none of the suggested solutions have helped me so far.
I also found this error in the Chrome console but I'm not not sure it's related to the my problem:
main.815f1087.js:2 Refused to connect to
'http://localhost:1337/admin/project-type' because it violates the
following Content Security Policy directive: "connect-src 'self'
https:".
Any idea ?
You need to build the app. Read this github issue
Run: npm run build or yarn build or strapi build depending on what you are using
First Build your Application by using npm run build then run npm start.
If you are running strapi CMS on docker, and using docker composer to build, then maintain the default port 1337 when binding the container port to your system port.
Do this: -1337:1337
Not this: -8000:13337
I had this error after upgrading to latest Strapi and all dependencies, what helped me, is to rebuild the Strapi admin interface:
yarn build
or
npm run build
then start develop
yarn develop
or
npm run develop
Got the same problem, a simple restart of the server did it for me.
First of all, need to Build the admin panel and then Start the application.
To build the admin panel,
npm run build
# or
yarn build
To start the application with auto-reload,
npm run develop
# or
yarn develop
when you change the port of your strapi app, you must run npm run build or yarn build or strapi build. because strapi admin templates use default port 1337 and for using on another port you must build the project again.
What I did is to change the host address from 0.0.0.0 to 127.0.0.1 in server.js and then ran npm run build and that fixed my issue. I know it sounds strange but it worked.
Thanks.

Is it mandatory for each deployment to production for remove node modules and run npm install?

I use vuetify (vue)
Is it mandatory for each deployment to production for remove node modules and run npm install? Or just run npm run build?
I have two option :
Option 1 : Every deployment, I run the npm run build directly
Option 2 :
Delete the contents of dist folder
Delete node_modules folder
npm install
npm run build
Which is the best option?
npm install
This command installs a package, and any packages that it depends on. If the package has a package-lock or shrinkwrap file, the installation of dependencies will be driven by that, with an npm-shrinkwrap.json taking precedence if both files exist. See package-lock.json and npm-shrinkwrap.
If you did not install or update the package before releasing the project, you do not need to execute npm install, otherwise, you need to execute it to ensure that dependent packages on the production environment is consistent with your local dependent package version.
If you are using an automatic build deployment tool like jenkins, for convenience you can execute the install command before each build. It's okay.
Imagine more environments, not just a production:
development
testing1
staging
uat
production
Can we upload the npm run build result (compressed js) or node_modules to our git repository? ANSWER IS NOT!!. So if you need to have a version of your app running in any of these environments, you must to execute npm run build. And this command needs the classic npm run install. I think this last sentence, answer your question.
(ADVICE) Docker to the rescue
assumption 1 your client-side app (vue) is not complex(no login, no session, no logout, etc ), you could publish it using a basic nginx, apache, basic-nodejs.
assumption 2 you are able to have one more server for docker private repository. Also if you are in google, amazon o azure, this service is ready to use, of course a payment is required
In one line, with docker you must execute just one time npm install and npm run build. Complete flow is:
developer push some changes to the git repository
manually or automatically a docker build in launched.
inside Dockerfile, npm install and npm run build is executed. Also a minimal server with nodejs (example) is configured pointing to your builded assets
your new docker image is uploaded to your docker private repository
that's all
If your quality assurance team needs to perform some tests to your new app, just a docker image download is required. If everything is ok, you pass to the next stage (staging or uat) or production. Steps will be the same: just download the docker image.
Optimizations
Use docker stages to split build and start steps
If your app does not have complex flows(no login, no session, no logout, etc ), replace node basic server with a simple nginx
I need login and logout
In this case, nginx or apache does not helps you because they are a simple static servers.
You could use a minimal nodejs code like this:
https://github.com/jrichardsz/nodejs-static-pages/blob/master/server.js
Adding /login , /logout, etc
Or use my server:
https://github.com/utec/geofrontend-server
which has a /login, /logout and other cool features for example: How are you planning to pass your backend api urls to your vue app in any of your environments?.

React app deployment error in npm development build

I'm developing a react application.When I try to deploy it using npm start it is showing that your development build is not optimized.Use npm run build to install production build.
The message means exactly what it says. Use npm run build to make a production build, and serve those built static files with some http server like nginx or apache. Your current npm start is making a debug build, and probably serving it with a hot reloading server.
To identify the differences between a production build and a debug build, check out your underlying build system and its configurations.

How to deploy Nestjs App (on Azure)?

I am trying to deploy a Nestjs API (with Auth0 authentication). When I run it in VS Code with npm run start:watch server, everything's fine.
Now the question is: what should I do to deploy it on a webserver? Should I only copy the dist folder (after runnin tsc)? what about node_modules? Should I leave the port to 3000?
As a side note I am trying to deploy it on Azure but I guess the questions holds for any platform.
Many thanks!
Modify your package.json file, and add a postinstall script, this script should be tsc or tsc --sourceMap false if you would like to avoid sourceMaps from being generated.
That would make azure to run tsc after installing all npm packages, remember to change start script also, so its value is 'node dist/index.js'

Resources