react.js | No pain,No gain. https://ufirst.jp/memo Sun, 01 Jun 2025 14:17:35 +0000 ja hourly 1 https://wordpress.org/?v=6.9.4 https://i0.wp.com/ufirst.jp/memo/wp-content/uploads/2022/08/apple-touch-icon-76x76-precomposed.png?fit=32%2C32&ssl=1 react.js | No pain,No gain. https://ufirst.jp/memo 32 32 126496119 reactプロジェクトを新規作成する https://ufirst.jp/memo/2025/06/react%e3%83%97%e3%83%ad%e3%82%b8%e3%82%a7%e3%82%af%e3%83%88%e3%82%92%e6%96%b0%e8%a6%8f%e4%bd%9c%e6%88%90%e3%81%99%e3%82%8b/ https://ufirst.jp/memo/2025/06/react%e3%83%97%e3%83%ad%e3%82%b8%e3%82%a7%e3%82%af%e3%83%88%e3%82%92%e6%96%b0%e8%a6%8f%e4%bd%9c%e6%88%90%e3%81%99%e3%82%8b/#respond Sun, 01 Jun 2025 14:17:34 +0000 https://ufirst.jp/memo/?p=3980 従来は create-reat-app を使ってreactのアプリの開発を始めることが一般的だったがcreate-react-appが非推奨となったため「vite」にて作成する

nodejsがインストールされている状態で以下を実行

% npm create vite@latest my-app-name

フレームワークの選択と進めためからreactを選ぶ

言語を選択する
SWCとはJS/TSのコンパイラ。従来通りのBabelにするかSWCにするか選択する必要がある。ビルド速度を優先したい場合はSWCにする。Babelプラグインを使いたい場合はBabelにする。

選択を終えたら以下のコマンドで開発サーバーが起動しコーディングができるようになる

cd my-app-name
npm install
npm run dev
]]>
https://ufirst.jp/memo/2025/06/react%e3%83%97%e3%83%ad%e3%82%b8%e3%82%a7%e3%82%af%e3%83%88%e3%82%92%e6%96%b0%e8%a6%8f%e4%bd%9c%e6%88%90%e3%81%99%e3%82%8b/feed/ 0 3980
プロジェクトごとにNodeのバージョンを変更する「Volta」を使う https://ufirst.jp/memo/2024/01/%e3%83%97%e3%83%ad%e3%82%b8%e3%82%a7%e3%82%af%e3%83%88%e3%81%94%e3%81%a8%e3%81%abnode%e3%81%ae%e3%83%90%e3%83%bc%e3%82%b8%e3%83%a7%e3%83%b3%e3%82%92%e5%a4%89%e6%9b%b4%e3%81%99%e3%82%8b%e3%80%8cvolta/ https://ufirst.jp/memo/2024/01/%e3%83%97%e3%83%ad%e3%82%b8%e3%82%a7%e3%82%af%e3%83%88%e3%81%94%e3%81%a8%e3%81%abnode%e3%81%ae%e3%83%90%e3%83%bc%e3%82%b8%e3%83%a7%e3%83%b3%e3%82%92%e5%a4%89%e6%9b%b4%e3%81%99%e3%82%8b%e3%80%8cvolta/#respond Mon, 29 Jan 2024 05:57:41 +0000 https://ufirst.jp/memo/?p=3869

Nodeのバージョンアップが激しすぎて1年も立つと過去のプロジェクトは最新のNodeで動作しなくなる。プロジェクトを変えるたびにNodeのバージョンを切り替えのは大変手間だが、それを自動的にやってくれるのが「VOLTA」である。

インストール手順

公式サイトを見るとLinuxとMacは次のようにする

curl https://get.volta.sh | bash

Windowsの場合は「download and run the Windows installer」よりインストーラーをダウンロードし実行する

Getting Started | Volta
Getting Started

利用方法

Nodeのバージョンを 14.14.5 にした場合は利用するディレクトリで次の通り実行する。

> volta install node@14.15.5
success: installed and set node@14.15.5 (with npm@6.14.11) as default

>node -v
v14.15.5


> npm -v
6.14.11

既存プロジェクトでNodeのバージョンを指定したい場合

