Search code examples
reactjsamazon-s3cdn

Using CDN to serve node_modules for S3 static websites


I have prototyped a ReactJS static app (no backend) on my local computer by following some tutorial. I have used create-react-app to generate my project. The next step (which is not part of the tutorial) is to upload that to S3 as a static website. I noticed that the node_modules folder is quite large (around 500 MB).

In my reading of other tutorials about static websites in AWS, some JavaScript libraries (jquery) are actually served using CDN, instead of being bundled with the app folder that is be uploaded to S3. Can that be done with node_modules as well so I can avoid uploading that?


Solution

  • Create react app comes with the tools necessary to develop and build your application.

    develop

    When you develop, you can run npm run start, it will run the project as a website and open it on the default browser.

    build

    When you are ready to deploy, use 'npm run build', it will produce a build directory with optimised code ready for deployment. You can copy the contents of the build directory into your S3 bucket. https://create-react-app.dev/docs/production-build

    You can also check the available commands in the scripts section of your package.json.

    Then what happened to the node_modules directory

    The build process is using webpack to build the project. it will only include the bare minimum files required to run your application. It will not include the entire node_modules directory.