我正在使用Babel和Webpack开发一个ReactJS应用程序。我正在使用create-react-app facebook脚本,因此它可以处理Webpack的配置。我的问题是我创建了一个js文件并添加:
var childProcess = require('child_process');
Run Code Online (Sandbox Code Playgroud)
但是当我想编译新版本时,出现以下错误:
Module not found: 'child_process'.
Run Code Online (Sandbox Code Playgroud)
我不知道该怎么办。我已经读到将自定义配置添加到webpack.config.js可能是解决方案,但是我正在使用create react app,所以我没有Webpack配置。我尝试运行npm run弹出并创建自己的webpack.config.js,但是它不起作用。
我希望有人能帮助我。
我正在尝试使用库中的变量,该变量已加载到index.html我的 React 组件的脚本标记中。我已经正常加载了:
<head>
...
<script src="https://cdn.plaid.com/link/v2/stable/link-initialize.js"></script>
<!-- gives me the 'Plaid' library -->
...
<title>React App</title>
</head>
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试访问Plaid我的 React 组件时,它是未定义的。我很困惑,因为如果我在它之前放置一个调试器,我仍然可以访问它。例如,在我的App.js组件中,我有:
componentDidMount() {
debugger // can access 'Plaid' here
Plaid // throws error, 'Plaid' is undefined
}
Run Code Online (Sandbox Code Playgroud)
为什么会Plaid抛出错误,即使我可以通过调试器访问它?
我已经通过 create react app 编译了我的应用程序: yarn build //code is minified and obfuscated as expected
然后我通过firebase serve和部署了该应用程序firebase deploy。
我的 firebase.json:
{
"database": {
"rules": "database.rules.json"
},
"hosting": {
"public": "build",
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
所有都“几乎按预期”部署,有两个警告:
该结构看起来与包含媒体的构建文件夹完全不同......(字体必需):
我是否设置错误,部署了错误的文件夹,或者可能没有忽略我应该做的事情?公开整个 js 堆栈未缩小和或没有任何混淆似乎非常“不安全”......
再次,一如既往,非常感谢任何方向!
javascript firebase reactjs firebase-hosting create-react-app
我得到'React.Children.only预计会收到一个React元素的孩子.来自以下代码.
我使用create-react-app创建了一个新的react应用程序并安装了redux和react-redux,并创建了一个简单的reducer并使用它创建商店.
import React from "react";
import ReactDOM from "react-dom";
import { createStore, combineReducers } from "redux";
import { Provider } from "react-redux";
import logo from './logo.svg';
import './App.css';
class App extends React.Component {
render() {
return (
<div className="App">
<div className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h2>Welcome to React</h2>
</div>
<p className="App-intro">
To get started, edit <code>src/App.js</code> and save to reload.
</p>
</div>
);
}
}
function productReducer(state = [], action) {
return state;
}
const store = createStore( …Run Code Online (Sandbox Code Playgroud) 默认情况下,create-react-app包含的新项目中有一个 Service Worker 脚本。我不需要那个默认值registerServiceWorker.js和默认值service-worker.js(在构建中隐藏和可用),因为我想添加我自己的 Service Worker。我也想用 es6 语法创建我自己的 service worker 代码,所以只在公共文件夹中添加一个“sw.js”并在我的 index.html 中注册它对我来说不是最好的选择。如何使用 构建一些自定义 sw 代码ES6 create-react-app?
ecmascript-6 reactjs webpack service-worker create-react-app
在我的 package.json 文件中,我有
"homepage" : "http://myname.project.com/myapp",
我使用构建应用程序npm run build,输出确认创建了一个 /build 目录。输出:
102.97 KB 构建/静态/js/main.bfa46b52.js
24.37 KB 构建/静态/css/main.cfe8a47e.css
该项目是在假设它托管在 /myapp/ 的情况下构建的。您可以使用 package.json 中的主页字段来控制它。
构建文件夹已准备好部署。
如果我使用启动应用程序npm start或serve -s build应用程序将启动并且 index.html 页面将加载,但从 index.html 对静态资源的引用已损坏(/myapp/static/js/main.js 和 /myapp/static/css /main.css)。我可以在没有 /myapp 的情况下直接导航到它们(localhost:5000/static/js/main.js 有效;localhost:5000/myapp/static/js/main.js 无效)
如果我将构建文件夹部署到 /myapp 目录下的其他普通旧 apache 服务器,它似乎按预期工作。有什么方法可以配置serve -s build为从 /myapp 目录提供页面吗?
该应用程序目前只有一页,没有链接,也没有实现 react-router 或任何等效的东西。该团队一直在使用 webpack,但将其删除。该应用程序使用 react-scripts 进行捆绑。
我需要为每个开发/生产服务器加载一个不同的 .env 文件。
Dev/Prod Server | .env file
---------------------------------------------
localhost | .env.development.local
development | .env.development
qa | .env.qa
production | .env.production
Run Code Online (Sandbox Code Playgroud)
所以,我尝试以这种方式编辑 package.json
"start-js": "react-scripts start .env.development.local",
"start": " npm-run-all -p watch-css start-js",
"build:dev": "npm run build-css && react-scripts build .env.development",
"build:qa": "npm run build-css && react-scripts build .env.qa",
"build:pro": "npm run build-css && react-scripts build .env.production"
Run Code Online (Sandbox Code Playgroud)
但是如果我执行一个build:*命令,加载的文件总是 .env.production 因为 NODE_ENV 是生产。是否可以使用"build:*命令加载其他 .env 文件?
当您使用create react app并在代码中出错时,您将获得这个非常详细和精确的错误页面:

如何将此错误页面合并到我自己的自定义项目中.我怀疑需要一种类型的webpack插件.我项目中的工具是:
这是我目前的webpack配置:
import webpack from 'webpack'
import autoprefixer from 'autoprefixer'
import HtmlWebpackPlugin from 'html-webpack-plugin'
import ExtractTextPlugin from 'extract-text-webpack-plugin'
import CopyWebpackPlugin from 'copy-webpack-plugin'
import FriendlyErrorsPlugin from 'friendly-errors-webpack-plugin'
import helpers from './helpers'
const NODE_ENV = process.env.NODE_ENV
const isProd = NODE_ENV === 'production'
module.exports = {
entry: {
'app': [
helpers.root('client/app/index.jsx')
]
},
output: {
path: helpers.root('dist'),
publicPath: '/'
},
resolve: {
extensions: ['.js', '.jsx', '.json', '.css', '.scss', '.html'],
alias: {
'app': 'client/app' …Run Code Online (Sandbox Code Playgroud) 我create-react-app在尝试在智能电视浏览器中运行应用程序时遇到了一个环境大问题。
规格电视和浏览器(http://whatismybrowser.com)
这是我想要的:
create-react-app项目yarn buildyarn global add serve && serve -s buildhttp://<my-lan-ip>:5000(5000端口为serve命令提供的默认端口)当我按照这些步骤操作时,我会得到一个空白页。空白页仅在电视浏览器中出现。在PC和Mobile上运行正常。
因此,我想知道并要求在这里分享一些想法:
有人已经遇到这个问题了吗?有什么解决办法还是不行,不可能吗?
编辑:解决方案
感谢@Rikin和@JoeClay我提出了一个解决方案。首先,下载Chrome 23版之后,我可以看到问题是polyfills(Set和Map)。
因此,安装后yarn add core-js --dev和yarn add raf --dev。
更新src / index.js
import 'core-js/es6/map'; // <-- added this line after …Run Code Online (Sandbox Code Playgroud) 我已经使用Create-React-App创建了一个应用程序,并且热重装工作正常,但是Redux状态丢失了。如果我编辑任何组件(例如,更改标题),则状态会丢失-最令人讨厌的是,由于该状态中存储的令牌丢失,用户已注销。我已经遵循了各种示例,包括(https://redux.js.org/recipes/configuring-your-store),并且无法弄清这里出了什么问题。
有什么想法我错过了吗?谢谢!
index.js
const renderApp = () => {
ReactDOM.render(
<App />, document.getElementById('root')
);
};
if (process.env.NODE_ENV !== 'production' && module.hot) {
module.hot.accept('./App', renderApp);
}
renderApp();
Run Code Online (Sandbox Code Playgroud)
store.js
const inititalState = {};
const store = createStore(
rootReducer,
inititalState,
compose(applyMiddleware(thunk),
window.__REDUX_DEVTOOLS_EXTENSION__&& window.__REDUX_DEVTOOLS_EXTENSION__()));
if (process.env.NODE_ENV !== 'production' && module.hot) {
module.hot.accept('./reducers', () => store.replaceReducer(rootReducer));
}
export default store;
Run Code Online (Sandbox Code Playgroud)
reducers / index.js
import { combineReducers } from 'redux';
import errorReducer from './errorReducer';
import authReducer from './authReducer';
export default combineReducers({
'errors': errorReducer,
'auth': …Run Code Online (Sandbox Code Playgroud) javascript reactjs redux hot-module-replacement create-react-app
create-react-app ×10
reactjs ×10
javascript ×5
webpack ×3
node.js ×2
browser ×1
ecmascript-6 ×1
express ×1
firebase ×1
html ×1
react-redux ×1
redux ×1
television ×1