当我在react中构建我的应用程序时,我有一个错误。只有当我试图构建应用程序时,我才注意到这个错误。
当我停止dev服务器并再次运行它时,它显示了相同的错误。似乎我做了一些仅在再次启动服务器或进行构建时才显示的更改:
未找到
模块:错误:无法解决“\node_modules\htmlparser2\lib”中断更改中的“缓冲区”:webpack <5用于default.This包含node.js核心模块的填充不再是这种情况。验证是否需要这些模块,并为其配置一个多填充。
如果您想要包含一个多填充,您需要安装‘缓冲区’。如果不想包含多填充,可以使用如下空模块: resolve.alias:{“缓冲区”:false }
退出代码1错误命令失败。
我的申请是用CRA和打字本写的。这是我的package.json
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"scripts": {
"start": "react-scripts start",
"build": "cross-env NODE_OPTIONS='--max-old-space-size=4096' react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"dependencies": {
"@apollo/react-hooks": "^4.0.0",
"@date-io/moment": "^1.3.13",
"@material-ui/core": "^4.11.0",
"@material-ui/icons": "^4.9.1",
"@material-ui/lab": "^4.0.0-alpha.56",
"@material-ui/pickers": "^3.2.10",
"@optimizely/react-sdk": "^2.4.0",
"apollo-boost": "^0.4.9",
"classnames": "^2.2.6",
"formik": "^2.2.5",
"graphql": "^15.4.0",
"lodash": "^4.17.20",
"moment": "^2.29.1",
"react": "^16.13.1",
"react-app-polyfill": "^2.0.0",
"react-dom": "^16.13.1",
"react-google-recaptcha": "^2.1.0",
"react-masonry-css": "^1.0.14",
"react-router-dom": "^5.2.0",
"react-test-renderer": "^16.13.1",
"react-toastify": "^6.1.0",
"reset-css": "^5.0.1",
"use-debounce": "^5.1.0"
},
"devDependencies": {
"@testing-library/jest-dom": "^5.11.6",
"@testing-library/react": "^11.2.1",
"@testing-library/user-event": "^12.2.2",
"@types/classnames": "^2.2.11",
"@types/enzyme": "^3.10.8",
"@types/enzyme-adapter-react-16": "^1.0.6",
"@types/jest": "^26.0.15",
"@types/lodash": "^4.14.165",
"@types/moment": "^2.13.0",
"@types/node": "^14.14.9",
"@types/react": "^16.9.56",
"@types/react-dom": "^16.9.9",
"@types/react-google-recaptcha": "^2.1.0",
"@types/react-router-dom": "^5.1.6",
"cross-env": "^7.0.2",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.5",
"enzyme-to-json": "^3.6.1",
"husky": "^4.3.0",
"jest-dom": "^4.0.0",
"jest-sonar-reporter": "^2.0.0",
"lint-staged": "^10.5.1",
"node-sass": "^5.0.0",
"prettier": "^2.2.0",
"ts-jest": "^26.4.4",
"typescript": "^4.1.2"
}
}发布于 2021-01-27 15:57:53
我找到了解决问题的办法。它没有显示为测试错误,这有点奇怪。
溶液
为了解决这个问题,我将TEST_ID的位置替换为组件中的位置,然后将my-component.test文件导入到组件中:
我的组件
import React, { FunctionComponent } from 'react';
/* Tests */
export const TEST_ID = 'my-component-test-id';
export const MyComponent: FunctionComponent = () => {
return <div data-test-id={TEST_ID}>123</div>;
};测试
import React from 'react';
import { cleanup } from '@testing-library/react';
import { mount } from 'enzyme';
import { TEST_ID } from './my-component';
import MyComponent from './my-component'
afterEach(cleanup);
it('Render My component and have some value', () => {
const wrapper = mount(<MyComponent />);
const myComponent = wrapper.find(`[data-testid='${TEST_ID}']`);
expect(myComponent).toHaveValue('some value');
});分析
显然,如果您从.test文件中导入一些值(在我的例子中是一个常量),如果您试图构建您的应用程序,就会出现上述错误。
在我的例子中,我有一个组件:
MyComponent
import React, { FunctionComponent } from 'react';
/* Tests */
import { TEST_ID } from './test/my-component.test';
export const MyComponent: FunctionComponent = () => {
return <div data-test-id={TEST_ID}>some value</div>;
};TEST_ID是从my-component.test文件导入的。该常量的目的是为组件设置测试id,以便在测试中找到基于该测试id的元素。
测试
import React from 'react';
import { cleanup } from '@testing-library/react';
import { mount } from 'enzyme';
import MyComponent from './my-component'
afterEach(cleanup);
export const TEST_ID = 'my-component-test-id';
it('Render My component and have some value', () => {
const wrapper = mount(<MyComponent />);
const myComponent = wrapper.find(`[data-testid='${TEST_ID}']`);
expect(myComponent).toHaveValue('some value');
});发布于 2022-03-28 09:24:40
您需要安装缓冲区包。
npm install buffer发布于 2021-12-19 09:50:17
你必须安装这个
npm安装断言browserify-zlib缓冲区流程流-browserify util。
它会帮你走出困境
https://stackoverflow.com/questions/65922760
复制相似问题