使用yarn2驱动的monorepo,有两个工作区,common和app。我在root's typescript中宣布react-scripts和typescript为devDependencies,在app's package.json中宣布typescript和react-scripts为peerDependencies。
但是,运行yarn start会出现错误
command not found: react-scripts知道该怎么做吗?
侧纹
react-脚本有peerDependencies on react,我在根工作区的依赖部分提供了它。
为根附加package.json:
{
"name": "monorepo",
"packageManager": "yarn@3.2.1",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.3.0",
"@testing-library/user-event": "^13.5.0",
"@types/jest": "^27.5.2",
"@types/node": "^16.11.43",
"@types/react": "^18.0.15",
"@types/react-dom": "^18.0.6",
"fictoan-react": "^0.41.22",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"styled-components": "^5.3.5",
"web-vitals": "^2.1.4"
},
"devDependencies": {
"react-scripts": "5.0.1",
"typescript": "^4.7.4"
},
"workspaces": [
"common",
"app"
]
}app的package.json
{
"name": "app",
"version": "0.1.0",
"private": true,
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"dependencies": {
"common": "workspace:*"
},
"peerDependencies": {
"@testing-library/jest-dom": "*",
"@testing-library/react": "*",
"@testing-library/user-event": "*",
"@types/jest": "*",
"@types/node": "*",
"@types/react": "*",
"@types/react-dom": "*",
"fictoan-react": "*",
"react": "*",
"react-dom": "*",
"react-scripts": "*",
"styled-components": "*",
"web-vitals": "*"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}发布于 2022-07-13 15:02:27
不确定这是否是黑客,但使用https://yarnpkg.com/getting-started/qa#how-to-share-scripts-between-workspaces解决了这一问题
将其添加到根的package.json中
"scripts": {
"g:start": "cd $INIT_CWD && react-scripts start",
"g:build": "cd $INIT_CWD && react-scripts build",
"g:test": "cd $INIT_CWD && react-scripts test",
"g:eject": "cd $INIT_CWD && react-scripts eject"
}在app的package.json中更新了这个
"scripts": {
"start": "yarn g:start",
"build": "yarn g:build",
"test": "yarn g:test",
"eject": "yarn g:eject"
}PS:不太清楚为什么react-scripts在应用程序工作区中不可用,即使它被指定为peerDependency :/
https://stackoverflow.com/questions/72967617
复制相似问题