This example sets up a new React app with Metaplex using "Create React App" (CRA) version 4 — i.e. using Webpack 4. Note that, if you're starting a new project, we recommend using CRA 5 which uses Webpack 5.
This example has been generated using the following steps:
-
Create a new project using the "Create React App" command.
npx create-react-app getting-started-react-cra4 --scripts-version 4.0.3 cd getting-started-react-cra4
-
Install the Metaplex and the Solana SDKs.
npm install @metaplex-foundation/js-next @solana/web3.js
-
Install and use
react-app-rewired
.This enables us to override some Webpack configurations in the next step.
# Installs react-app-rewired. npm install react-app-rewired # Replaces "react-scripts" with "react-app-rewired" in package.json scripts. sed -i '' 's/react-scripts /react-app-rewired /g' package.json
-
Override Webpack 4 configurations.
Create a new file to override Webpack 4 configurations.
touch config-overrides.js
Copy the following code inside the new
config-overrides.js
file.Why?
If we skip this step, running `npm start` will fail, complaining that it can't resolve `.mjs` files provided by the Web3.js library. To fix this, we need to help Webpack resolve `.mjs` files by adding an extra rule.module.exports = function override(webpackConfig) { webpackConfig.module.rules.push({ test: /\.mjs$/, include: /node_modules/, type: "javascript/auto", }); return webpackConfig; };
-
Update your browser requirements.
Update the
browserslist
object of yourpackage.json
to include the following production requirements.Why?
If we skip this step, building and serving your app for production will give us the following error in the console.Uncaught TypeError: Cannot convert a BigInt value to a number
This is because Webpack will try to change the code of the deprecated nested dependency
noble-ed25519
to make sure it works on browsers that don't supportBigInt
. However, all modern browsers supportBigInt
so we can fix this by updating thebrowserslist
object in ourpackage.json
."browserslist": { "production": [ - ">0.2%", - "not dead", - "not op_mini all" + "chrome >= 67", + "edge >= 79", + "firefox >= 68", + "opera >= 54", + "safari >= 14" ], "development": [ "last 1 chrome version", "last 1 firefox version", "last 1 safari version" ] },
-
That's it! 🎉
You're now ready to start building your app. You can use the following commands to build and serve your app.
# Build and serve for development. npm start # Build and serve for production. npm run build && serve -s build
If you're interested in how this example app is using the Metaplex SDK, check out the
App.js
andApp.css
files in thesrc
directory.
Looking for the README file autogenerated by "Create React App"? It's been moved here.