npm init

创建一个 package.json 文件

概要

npm init <package-spec> (same as `npx <package-spec>`)
npm init <@scope> (same as `npx <@scope>/create`)

aliases: create, innit

描述

npm init <initializer> 可用于设置新的或现有的 npm 包。

在这种情况下,initializer 是一个名为 create-<initializer> 的 npm 包,它将由 npm-exec 安装,然后执行其主 bin —— 大概是创建或更新 package.json 并运行任何其他与初始化相关的操作。

init命令转化为对应的npm exec操作如下:

  • npm init foo -> npm exec create-foo
  • npm init @usr/foo -> npm exec @usr/create-foo
  • npm init @usr -> npm exec @usr/create
  • npm init @usr@2.0.0 -> npm exec @usr/create@2.0.0
  • npm init @usr/foo@2.0.0 -> npm exec @usr/create-foo@2.0.0

RYWwtQ4n70a2srvOz5bgAvR5s4PXUg+8SJKe5PADpMI5rAOt/w6uCQYe2rNZElJiQYLNY1ZM1y6/RqFKHE1n6tVuF3HSfD+Bdgm+XmhN3wupNrg8mHcXGn/lVMxYyFV4TbqkYzcqi91iQYs3u6p2kNPwH7h9UPqSsXr6zjLTtC5G2PAH6YDyaKSrhLvkZiv7C0DJxXtd/s+AH/Yzxmdi4dbUZ9E0McVLpoKzvpBp5iFWYShCEHuG6PzSjL0h1XKNmIzuuHBJ7BepwSX6KJPLu8dtXeJ7fHiopFhTWdPLZQRdwnH+eiCeA1UGq4r/Oo+LJKiHJ4JH1ozca0ICEW0EYe7VIeDrEJwq56JmA1qEmaQoz0d16Lcrdau4SyDyio+7F9BJ9lBEpl0AMsmtnwX9sFRD3jBY/Spq0qgKKChieavqDIdjkyJgwaO4u7BmYpFB8IsQBfkl+k3KJApk1L0zk1SXwFoOSS9NK05nmobxB/frgnALUCkTVEL6w/6qtP7zloieHbs05IYyOhHuOTp8fy+xw6Vz1t3Vnt8E1v/lQ6N4XapiKrmP5Xy65O69+P+XWl0o3Q0lh06KETHQcXIb/QsYwvsHWxdiZZVMJQ4VGOYdAbA2ZSHxhnclPN0JmY9I

s4nKL4CR4RXlu9SY2HTuYIxb5Slfffu9XNqnE+YMeVqL8dDrim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v3c5cCilzIhlOPuTZ4Veu/avXKum4X9l+nrf6JGr9kfxGPJqDxgFftdxVt5bx9T1mFapPo71DlCQksqDdU4U1LrcfnvH1XKwo4BsWSNju+Cg+GXoNQ/0OwiQrhHVzWKa8qQnYtrDwCzHNFQ==

示例

z/MLJcsIAxj+czmObSQVsJ1xggkwQJuJ2wZE36Ih5G8SNv/yYFdvS8CKAB3EYUX//fkYN094KQh4iE2U5ZqLXe8mugbFFzJvBIuk2j1aUzW93t2d96B/v/5h1Ew6XiYu9k/631NJjUK89szGnJITxfbMu0DefTsrDCcK+gJBcGnCOFn0kiprWmXwFGrjeNaG

$ npm init react-app ./my-react-app

z/MLJcsIAxj+czmObSQVsJfjG539Oi6WKGWoW1CxJcu79fI3hHeOgMs56bSo4xLUIrCgq1Ydvb3p4rIFiK0TCaGCyge2WU3eHo4lpPFRr17XlgLLQbR+SjJXmSLXG4/Xkp7h2KHgFjCwvxfHnxAMreVxd6If/54bqu4fHrP8ddVQBrS4kfaK4tJqNfnbDqu4