> volta pin node@14.15.5
> volta pin npm@6.14.11

上記コマンドを実行すると「package.json」ファイル内に次の内容が追記され、バージョンが固定される。

  "volta": {
    "node": "14.15.5",
    "npm": "6.14.11"
  }

]]>
https://ufirst.jp/memo/2024/01/%e3%83%97%e3%83%ad%e3%82%b8%e3%82%a7%e3%82%af%e3%83%88%e3%81%94%e3%81%a8%e3%81%abnode%e3%81%ae%e3%83%90%e3%83%bc%e3%82%b8%e3%83%a7%e3%83%b3%e3%82%92%e5%a4%89%e6%9b%b4%e3%81%99%e3%82%8b%e3%80%8cvolta/feed/ 0 3869
create-react-app で作ったReactアプリにMapbox GL JSを使いビルドするとエラーになる https://ufirst.jp/memo/2022/10/create-react-app-%e3%81%a7%e4%bd%9c%e3%81%a3%e3%81%9freact%e3%82%a2%e3%83%97%e3%83%aa%e3%81%abmapbox-gl-js%e3%82%92%e4%bd%bf%e3%81%84%e3%83%93%e3%83%ab%e3%83%89%e3%81%99%e3%82%8b%e3%81%a8%e3%82%a8/ https://ufirst.jp/memo/2022/10/create-react-app-%e3%81%a7%e4%bd%9c%e3%81%a3%e3%81%9freact%e3%82%a2%e3%83%97%e3%83%aa%e3%81%abmapbox-gl-js%e3%82%92%e4%bd%bf%e3%81%84%e3%83%93%e3%83%ab%e3%83%89%e3%81%99%e3%82%8b%e3%81%a8%e3%82%a8/#respond Fri, 07 Oct 2022 16:00:24 +0000 https://ufirst.jp/memo/?p=3689 npm start

でWebPack Dev Serverで動作させているぶんには問題ないのだが、いざBuildを行いWebサーバー上でモジュールをデプロイし実行させると以下のエラー

An error occurred while parsing the WebWorker bundle. This is most likely due to improper transpilation by Babel; please see https://docs.mapbox.com/mapbox-gl-js/guides/install/#transpiling

please see https://docs.mapbox.com/mapbox-gl-js/guides/install/#transpiling と記載されているので読んで見る。

If you’re using @babel/preset-env in conjunction with browserslist to set target browser environments, consider using the following browserslist queries to select a set of compatible transforms.

https://docs.mapbox.com/mapbox-gl-js/guides/install/#transpiling

Browserlistを以下のようにせよとの事
Babelの設定を変更しトランスパイルされたモジュールの実行対象を絞る必要がある。

>0.2%, not dead, not ie 11, not chrome < 51, not safari < 10, not android < 51

このbrowserlistはpackage.jsonに定義されている。

変更前

  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },

変更後

  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not ie 11",
      "not chrome < 51",
      "not safari < 10",
      "not android < 51"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },

これで npm run build したものをWebサーバ上で実行すると無事動作するようになる。

]]>
https://ufirst.jp/memo/2022/10/create-react-app-%e3%81%a7%e4%bd%9c%e3%81%a3%e3%81%9freact%e3%82%a2%e3%83%97%e3%83%aa%e3%81%abmapbox-gl-js%e3%82%92%e4%bd%bf%e3%81%84%e3%83%93%e3%83%ab%e3%83%89%e3%81%99%e3%82%8b%e3%81%a8%e3%82%a8/feed/ 0 3689
reactで開発したアプリをWebサーバへ載せて動作させたら「Uncaught SyntaxError: Unexpected token ‘ https://ufirst.jp/memo/2022/09/react%e3%81%a7%e9%96%8b%e7%99%ba%e3%81%97%e3%81%9f%e3%82%a2%e3%83%97%e3%83%aa%e3%82%92web%e3%82%b5%e3%83%bc%e3%83%90%e3%81%b8%e8%bc%89%e3%81%9b%e3%81%a6%e5%8b%95%e4%bd%9c%e3%81%95%e3%81%9b%e3%81%9f/ https://ufirst.jp/memo/2022/09/react%e3%81%a7%e9%96%8b%e7%99%ba%e3%81%97%e3%81%9f%e3%82%a2%e3%83%97%e3%83%aa%e3%82%92web%e3%82%b5%e3%83%bc%e3%83%90%e3%81%b8%e8%bc%89%e3%81%9b%e3%81%a6%e5%8b%95%e4%bd%9c%e3%81%95%e3%81%9b%e3%81%9f/#respond Mon, 26 Sep 2022 09:05:27 +0000 https://ufirst.jp/memo/?p=3676 数時間ハマってしまったのでメモ。

