标签: create-react-app

“使用 JSX 时 React 必须在范围内”(react/react-in-jsx-scope 与 index.js 上的“window.React = React”)

我正在关注 O'Reilly 的“Learning React”的第 5 章“React with JSX”。

我编写了 Recipes Appcreate-react-app作为基础。

索引.js

import React from 'react';
import ReactDOM from 'react-dom';

import './index.css';

import App from './App';
import Menu from './Menu';

import registerServiceWorker from './registerServiceWorker';

import data from './data/recipes';

window.React = React;

ReactDOM.render(<Menu recipes={data} />, document.getElementById('root'));

registerServiceWorker();
Run Code Online (Sandbox Code Playgroud)

菜单.js

import Recipes from './Recipes';

const Menu = ({recipes}) => (
    <article>
        <header>
            <h1>Delicious Recipes</h1>
        </header>
        <div className = "recipes">
        {recipes.map((recipe, i)=>    
            <Recipes key={i} {...recipe}  />
        )}
        </div>
    </article>
);

export default …
Run Code Online (Sandbox Code Playgroud)

jsx reactjs react-dom create-react-app

14
推荐指数
2
解决办法
2万
查看次数

玩笑+打字稿+绝对路径(baseUrl)提供错误:找不到模块

我正在设置配置以在create-react-app + typescript应用程序(我从中弹出)中运行测试。我正在使用笑话+酶。我在tsconfig.json中进行了设置,baseUrl='./src'以便在导入模块时可以使用绝对路径。例如,这是我的一个文件中的典型import语句:

import LayoutFlexBoxItem from 'framework/components/ui/LayoutFlexBoxItem';
Run Code Online (Sandbox Code Playgroud)

您可以看到该路径是绝对路径(来自/ src文件夹),而不是相对路径。当我在调试模式(yarn start)中运行时,效果很好

但是当我运行测试(yarn test)时,出现以下错误:

 Cannot find module 'framework/components/Navigation' from 'index.tsx'
Run Code Online (Sandbox Code Playgroud)

因此,尽管我在tsconfig.json中进行了设置,但看起来jest无法解析此绝对路径。这是我的tsconfig.json:

{
  "compilerOptions": {
    "outDir": "dist",
    "module": "esnext",
    "target": "es5",
    "lib": ["es6", "dom"],
    "sourceMap": true,
    "allowJs": true,
    "jsx": "react",
    "moduleResolution": "node",
    "rootDir": "src",
    "forceConsistentCasingInFileNames": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "suppressImplicitAnyIndexErrors": true,
    "noUnusedLocals": true,
    "baseUrl": "./src"    
  },
  "exclude": [
    "node_modules",
    "build",
    "dist",
    "config",    
    "scripts",
    "acceptance-tests",
    "webpack",
    "jest",
    "src/setupTests.ts"
  ]
}
Run Code Online (Sandbox Code Playgroud)

现在,我可以看到 …

absolute-path typescript jestjs visual-studio-code create-react-app

13
推荐指数
12
解决办法
6795
查看次数

如何为我在Fedora上运行的虚拟机分配更多内存,以避免堆内存错误

我在Fedora虚拟机上运行Jenkins,并有一个由create-react-app创建的应用程序.