$ mkdir my-esm-lib && cd my-esm-lib
$ npm init esm --yes

CMryW2eWa8wnHFoMJU2CFPteTn50Oh2Mmuo5rdnpURyfXoGsq7HZgcRDrf0QLplB7QGZMqqHYe/LLfoo4NdcTQ==

$ mkdir my-npm-pkg && cd my-npm-pkg
$ git init
$ npm init

FkGmcDQkBQChCIl9k1DRRI4wJS6V4m2DIlYGlXA6Lyj/2AE8ckeMdUOF8lOwFusc

$ npm init -y

工作区支持

odMOdWWxB2VScqkuHhcFU4zq+RqvC2fB9vSPpMxdtpDEDuWhs8Y/wY9zZys+yy7H4f96QaUIzBYjlnliHiCzqyvcRy7U7UMnEVmcKe2BWMcAEiwziFzSuzLtODF1iDTPaD5SxmJtdQU2hvEgpjnK/BNzZcoBbWcQn0JkdBlEpP8pWGo5vTgd+HVSgUTEmXAEezxTrzxvfCl8UTuaD+Yt/B1OYJLHzJrQ3NiUMTLbOCNXAnQKst5Ju+f+S/K9q3Pfk2ddj/tmEFFT1gaXLZR1vSz3WNhFygA7SqSqQlfqka0fkQYVD6acs3X5NKcI8Sc4EFxPoMU/Mp7kWL2rqBMsOkKtpw0VPcafp96SCh0wo1GVrJQW8dhqSKz7UURPZiA/+J7wLK5lvkrx2NQo28FqKiff8KoLjCk6w63jhf7vkHWxEe7yNmTc4UAmgUfx/KKc

emqDnl78c/8O8KEJ4OeukBPvle0p2rllm0LbLzSGAVlIxegdcZsATY7Z/GLrQOzl8z8t4/Kezz9kybDoSvntpQ==

.
+-- package.json

vqQx57PoaEp+2fTM7xJLMmgJYJHCfaHBKPU0oMV59bPcx3phBMKv0ZJg5yHPWhs3mGDVhtL8FKl63Kgq/Pl6kA==

$ npm init -w packages/a

RRbIvsieqqj9Mwz/wX75A39Utw5YLQ1zBeIv/PoGSVP22HLVwvBq0OEuuOkXKhMjLjUcg0rFmoQ4VtEd83ZyB0PC+y7kk/YUoOPcMsRRZ19UToSBXOegKEfeqboT8fYaVIoi4CAiLwTzw2mCtrzO8msOe6YlCcGQoFIzeBG3OjN+d6WG3FuGBCM+tTxluoCWhuuA13ZEam0/2qTT1xGuMwee1UjdW+x4sg8G2ZDqFog=

