所以我正在用React构建一个Rails 7应用程序。我试图在react前端显示来自rails API的数据,但当我尝试访问http://localhost:3000/
顺便说一下,我没有使用webpack,而是使用yarn作为包管理器。
我尝试将react脚本更新到5.0.0,但没有成功。我尝试使用react错误覆盖6.0.10和6.0.11的react脚本4.0.3。也尝试了那些反应脚本为5.0.0的版本……真的没有任何效果,我要疯了lol
我删除了yarn.lock文件,无数次安装了yarn。我还尝试了纱线添加过程。。
有人能帮我吗?
安慰
Uncaught ReferenceError: process is not defined at index.js? [sm]:212:5 (anonymous) @ index.js? [sm]:212
这是我的代码:
包.json
{ "devDependencies": { "react-error-overlay": "6.0.9" }, "resolutions": { "react-error-overlay": "6.0.9" }, "dependencies": { "react-scripts": "4.0.3" } }
yarn.lock包括
react-dev-utils@^11.0.3: version "11.0.4" resolved "https://registry.yarnpkg.com/react-dev-utils/-/react-dev-utils-11.0.4.tgz#a7ccb60257a1ca2e0efe7a83e38e6700d17aa37a" integrity sha512-dx0LvIGHcOPtKbeiSUM4jqpBl3TcY7CDjZdfOIcKeznE7BWr9dg0iPG90G5yfVQ+p/rGNMXdbfStvzQZEVEi4A== dependencies: "@babel/code-frame" "7.10.4" address "1.1.2" browserslist "4.14.2" chalk "2.4.2" cross-spawn "7.0.3" detect-port-alt "1.1.6" escape-string-regexp "2.0.0" filesize "6.1.0" find-up "4.1.0" fork-ts-checker-webpack-plugin "4.1.6" global-modules "2.0.0" globby "11.0.1" gzip-size "5.1.1" immer "8.0.1" is-root "2.1.0" loader-utils "2.0.0" open "^7.0.2" pkg-up "3.1.0" prompts "2.4.0" react-error-overlay "6.0.9" recursive-readdir "2.2.2" shell-quote "1.7.2" strip-ansi "6.0.0" text-table "0.2.0" react-error-overlay@6.0.9, react-error-overlay@^6.0.9: version "6.0.9" resolved "https://registry.yarnpkg.com/react-error-overlay/-/react-error-overlay-6.0.9.tgz#3c743010c9359608c375ecd6bc76f35d93995b0a" integrity sha512-nQTTcUu+ATDbrSD1BZHr5kgSD4oF8OFjxun8uAaL8RwPBacGBNPf/yAuVVdx17N8XNzRDMrZ9XcKZHCjPW+9ew== react-is@^16.13.1: version "16.13.1" resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4" integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ== react-is@^17.0.1: version "17.0.2" resolved "https://registry.yarnpkg.com/react-is/-/react-is-17.0.2.tgz#e691d4a8e9c789365655539ab372762b0efb54f0" integrity sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w== react-refresh@^0.8.3: version "0.8.3" resolved "https://registry.yarnpkg.com/react-refresh/-/react-refresh-0.8.3.tgz#721d4657672d400c5e3c75d063c4a85fb2d5d68f" integrity sha512-X8jZHc7nCMjaCqoU+V2I0cOhNW+QMBwSUkeXnTi8IPe6zaRWfn60ZzvFDZqWPfmSJfjub7dDW1SP0jaHWLu/hg== react-scripts@4.0.3: version "4.0.3" resolved "https://registry.yarnpkg.com/react-scripts/-/react-scripts-4.0.3.tgz#b1cafed7c3fa603e7628ba0f187787964cb5d345" integrity sha512-S5eO4vjUzUisvkIPB7jVsKtuH2HhWcASREYWHAQ1FP5HyCv3xgn+wpILAEWkmy+A+tTNbSZClhxjT3qz6g4L1A== dependencies: "@babel/core" "7.12.3" "@pmmmwh/react-refresh-webpack-plugin" "0.4.3" "@svgr/webpack" "5.5.0" "@typescript-eslint/eslint-plugin" "^4.5.0" "@typescript-eslint/parser" "^4.5.0" babel-eslint "^10.1.0" babel-jest "^26.6.0" babel-loader "8.1.0" babel-plugin-named-asset-import "^0.3.7" babel-preset-react-app "^10.0.0" bfj "^7.0.2" camelcase "^6.1.0" case-sensitive-paths-webpack-plugin "2.3.0" css-loader "4.3.0" dotenv "8.2.0" dotenv-expand "5.1.0" eslint "^7.11.0" eslint-config-react-app "^6.0.0" eslint-plugin-flowtype "^5.2.0" eslint-plugin-import "^2.22.1" eslint-plugin-jest "^24.1.0" eslint-plugin-jsx-a11y "^6.3.1" eslint-plugin-react "^7.21.5" eslint-plugin-react-hooks "^4.2.0" eslint-plugin-testing-library "^3.9.2" eslint-webpack-plugin "^2.5.2" file-loader "6.1.1" fs-extra "^9.0.1" html-webpack-plugin "4.5.0" identity-obj-proxy "3.0.0" jest "26.6.0" jest-circus "26.6.0" jest-resolve "26.6.0" jest-watch-typeahead "0.6.1" mini-css-extract-plugin "0.11.3" optimize-css-assets-webpack-plugin "5.0.4" pnp-webpack-plugin "1.6.4" postcss-flexbugs-fixes "4.2.1" postcss-loader "3.0.0" postcss-normalize "8.0.1" postcss-preset-env "6.7.0" postcss-safe-parser "5.0.2" prompts "2.4.0" react-app-polyfill "^2.0.0" react-dev-utils "^11.0.3" react-refresh "^0.8.3" resolve "1.18.1" resolve-url-loader "^3.1.2" sass-loader "^10.0.5" semver "7.3.2" style-loader "1.3.0" terser-webpack-plugin "4.2.3" ts-pnp "1.2.0" url-loader "4.1.1" webpack "4.44.2" webpack-dev-server "3.11.1" webpack-manifest-plugin "2.2.0" workbox-webpack-plugin "5.1.4" optionalDependencies: fsevents "^2.1.3"
反应控制器.js
import { Controller } from "@hotwired/stimulus"; import React from "react"; import ReactDOM from "react-dom"; import App from "../components/App"; export default class extends Controller { connect() { console.log("Connected"); const e = React.createElement; const root = ReactDOM.createRoot(document.getElementById('root')); root.render(e(App), root); } }
应用程序.js
import React from 'react' import { BrowserRouter as Router, Route, Routes } from 'react-router-dom' import Companies from './Companies/Companies' import Company from './Company/Company' import htm from 'htm'; const html = htm.bind(React.createElement); console.log(process.env.NODE_ENV) const App = () => { return html` <${Router}> <${Routes}> <${Route} exact path='/' component=${Companies}<//> <${Route} exact path='/companies/:slug' component=${Company}/> <//> <//> `; } export default App
索引js
// Import and register all your controllers from the importmap under controllers/* import { application } from "./application" import ReactController from "./react_controller" application.register("react", ReactController) // Eager load all controllers defined in the import map under controllers/**/*_controller import { eagerLoadControllersFrom } from "@hotwired/stimulus-loading" eagerLoadControllersFrom("controllers", application)
babel配置.js
module.exports = function(api) { var validEnv = ['development', 'test', 'production'] var currentEnv = api.env() var isDevelopmentEnv = api.env('development') var isProductionEnv = api.env('production') var isTestEnv = api.env('test') if (!validEnv.includes(currentEnv)) { throw new Error( 'Please specify a valid `NODE_ENV` or ' + '`BABEL_ENV` environment variables. Valid values are "development", ' + '"test", and "production". Instead, received: ' + JSON.stringify(currentEnv) + '.' ) } return { presets: [ isTestEnv && [ '@babel/preset-env', { targets: { node: 'current' } } ], (isProductionEnv || isDevelopmentEnv) && [ '@babel/preset-env', { forceAllTransforms: true, useBuiltIns: 'entry', corejs: 3, modules: false, exclude: ['transform-typeof-symbol'] } ] ].filter(Boolean), plugins: [ 'babel-plugin-macros', '@babel/plugin-syntax-dynamic-import', isTestEnv && 'babel-plugin-dynamic-import-node', '@babel/plugin-transform-destructuring', [ '@babel/plugin-proposal-class-properties', { loose: true } ], [ '@babel/plugin-proposal-object-rest-spread', { useBuiltIns: true } ], [ '@babel/plugin-proposal-private-methods', { loose: true } ], [ '@babel/plugin-proposal-private-property-in-object', { loose: true } ], [ '@babel/plugin-transform-runtime', { helpers: false } ], [ '@babel/plugin-transform-regenerator', { async: false } ] ].filter(Boolean) } }