原因はscriptタグによるjsファイルが読み込まれないこと

以下を見直す

package.json

"homepage": "***"

React Routerを使っている場合は以下についても

const router = createBrowserRouter([
    {
        path: "/",
        element: <App/>,
    }
],{
        basename: "***",
});
]]>
https://ufirst.jp/memo/2022/09/react%e3%81%a7%e9%96%8b%e7%99%ba%e3%81%97%e3%81%9f%e3%82%a2%e3%83%97%e3%83%aa%e3%82%92web%e3%82%b5%e3%83%bc%e3%83%90%e3%81%b8%e8%bc%89%e3%81%9b%e3%81%a6%e5%8b%95%e4%bd%9c%e3%81%95%e3%81%9b%e3%81%9f/feed/ 0 3676
create-react-appで作成したReactアプリのbuild出力先を変更する方法 https://ufirst.jp/memo/2022/08/create-react-app%e3%81%a7%e4%bd%9c%e6%88%90%e3%81%97%e3%81%9freact%e3%82%a2%e3%83%97%e3%83%aa%e3%81%aebuild%e5%87%ba%e5%8a%9b%e5%85%88%e3%82%92%e5%a4%89%e6%9b%b4%e3%81%99%e3%82%8b%e6%96%b9%e6%b3%95/ https://ufirst.jp/memo/2022/08/create-react-app%e3%81%a7%e4%bd%9c%e6%88%90%e3%81%97%e3%81%9freact%e3%82%a2%e3%83%97%e3%83%aa%e3%81%aebuild%e5%87%ba%e5%8a%9b%e5%85%88%e3%82%92%e5%a4%89%e6%9b%b4%e3%81%99%e3%82%8b%e6%96%b9%e6%b3%95/#respond Mon, 01 Aug 2022 07:16:08 +0000 https://ufirst.jp/memo/?p=3578 npm run eject

を実行することで隠蔽されていたWebPack関係のファイルが出現し設定を色々変更する事ができるようになる。

]]>
https://ufirst.jp/memo/2022/08/create-react-app%e3%81%a7%e4%bd%9c%e6%88%90%e3%81%97%e3%81%9freact%e3%82%a2%e3%83%97%e3%83%aa%e3%81%aebuild%e5%87%ba%e5%8a%9b%e5%85%88%e3%82%92%e5%a4%89%e6%9b%b4%e3%81%99%e3%82%8b%e6%96%b9%e6%b3%95/feed/ 0 3578
react にて npm run build実行時に出力先のディレクトリにあるファイルが削除されないようにする https://ufirst.jp/memo/2022/06/react-%e3%81%ab%e3%81%a6-npm-run-build%e5%ae%9f%e8%a1%8c%e6%99%82%e3%81%ab%e5%87%ba%e5%8a%9b%e5%85%88%e3%81%ae%e3%83%87%e3%82%a3%e3%83%ac%e3%82%af%e3%83%88%e3%83%aa%e3%81%ab%e3%81%82%e3%82%8b%e3%83%95/ https://ufirst.jp/memo/2022/06/react-%e3%81%ab%e3%81%a6-npm-run-build%e5%ae%9f%e8%a1%8c%e6%99%82%e3%81%ab%e5%87%ba%e5%8a%9b%e5%85%88%e3%81%ae%e3%83%87%e3%82%a3%e3%83%ac%e3%82%af%e3%83%88%e3%83%aa%e3%81%ab%e3%81%82%e3%82%8b%e3%83%95/#respond Fri, 24 Jun 2022 01:56:23 +0000 https://ufirst.jp/memo/?p=3561 npm run build を実行すると出力先ディレクトリのファイルがすべて削除されてしまう。