当我尝试在我的本地机器上构建生产时,大约8分钟后,它确实被编译成功(尽管有消息:'捆绑包大小明显大于推荐...'

但是,当我在Jenkins构建过程中运行相同的脚本时,出现以下错误:FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory.

构建脚本如下:npm run build-css && node --max_old_space_size=8192 node_modules/.bin/react-scripts-ts build && npm run copy-to-build.

我的问题是,如何为在Fedora上运行的虚拟机分配更多内存,以便脚本可以在抛出之前成功运行FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory.

linux performance fedora jenkins create-react-app

13
推荐指数
1
解决办法
200
查看次数

使用create-react-app创建新的React组件的最佳方法

我正在使用create-react-app。是否有命令创建用于支撑文件的组件。

现在,我正在手动创建文件。假设组件名称为Button,我在组件文件夹内创建一个名为Button的文件夹,然后创建Button.js和Button.css文件。

是否有任何CLI命令使用create-react-app来执行此操作?我知道有一种方法可以执行此Angular CLI,但是对React CLI一无所知。

javascript reactjs create-react-app

13
推荐指数
3
解决办法
2万
查看次数

创建 React App V2 - 多个入口点

我正在尝试构建一个带有 2 个入口点的 React 应用程序,一个用于应用程序,一个用于管理面板。

我从 Create React App V2 开始,并关注这个 gitHub 问题线程https://github.com/facebook/create-react-app/issues/1084和本教程http://imshuai.com/create-react-app -多个入口点/。

我正在尝试移植从 CRA V1 添加多个入口点的说明以在 V2 中工作,但我想我遗漏了一些东西。

弹出 CRA 后,这些是我更改/添加到 path.js 的路径:

module.exports = {
    appBuild: resolveApp('build/app'),
    appPublic: resolveApp('public/app'),
    appHtml: resolveApp('public/app/index.html'),
    appIndexJs: resolveModule(resolveApp, 'src/app'),
    appSrc: resolveApp('src'),
    adminIndexJs: resolveModule(resolveApp, 'src/admin'),
    adminSrc: resolveApp('src'),
    adminPublic: resolveApp('public/admin'),
    adminHtml: resolveApp('public/admin/index.html'),
};
Run Code Online (Sandbox Code Playgroud)

我已将这些入口点添加到 webpack:

    entry: {
        app: [
            isEnvDevelopment &&
                require.resolve('react-dev-utils/webpackHotDevClient'),
            paths.appIndexJs,
        ].filter(Boolean),
        admin: [
            isEnvDevelopment &&
                require.resolve('react-dev-utils/webpackHotDevClient'),
            paths.adminIndexJs,
        ].filter(Boolean)
    },
    output: {
      path: isEnvProduction ? paths.appBuild : undefined, …
Run Code Online (Sandbox Code Playgroud)

reactjs webpack create-react-app

13
推荐指数
2
解决办法
1万
查看次数

Jest - SyntaxError: 不能在模块外使用 import 语句

我在jest:24.9.0没有任何配置的情况下使用,从 create-react-app 全局安装。在这些文件中,我使用了 es6 模块。使用时没有错误"test": "react-scripts test"

但是,当我开始使用jestwith 时,"test": "jest --config jest.config.js",我看到以下错误。

 FAIL  src/configuration/notifications.test.js
  ? Test suite failed to run

    /var/www/management/node/src/configuration/notifications.test.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import notifications from './notifications';
                                                                                             ^^^^^^

    SyntaxError: Cannot use import statement outside a module
Run Code Online (Sandbox Code Playgroud)

javascript reactjs jestjs create-react-app

13
推荐指数
2
解决办法
4432
查看次数

create-react-app error import type * as PrettyFormat from './types' in Pretty-format

这是 cra 的全新安装,在节点 13.11.0 上运行打字稿模板,尝试运行 npm start 时出现错误

TypeScript error in D:/Projects/personal-site/node_modules/@types/testing-library__react/node_modules/pretty-format/build/index.d.ts(7,13):
'=' expected.  TS1005

     5 |  * LICENSE file in the root directory of this source tree.
     6 |  */
  >  7 | import type * as PrettyFormat from './types';
       |             ^
     8 | /**
     9 |  * Returns a presentation string of your `val` object
    10 |  * @param val any potential JavaScript object```
Run Code Online (Sandbox Code Playgroud)

typescript reactjs create-react-app

13
推荐指数
2
解决办法
5101
查看次数

如何使用 create-react-app(无弹出)导入共享的打字稿代码?

我正在尝试在非弹出模式下的 create-react-app 中实现 TypeScript 代码共享,但我遇到了臭名昭著的限制,即src不允许在外部导入:

You attempted to import ../../common/src/lib.ts which falls outside of the project src/ directory. [...]

对于非 TypeScript 的情况,这里已被询问并回答,但我无法获得任何使用 TypeScript 的解决方案。具体而言,所提出的解决方案存在的问题是:

  • 设置baseDir在ts-config.json:在这里创建反应的应用程序内抱怨:你的项目baseUrl只能被设置为src或node_modules。Create React App 目前不支持其他值。

  • 基于 react-app-rewired 的方法:更有前途。禁用ModuleScopePlugin让我过去了“在 src 之外尝试导入”错误,但现在的问题是打字稿文件的加载器无法正常运行:

    在此处输入图片说明

    我已经验证它.ts本身没问题:./src从那里复制并导入它工作正常。

    我还增加了../../common/src文件夹添加到includes列表中ts-config.json。

    我的猜测是 webpack 加载器配置以某种方式有一个规则,可以防止 TypeScript 加载器转译与其预期路径模式不匹配的文件。如何使用 react-app-rewired 解决这个问题?

  • 符号链接源也不起作用——同样的问题。可能是因为 webpack 在内部解析符号链接并在正常加载器规则不适用的路径中看到文件。

  • 基于弹出的解决方案:我现在不想弹出,但我尝试过,基本上又遇到了同样的问题。

我还发现了其他听起来相关但没有回答问题的问题:

javascript typescript reactjs webpack create-react-app

13
推荐指数
2
解决办法
5301
查看次数

无法使用 docker 容器运行 react 应用程序

我有一个 react-app,它简单地显示 hello-world 消息,但我喜欢通过 docker-container 运行该应用程序,但遇到了这个问题。此消息后,进程停止而不运行应用程序..

? ?wds?: Project is running at http://172.17.0.2/
? ?wds?: webpack output is served from
? ?wds?: Content not from webpack is served from /app/public
? ?wds?: 404s will fallback to /
 Starting the development server...
Run Code Online (Sandbox Code Playgroud)

无法理解我应该做什么,因为我在 Dockerfile 中有一个带有基本代码的非常小的应用程序

FROM node:alpine
RUN mkdir /app
COPY . /app
WORKDIR /app
COPY package.json ./
RUN npm install
CMD ["npm", "start"]
Run Code Online (Sandbox Code Playgroud)

我是否需要安装 webpack-dev-server,我试过了,但出现版本错误,例如“手动添加的服务器”的版本低于已经服务器的版本。所以我重新安装了 webpack-dev-server。

我已经用“create-react-app”创建了应用程序,所以我认为每个依赖项都是自动管理的..有谁知道,我该如何解决问题..提前致谢(顺便说一句..)

我用来构建的命令: docker build . -t lucki

运行镜像命令: docker run -p 3000:3000 lucki

这是项目结构: …

docker reactjs create-react-app

13
推荐指数
1
解决办法
7331
查看次数

npx create-react-app 每次都花费太长时间

我是 React 的新手。每当我使用npx create-react-app 时,下载诸如"react, react-dom 和 react-scripts" 之类的所有包都需要很长时间。是每次都会发生什么,还是有什么我可以做的事情,这样每次都不会花费太长时间。

reactjs create-react-app

13
推荐指数
2
解决办法
1万
查看次数