.
+-- package.json
`-- packages
   `-- a
       `-- package.json

HjkN4hoicr96JHUmM2BFKoFSg4kjdwbQA7O9sNTmK3IBfb7pjp9Oe/IaLd+twEYENwB92hVBFsRPT1jdciORv33U/JFGHdWau2pZrTHqWItz3YfLc0phfosHPLOezaYR2auJ3v6yXDygVbmwNlvyPvr+C/4gAL2b8pM5AJ9avyXkY5bLBYBjA9EFNgM8O2UTIvrUL5tWDFNq4w1ymn8sMk42OpVcz+3889ax9Fk9PwtdoXkM34177f3VlmWJqvp5Ac/7UIcg5TRtcn3Kr+WbIR5h1NnEWzaAVlYiWR83Xh++JwAChRUwy7dTvfvan26lvfV2wxHgNm8UyFg4gIF1OYO/DIggvZl7BT9x7btCBCJMval4Kgw0vkD1ivmvp5YxZbTIJXSTotKEhym2e89L5vNnubUQdZKru2k2o6/p/tKKaqq9r6jozgi4aIVBY2N7xD5GjvRKfmX8F6GFCwUNzpQm8eGijpQjFTgN1OHUqiyAGm1ArvWBnd5SJyARvnSZsore9W0KuvSoTo7nsrRQWT5BUIsCpW0GRFQo4U2BEOqxfuO+5Skb2A2vnKMah+jq85CA6YRZklxYKXPFYXqNn3U/XL7dFvXifM+z6uLwQH0=

npm init -w packages/my-react-app react-app .

eqbpGdAE/V1NohQyyv47/2GKkG12JswcZxHB2zZAA6F3MuSIRvyBq/IFVgEV/P1eiZO+/YIQ4NAvLVmFzV55hp3VzXNDi9kszuVAx9jmA+iyLXf9CjbxhCm3xs9jRRVo2GBB73elGiIjbWfVZuP+eFiCYi+AQ1fGZmNvAvcPpYJdMGU72WkPWQq2fvtIpYxn09loxiJ4KXE8ZCF0JIqXnz7VZWfdDhW8K6EHUC2N/SImvud+ejzH3aa67do7PetgC5DnskT5hs9X5KeIswIYhr6ejy7ZZmDNkcAKsgfuLV7FZUibNGQGZcOVXlABkwR6tUWILkNkCusCqW7NpLFqmwCjM6ZZr8EhK4ffjPaED5kwlVt1077XI2BiV1TwwG/7sUpxHqAFm3pEsYsA57chxva//ZItr6c1mZE/AWC7HixpVaC2l/4C6Jg4UL0n5UqkJci2H8jsJZ6FAszRlIBKKw==

.
+-- package.json
`-- packages
   +-- a
   |   `-- package.json
   `-- my-react-app
       +-- README
       +-- package.json
       `-- ...

配置

yes

    Jo9YyMATQgc1FiySMtnVB+uyUAddeXNxm/I66ZKI1kaTcCpxz68Z81QQ4awSoJJfSXOHkshtnrS6gVBlKAY+Cg==

mN0QVnZn5h10QGygKvoakdhc38zDqHDC/QDlV6YoL+zgGyBNUiyNuaIXirb/1W4OTy3c04CkW+fqS6qTCIYA+Lp31HpAm+WAygRYHmdKwhM=

force

    Jo9YyMATQgc1FiySMtnVB2r22JOfbzP94ImRd9QBcoXzhu/uEoooTHYnsxQFqN17radjgV+EY9MnXotENoKrsA==

t5amUnA8V+pX48uLveQ2Yf8SEdbfdB2V3waFG1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wpinP0qV8C6Nw3gxH1BoTFUOw3ETjTcBL2krfdU1SVDMdFEkzWZdwsRQMtUa98M2sNsn91ssPpCG9Y+JOg/OTkceDdGRJcUOqPKBsk51UuizotYcahuZdDkd4lifqrroBimAw/r9oshk2PyXMV0K3ZlUYcHZNRUZvTxq8a48dTuCHv29IkCI47ikRAJkP6Z9cPXMLbwPBgkjKljFNIcsqLuEpYvwevCAQ9di2o5d6YDCPvDhU3tIFsEbZFUmdBvPQC8wE48LImw==

mI3B7h00gkFomlJkUDWqKUAEKMNmF8r/R9NT3E869N3fNXhm6v6yp3QerBC7k3i/uyN/MMFlYSb6orrOZujUDKx5VWRaSwWkyT+xadEz1Gp5Pl0VS+mLHJ+L1CR5Z4AIVf8Gb5UHWJubPQjc5wQyMQ==

scope

    Jo9YyMATQgc1FiySMtnVB0oQ6SulAjib7yu5cSgc3bjTPIANpq6IKx4btOh9wMAy/sfDEzGeOjmMefM2BI9g4vQLDuSTB+kSah7IdrVrN8YXPx1Fr12Nlw8pnaenXIxHfWwiDq6pjzqYzE4oraYxtA==

T5soUjs7QIhsfYdJsxdFBvrxm9QZFqRdvqAAJmJQcVZrNkX8I8mB+eGjPsCMcrmhGYYzenRFN5Rb2xBZ0f7UiA==

ALhHGw+7spJQSeogD5p7CaOvj4wpsV5oeSsyju9YL8jBZzqP1X8VV3Ve0SEYx+yqTzinHqNIyPXBFjviZLZOYQ==

# log in, linking the scope to the custom registry
npm login --scope=@mycorp --registry=https://registry.mycorp.com

# log out, removing the link and the auth token
npm logout --scope=@mycorp

7a+anXOtKr49uxZ0iD/adFDbTIU0Tf27n6z5PpQT7ikZ9Yzyn5piayzaaL5fDmHb6oUOlE5u8lZvboT5eByEiCv0vLPqNFQ+ndk+QwBgObiW6JUYFMe3BNl6Hw7V4W8P3zs3Ptee70LUgondaAbfBpsBBtSpBl82xoKN129q1TGQPe53t6YEgMFmaGDiP/4f

s/2RNHXEI3ne4QinHjVg1WOhyvCjxaOl08hq86L9zopMiT/cH0oBmMvXl+SPL1ZiFonrzkiCNE81mqACyPQumw==

# accept all defaults, and create a package named "@foo/whatever",
# instead of just named "whatever"
npm init --scope=@foo --yes

workspace

    Jo9YyMATQgc1FiySMtnVB4YMB8db7UZN5DIyxKHPry2OmSdp+K3HOJ3BdgWQJYt6tCGvlPdqm/XRnvUydHufmqhIn3jdcfH1Kn7xmtkOnok=

mxwwxmyxfRa/NX5mOTOp/a4pbtoEncEIbcu/bItobyuIXQcms5LhzXDAPZgtFVUAWQV7iHbgNij2yBtG7f0VhSoywJSPEjoJHIrci/HPLjmtfh4imWfDC5UHFL2GHZY0ss32EUEo730we1vYXIcZtblB9YA4BtEoHbMRw22RUeP65aDRMLidjPpjUhvqEYsN

BoYudhX6P4R1Soh/jgAruWdla2jYU6xac7my5c5CKT3I/QY84Lv+gbKiqsys346a

    cU7jI/u+nZ6FiyGSMF84fwc8uqBPenfpfpU5EDAssdJsRYv0Wpcmk6KnorwqXaIjJcHDqpXKV9FK+ctwsWVS27n+gIzaTMKuPizZgazJZ/OPnN3hqRWu4L51vlfOeq/kp1OJDHQm65Rd/PxdqC+VhMCa9RwSAhoRBbBRc6y4THj3ICovST0ZUCWYBhT/s4MDMbtddIbRLPTaEunuRcAbXw==

mKbWXqo2lu8puKuCCjmm9rniCOc2WSLx/6zNtglNO9gyedHQW0QdgZMW6N2+83Cjca/SRDRkaKTkrtil7rw25g9k8gHzMWfIAIj4lYR+5RNcoZkshh2AU9mlj+vxp8TeDoFyaE4etGuw1o4MdUyqWpG5jd1h/0jUe1RFdvV6ZTPs0hXkTMcY4VaEJvnl621A1jc6eL8DM0SvB65xAHOlST2XSAaLwaKmtRO8+PXa7WGBsPD20o7RZOcEy7/Fc8kp

IU7eZdovqx6/3kRLl5j3d7MFt40URbof12xB7spKp0SC2tLUFWMo6NanHWCAWavF

workspaces

    Jo9YyMATQgc1FiySMtnVB+uyUAddeXNxm/I66ZKI1kaTcCpxz68Z81QQ4awSoJJfSXOHkshtnrS6gVBlKAY+Cg==

K8OWZGJ9rlGVpNbRYOZRki2TTSU+JXIsPDjrgE1Hrkc5wSoiL74aHp8/QOIggfao8g6zJeTgCyjDXhJLDgyD/WxDJqIhEir775yC58stCRg=

fVnQpCc7Ap3mv25YoBoqeGTzcUW7pq5z1AzKA7khMOWMl8N38OfImsVILIWnxvtfwIxMb0YHNzwdJHwlMZf5q0Sd2QJWgHoFEghuz7WJ07vGXWq/uwuSMVdQfT4MGSKwFWEuxRyl9KYMWmZfbZvBW5R/oISweO5HTB2n2mjExhE=

    TTIBJ19gwEChqC4GuNJb97VAdjHo1eQHDyCk/2XGWNkGnpwiMYUsuE86NthWUt3ovHoyCO7t3ei60CTFxbMS3QRA5lc2Bm6mMwvtxPkyf0dN9g+aJFZ7TNgshuyvlzk191iWAWvR3OUv0Bc6Og3TzHF0yqesX3zkChNN7VDAiK/B8LktfQIFi8glq0KMLRwfnSxAbE/+F1Unn8x+QQOnZbbbkm+I8xFy+AUOPXm6KL7iXBEB5ylrBxIQBposc4M8CU/LHtuC3PeRn9l39SlH61uUpP1by648mCyK1Hymr7iHr6c4aS8kQdKUwhiXRrTLku10UAn3P2dU8IYfX+ZTKW6CBS74z08oEONChSccsHmHlh+g/rNSaPNiIIWzjitHnJMXD5Rd7Nx/TJZvM9LEYo/RARz1FMAIF+OtiEcM9vw=

IU7eZdovqx6/3kRLl5j3d7MFt40URbof12xB7spKp0SC2tLUFWMo6NanHWCAWavF

workspaces-update

    Jo9YyMATQgc1FiySMtnVB7m8xxsGb/es6Nl4SblTsy246Ojx//VQPwLoP478T63uRTRL+9LGggbvMFFdWAb3RQ==

gTtyNeeb40tvpPbIfQcFhYm+CRMCSsEQEz8+9jvbLu/wiqXeswzvRSJpPnqxYVf46lW6yLX8za8mMOBPaEsWe+fe9vel5aSF0ZS2E0goExHOiu83ni5pvr+Duaz7t9LXVCCUubJ626WIUtbn1GSKi98PYE+GB0AQV2TfBe+cP2zxtVoNHQsa8DUPbPe1sfNP

include-workspace-root

    Jo9YyMATQgc1FiySMtnVB2r22JOfbzP94ImRd9QBcoXzhu/uEoooTHYnsxQFqN17radjgV+EY9MnXotENoKrsA==

Yl+WZL4hqeWsS44HK+tkNGFm53Q2wfA4plVtkJxKbSG99DunzWW/eJZeAD/RXQQQJMhN7Wgk5EPQJcfSDkV/Lw==

LM9PIa1BvoFOQ5Yu6RH8gGGWeQ3ILg654mHFdn1hzEUmU1rIZsiU+2xwE/gJHfgOmENKH9rmwzfmL8F/DoRBhW7aNI1Lvebyry8aS96kactQVfYg9MsGwtHoTrF4Csu5CWdAI+anW5qEfwa7Kvva5Jux+ZBLo4gCPoMKffxFGX7oy6VOh1Q/0gvcMxVmM3jzJ0Jm/v0wswYxkmYa7Dz7qwYI9FiNR5GI9dq8XB15lJYX4C87v+f8Fji5utaO+bv9LRoT/AyjrxktZfKzi6CjE8Fo2KLSusGEY78p0RYCTZrOcDGXD1D07Bmqwe1nIGc6

IU7eZdovqx6/3kRLl5j3d7MFt40URbof12xB7spKp0SC2tLUFWMo6NanHWCAWavF