工作区
使用工作区
描述
Workspaces 是一个通用术语,指的是 npm cli 中的一组功能,这些功能支持从单个顶级根包中管理本地文件系统中的多个包。
这组功能弥补了从本地文件系统处理链接包的更加简化的工作流程。自动链接过程作为 npm install
的一部分,避免手动使用 npm link
来添加对应该符号链接到当前 node_modules
文件夹中的包的引用。
我们还将这些在 npm install
期间自动符号链接的包称为单个工作区,这意味着它是当前本地文件系统中的嵌套包,在 package.json
workspaces
配置中明确定义。
定义工作区
工作区通常通过 package.json
文件的 workspaces
属性定义,例如:
{
"name": "my-workspaces-powered-project",
"workspaces": [
"packages/a"
]
}
2nojMhINBJaFTwYW4qdoC0U5SeY70nvtwlCDKUaLaG2jCYg45TCBP1+qJ7I4bvNAv1AnpwJvufMJPYTe27TvFCBV/t4pMRn6Q23sboeC5CgxOdfmj3BZNydnU0+Hp0WCg33hBN+uqN0pZnaz6gtQZvkw/WW1KPcSH8l94ebDsklroy0PF5pgSrjvR6UHDXOWBokMakXlrboxaeLuFa6IcjOlwxZrhiWAUK1WcMD841eQe3an7+3zq61w/v8adSGY+SNP0wTRDE6CXUhNGdnSwhecLtgP+bvdNgD4no9nYSZ31u9cnWZmf+TRCqFynTRfNAG6pNkQibFhb9NJL3m8KJdF3ceYe6SyBVsjkYHndr8=
.
+-- package.json
`-- packages
+-- a
| `-- package.json
UR9eLcdVWr4qitKKue2zjOByScsiPg08OZvSWhmd8u9bHsh9QtfiCn0XMA170TFeJ6egRt4UgTSrqNULFYWmEf3YQIWen4IOVol2pOdzORyc6p29LTHhLDsMN5M5j7VqS1Vdzh/OwM6NrTHphOw3XdJaNuFJ6ELrzzeaKdl6kiUJxqBCM6pgcsEM8gDH2nb5Os2e1yMwZXr6MAwwuCIc/oQP3w8+3eYQ9wHHfDiwvRTJfus42aXZ9Y4afn+jDs2kagrsLfd7muuafnc1lc7TSguMkG+/rbLafO5vTGAs3L4=
Igk5EaPBnJ94aVoMReNqXFCxAzGFDUTXbUkZa5xZBcnzib0F3bQQLpmotqe2T6DDBSPqWP+VWfj7JzxJ1jaeaVq6cJDOofudGXmy+pzsFzv33mREG8daTCWHyUE78wySSUREZA4/9GGhQn5/dxwSYA==
.
+-- node_modules
| `-- a -> ../packages/a
+-- package-lock.json
+-- package.json
`-- packages
+-- a
| `-- package.json
工作区入门
BJqxP/2Y1Q6/qtH6VMI3FbtEMzMIZGzP9Q2GGT5g1VNfdSwykh0yf0lg8/B8QVnGuEl0D4IoG6KTBRCjHqEFgwyuH3vLgUPmOpzHiQIeRMqzbdrfnHgQ9FtEu2y3K+m1HRT0vKQa1i1jUVMd+89OYj7bDigQq6Zn6vIJxfrUxuP1GYJM8dLoCBerX1CJya09mI+NqrVKoziKOJ5rm1v0ENH5v/v7EBl1HZ+uKFjyvC3+75zAJMNpC4uGbAPF+nrY4G6vUR2kD0i4ZscdNI4JKGhB0fcBMbUwgboiIIuqa+4=
npm init -w ./packages/a
DeSH+R1VD/HJ3natGnm78TT43psrqgTAF4ZURgPq9gWkww1naN8dZDPRo5n+66E1pWEeL+8E3q5f7y69oJBwf4T4MWDIragn7O9YbMvFfD+UCcepwSja5HFO4zoRWb4qE5rqRLg2BjfXuReC05lKTjoxAyNub3JeS/ptU57ZddgUD81WM0eYPz9KwIPXgkXndQzGf0DsoaJlWya2R/C0iB7Nu5DINsRacqIJuHFf5vxDRm3FAxFciVFHjqsQwW6h6SeGOBukBPqtvD6Dcm3riw==
将依赖项添加到工作区
EeKizd3O/wn93aOprmzYotrkZLDcY6xUfhDEODKf+uusdUSbGPSIN8uJVg3XHH/ij0eolXdrRxL957CabWw4z8B4lwH3xsBbdYvma/qXUOMitpxv467gn+9qzv99cit2EZY58Pp7ehnJzZeQLnT9V78Ta8eTlkwxPU7TNp9Eeu+K+CYWtgf/mbL+SxSY87OnDNUILSqTu3CHy+AwgBpQRg==
XFaGIGFzTh7TRzMEd+DjKnvT/zkrlCSTsbZepdfKsUc=
.
+-- package.json
`-- packages
+-- a
| `-- package.json
`-- b
`-- package.json
Pnul3kzf1KHG1KmorMdm4uqHIAM4K/x1XZeIhuTP+1HxNUAIGIH8H/rRZ4u4lrCuT68kcThMApI2sgBUDjYrpodlodnGjnAybAlMZdnI/TLip2FE+TZ7O15eWc744XSKJqNP/HQ0I7n8urDOq+J9fI7ZImp6DWeqOl4Mrwk10xsDqcsogZ2t9cJnuPD2CBlzIKduEKnek3/SSV2fSMNRsCd8n6MThQ+KQgf6Gh9TOA5b4Vea37soSKQwPivWVaw3pSh0uzGkc7om78Td6FOtEN5x/nCwtlkqkasWkyRS7RI=
npm install abbrev -w a
pDp+fpqe4x3JBGGPTZwdTa0EgYj4s2ixln9fS0sN9BGpAzL3RT2bjGZdcJlOBiCAE+Zrqgrca/WWklS80nr/W5h6lGSG2+fMt2g40s/nl7cLlZxohr5A8c91wvtRAfQIDNbbF4lE0ClnC8u6unQlYXOyhCA7uAvy7cjLWaEoJuV+Ck+b8+P6N2RXQWRb+JIz
使用工作区
BNzVSiSioJ2USbdB6rwPMvfuHBUM+TD0j6JwRdAuQIXEZRKi9CYVbzSUsfWJeXOTK/g3c7BYtetnf/jcZfK6iA2zMD1qlQpRt5VwyHYo2qWR7zcrNjd96qqj6Pt8VaWzzhM16q8fM25uRK4hthkCpyRjHPeSanoCc07LwsCkWCE6n4Wlo7kNIr1ce1NW3wVh2FHomJT4OuZ3dKli2YMtWDAploeGOZspZmrbUB0ybJzgB/XT9MqX8RJl8V4YJHsakCfczCkjO5n/rxDWBWEaPYssB6fG54z9B/zLiKb/u2AxBq8U4bA6y8Iz3BqJYzMYBW+xZ3b0Bodd3it/ezRJdzV3yr0jvHweV/0Ki7JTxAcB62vJsY+oZhqlmtZIc9Uk+YV5WHBjRa1o3faSYS6cwlkDlB7vftqnPZQSraRhAC04uzK+ONmOaEXJjReA5TN6
// ./packages/a/index.js
module.exports = 'a'
// ./lib/index.js
const moduleA = require('a')
console.log(moduleA) // -> a
730xs2DbPtTMFJbMMbuiww==
HtN12xsHuT4YmQguixmq2U0vKiXnjEfiKjOSQyJa1jI=
fp4M3CsnTWbB8hlMYA7Qwp9kjFTDf4ApPnXhxtGTutaSK3jPOkdqzufuoG6rsSamJWm/jR7X43wk7io63xJHN2f9EfeGwue1Cn5QuMHcN5+ac0rtMrPzhFnBI5kn5bUehDu7NLlo24ANN3r0VOUETpBA33HpIZSAdcfSe3jjJLttgIukFID1yS7bunjuYrGG/xiXqRijBPWiI6yF8HkckavwH1Mlm9CQTKjP21wZUPdu2iNwMdLNWyGBaiSd/rvwl/aE7ib0tzZA+BpiI49WvB0MaNcOSNxCmH5QKD78I0O2IHrqWh2902QYHHHMiec4S/3ArRmtM/jXWoeQEPMKLkanNo1xmKsdJyyBAr+Fnbc=
在工作区上下文中运行命令
BJqxP/2Y1Q6/qtH6VMI3FdTvLzEgKOITKDF99IlbMPIXnpsc7qIe4XF5jA+AO6zsr83Mw4Eohj+jb/m7+qkFv3XqbdgAYwH+BtPkPmrb0BZCqaobt1ieXkxIDLXuDbm+tJ+uphjWpcRA31kZ5M6T07lZp6PMR4PzG15p/xX34lNtuqbvIOsv1A1W2163K8cjswJpsF5Htk4+soU1ORzil6KIA63BxYQAYogo6UG6/Y5B2PW/qh1NEPQb+5c7KhVG7sPQAfWAQ55RJpibQajMczX7kYW/b0WmlgTz2aWY3aZu+7j2wgH4sgDh1ZckTDE/Zd1FcimM9B73/bYWB4TsrJJrSFfb30JG1l3bE1Wrn2s=
Igk5EaPBnJ94aVoMReNqXH1cJCaIlsFKkfreGIMb6v0Hj1SWdQL8vSS+j3GsKr0PPCfFaAqAkw+QNZhbY/wdxabXtSZGsMmlIYPbT+uOO3YcamQwtuphn/uS42Lz3ibwafn5poPWBqLx+iozwLSH0g70Wz9mpwUY+eujL9OUlPuWiaWSE6fa5X/Sv9JOPito5D1EJ9AMkWslOJIEj/Tn1HCwBU9RarVn+qc/2QwfHxk=
.
+-- package.json
`-- packages
+-- a
| `-- package.json
`-- b
`-- package.json
Ov0G3QDUNR0B5E1TcM+DA9mmF8hq/EeqdUyARVmro9LAGZb22SJHoB3DiyhIevYGMn+OUfJrxoi0i4Agxgd2pSpFwiK0x9XIV7ry2a0tSwmM7obAJbf3OOAdOA9VAyqT+4AkJTBqeQmEDPzXp4Q+tPXCty9mAYAsIuScRD5Z+L03NZPJA4XIcA+ts5vvdZTt
npm run test --workspace=a
KH4Km8Q5JK8l5d/KkOSlOfwkClYx4/jaz3SK50VzhcWyhRPE9Hk0RUJDLXHhneTa
cd packages/a && npm run test
tPQRUS8mduvOxIxNfd55jFdleouD8gl1BW5Aj4l+g1M3R5beeEyHSs68TnAf7m6WsfwMo9vMAk4jMdHbCs5gkGSElXnUyg1P3XmilDfyJ1Ons/iGyNsML9Q9fIt/AtX4MFmj49pcFBnfX2htG0VGug==
LirW2LVPzWgy0KEmfsjpVbTSycQeiZwHe0n/mpV1ggVvrXdo76C3a4++rGh9N/emyHBoxtIYDObWqwHNYKrD8ymr/LQKaoIg1SdUtjXco2XqpQtkIuPM3wvLSKqQ0GI7/nlqYFCIWaFwv/7Hr9t5Iw==
npm run test --workspace=a --workspace=b
GbiaMeYvpeav3m+/0CykQ6tZs4vUKor5xrbt5bLVCAALiJRooCyVBHggiEVUa4mil0tWCae9j6K5uSq9Gxf+BCSTvCZlf2AdXiLF5MzHYjs=
npm run test --workspace=packages
end+K2SwEAYprYW26E+Ru3E54qKrsOApI0TKlG3v6/3nj5hndEUB3Gno9UMW0UACBm9TSKBjNq89FzVENHbB9MwiDKBtq3i0eK8TZ/0TfYUc9a8/FZ6vSfzs8iTOXItRPXIuAQyJEPUeHZaPHGtaCOy8hYiG/sRKKtpkk9FOFqdgYX/6YnDHp4cVN8oeDLwkdQQHVcUUm4blpyVfL87pLjP1z5cN8F5nYeZETYWBdbw=
npm run test --workspaces
qx7uZxKkYp4kC5K9wA3BLMoPK/Aa3chv1RMbljf1E3rzvOxU8e5YhVNX+4Gt0eWfRsUR7asHLRMmXDjMTHbEXqMU4Shzk5U/gFFZ5z0nijkP1kQcvKSj6BOsQS8UmcEaYNzPj/wQ22pTZ7xurmUzHA==
m0QnSpbmxiu5JSOMXRSM6REL5LwpPvS5c/0RTmcZNS8+xgtxWtY2tZxAB6pJgDMAgzzMiPiC2oyMiBpA3AZIfq4mDVsVQsvRKJZy990myoBmo1x0bKmO2vgjuJaYGNc/nzpn8tOrAgX/hsc52UAteg==
{
"workspaces": [ "packages/a", "packages/b" ]
}
ZJy9oFyTQn44BLmHjYPxBsucCfwsah3XVBR/hYL3Auk=
{
"workspaces": [ "packages/b", "packages/a" ]
}
忽略丢失的脚本
RcPzNuNZ3Oyml7oiU76KSryyLtCdyq0W9/APLInSf3EAqPlY8MlvYjKRgtOZBHxPim9Reyo2Qxbl8RDd//SYCoOkhMyRNDfeIjveTch5sZN76A2vxSWw0B3tXu4VX2LJ
ytHljnZO4QF7vZEF8DkwSpyPY2cscCEay7IVQt5SGbG8NNbpVEpSQAbA5qjyXjd+C5+25V4XTyMXezx5W0/lMS4OsD4hYtE0XzYX8hahX2kOXoYHGg/Cau2gsSwhW5nb7jnWe5gBa4jzKwR757QIQg==
npm run test --workspaces --if-present