サーバーサイドのスクリプトなんかを同ディレクトリへ配置しておきたい場合困る。

/scripts/build.js を変更することで元ファイルを残すことが出来る

  .then(previousFileSizes => {
    // Remove all content but keep the directory so that
    // if you're in it, you don't end up in Trash
    fs.emptyDirSync(paths.appBuild);
  .then(previousFileSizes => {
    // Remove all content but keep the directory so that
    // if you're in it, you don't end up in Trash
    //fs.emptyDirSync(paths.appBuild);

]]>
https://ufirst.jp/memo/2022/06/react-%e3%81%ab%e3%81%a6-npm-run-build%e5%ae%9f%e8%a1%8c%e6%99%82%e3%81%ab%e5%87%ba%e5%8a%9b%e5%85%88%e3%81%ae%e3%83%87%e3%82%a3%e3%83%ac%e3%82%af%e3%83%88%e3%83%aa%e3%81%ab%e3%81%82%e3%82%8b%e3%83%95/feed/ 0 3561
ReactにてError: error:0308010C:digital envelope routines::unsupported エラーが発生 https://ufirst.jp/memo/2022/06/react%e3%81%ab%e3%81%a6error-error0308010cdigital-envelope-routinesunsupported-%e3%82%a8%e3%83%a9%e3%83%bc%e3%81%8c%e7%99%ba%e7%94%9f/ https://ufirst.jp/memo/2022/06/react%e3%81%ab%e3%81%a6error-error0308010cdigital-envelope-routinesunsupported-%e3%82%a8%e3%83%a9%e3%83%bc%e3%81%8c%e7%99%ba%e7%94%9f/#respond Wed, 22 Jun 2022 13:30:02 +0000 https://ufirst.jp/memo/?p=3559 (node:43616) [DEP0111] DeprecationWarning: Access to process.binding('http_parser') is deprecated. (Use `node --trace-deprecation ...` to show where the warning was created) Starting the development server... Error: error:0308010C:digital envelope routines::unsupported at new Hash (node:internal/crypto/hash:67:19) at Object.createHash (node:crypto:133:10) opensslErrorStack: [ 'error:03000086:digital envelope routines::initialization error' ], library: 'digital envelope routines', reason: 'unsupported', code: 'ERR_OSSL_EVP_UNSUPPORTED'

とのエラーが発生。原因はNodeJSが新しすぎること。

2022/06/22時点の安定版であるNodeJS v16を再インストールすることで解消。

]]>
https://ufirst.jp/memo/2022/06/react%e3%81%ab%e3%81%a6error-error0308010cdigital-envelope-routinesunsupported-%e3%82%a8%e3%83%a9%e3%83%bc%e3%81%8c%e7%99%ba%e7%94%9f/feed/ 0 3559
react開発時に特定のリクエストを外部サーバへ転送する方法 https://ufirst.jp/memo/2021/09/react%e9%96%8b%e7%99%ba%e6%99%82%e3%81%ab%e7%89%b9%e5%ae%9a%e3%81%ae%e3%83%aa%e3%82%af%e3%82%a8%e3%82%b9%e3%83%88%e3%82%92%e5%a4%96%e9%83%a8%e3%82%b5%e3%83%bc%e3%83%90%e3%81%b8%e8%bb%a2%e9%80%81/ https://ufirst.jp/memo/2021/09/react%e9%96%8b%e7%99%ba%e6%99%82%e3%81%ab%e7%89%b9%e5%ae%9a%e3%81%ae%e3%83%aa%e3%82%af%e3%82%a8%e3%82%b9%e3%83%88%e3%82%92%e5%a4%96%e9%83%a8%e3%82%b5%e3%83%bc%e3%83%90%e3%81%b8%e8%bb%a2%e9%80%81/#respond Wed, 29 Sep 2021 09:36:32 +0000 https://ufirst.jp/memo/?p=3268 create-react-app hogehoge cd hogehoge npm start

Webpack dev serverが起動しホットリロードで開発ができるようになる。

フロント側の開発ならこれでいいのだが、サーバーサイドの処理や外部APIを利用する場合このままでは都合が悪い。

その場合、特定のパスだけ外部サーバーに飛ばすようにする。
手順は以下の通り

npm install http-proxy-middleware
/src/setupProxy.js
const { createProxyMiddleware } = require("http-proxy-middleware");
module.exports = function (app) {
  app.use(
    "/api",
    createProxyMiddleware({
      target: "https://localhost:8080",
      changeOrigin: true,
    })
  );
};
npm start

これで http//localhost/api/***
のリクエストは http://localhost:8080 へリバースプロキシされる。

]]>
https://ufirst.jp/memo/2021/09/react%e9%96%8b%e7%99%ba%e6%99%82%e3%81%ab%e7%89%b9%e5%ae%9a%e3%81%ae%e3%83%aa%e3%82%af%e3%82%a8%e3%82%b9%e3%83%88%e3%82%92%e5%a4%96%e9%83%a8%e3%82%b5%e3%83%bc%e3%83%90%e3%81%b8%e8%bb%a2%e9%80%81/feed/ 0 3268
MUI にてエラー「Module not found: Can’t resolve ‘@emotion/react’ in ‘***/node_modules/@mui/styine’」 https://ufirst.jp/memo/2021/09/mui-%e3%81%ab%e3%81%a6%e3%82%a8%e3%83%a9%e3%83%bc%e3%80%8cmodule-not-found-cant-resolve-emotion-react-in-node_modules-mui-styine%e3%80%8d/ https://ufirst.jp/memo/2021/09/mui-%e3%81%ab%e3%81%a6%e3%82%a8%e3%83%a9%e3%83%bc%e3%80%8cmodule-not-found-cant-resolve-emotion-react-in-node_modules-mui-styine%e3%80%8d/#respond Tue, 28 Sep 2021 13:43:27 +0000 https://ufirst.jp/memo/?p=3260 ./node_modules/@mui/styled-engine/index.js Module not found: Can't resolve '@emotion/react' in '***/node_modules/@mui/styine' Compiling... Compiled with warnings.

以下をインストールする事で解消

npm install @emotion/react @emotion/styled
]]>
https://ufirst.jp/memo/2021/09/mui-%e3%81%ab%e3%81%a6%e3%82%a8%e3%83%a9%e3%83%bc%e3%80%8cmodule-not-found-cant-resolve-emotion-react-in-node_modules-mui-styine%e3%80%8d/feed/ 0 3260
ractでcssでコンパイルエラー「Error: Can’t resolve ‘./images/***.***’ in ‘***/src’ 」 https://ufirst.jp/memo/2021/09/ract%e3%81%a7css%e3%81%a7%e3%82%b3%e3%83%b3%e3%83%91%e3%82%a4%e3%83%ab%e3%82%a8%e3%83%a9%e3%83%bc%e3%80%8cerror-cant-resolve-images-in-src-%e3%80%8d/ https://ufirst.jp/memo/2021/09/ract%e3%81%a7css%e3%81%a7%e3%82%b3%e3%83%b3%e3%83%91%e3%82%a4%e3%83%ab%e3%82%a8%e3%83%a9%e3%83%bc%e3%80%8cerror-cant-resolve-images-in-src-%e3%80%8d/#respond Tue, 28 Sep 2021 12:51:09 +0000 https://ufirst.jp/memo/?p=3256 Failed to compile. ./src/***.css (./node_modules/css-loader/dist/cjs.js??ref--5-oneOf-4-1!./node_modules/postcss-loader/src??postcss!./src/***.css) Error: Can't resolve './images/***.gif' in '***/src'

エラー箇所

background-image: url('./images/***.gif');

どうやらコンパイル時にurlで指定されたパスを参照しているらしく、srcフォルダに画像を置いてやることで解消した。

]]>
https://ufirst.jp/memo/2021/09/ract%e3%81%a7css%e3%81%a7%e3%82%b3%e3%83%b3%e3%83%91%e3%82%a4%e3%83%ab%e3%82%a8%e3%83%a9%e3%83%bc%e3%80%8cerror-cant-resolve-images-in-src-%e3%80%8d/feed/ 0 3256