npm init

创建一个 package.json 文件

概要

npm init [--yes|-y|--scope]
npm init <@scope> (same as `npm exec <@scope>/create`)
npm init [<@scope>/]<name> (same as `npm exec [<@scope>/]create-<name>`)
npm init [-w <dir>] [args...]

描述

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

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

转发附加选项

cZwOaIgyyWBbi4uU7pvit/KTK/82y32w1Tswn0D6u+pZ+38iwUMQ5fnlEGdPd8waeA4ub6IO6bcvzuz2K//nMof1lZDdW6dIzm45RfAlQ7MGBX53lCJN47glvdqzBq+mg4oFAuatdCppyKVqinNsTumo8L4tjxas4mP0Gsd5/YnrGCB54E2ew93ni8Rg/5tWNCnm1eyQXOTbZc5JXndj2g==

boTsXKGeJdmNY537mjnNRuhqxLmONgF1J4sYlvV3y4kj28c6ufEEvVrUWUlkYOOIPyTC4ivLT1Q4zGkQ+Ez2z5GekDOsjBSA/2U0DUtJIGymD2osl8fXFWpm+bmZPLa3YXYkELPv2N4pYqLne36i11o+Po18NVwrJqFHeAQC2ImpC8TrdwOOLpMHGwb5zTPrC2r7xXrKvntRTcR/KpNfD8uO1R5nQFohVfxPCdwGnSA=

    5wYLAxA+Iy5vdiDhWO0mTMHVSq5x3foNMbiQgL5HOnEAxY65iN471EjvPUW5fVJkNXr5Rd4v3c5cCilzIhlOPuTZ4Veu/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+pX48uLveQ2Yf8SEdbfdB2V3waFG1xftM85IJ55r8p5UkFQ9CSMVuMU33+7EoCVYvywIKrwTHZVenA7B7g5NFNZ/VS3VKvNa9FzN8BaaBsjbYkw/earP6tGlpn4dJmks4dkWQ7A09RVaQ==

    fyXeAhHi99gPGIIEIKRAGAmAVb5hEkh6TfBNCzhGvuZojjeEbIEWUADb4ifTIdxQOIAJYkNu2Gdcb+rZ6c52m+GvuTF8NIfMQAjbSPbk2juECTpPYfiVXs45v5kaiKcVcGGJaiuNWfAwpKGjfHNo+jcqPOkQOyaC0GHbwgJfgzy9US64NX8vpzTTabRBzUwrs1v273JW4+OAz7wLeBVCE5a0GLfD7aba+rSqXzvvQgwnA+OIxonGuppEtsGNB5514tIaECTFMJvceIylOAV+kpTUSMUPyKdHalrL2Wg/YrRrLlXlZIbq4RDSNuNkuOeaIcHXl2VrFJNxE0+lbKa41MfIJxuQC2VsKgmgaKs/bE79MVXnI6LtFX5RdhHnVmKfd2wWAbdMpTGV5EKwVe8mQVy7+XO9zNH7d1khTp3uhsvcLKzoT65DhISO4kv3LXmWaKO66VKMPEPBGcWjRfJasUr/u2RkJlQ/rjuWXkfkSEL00g+Q9H/c1mQUBmcVx7iuWeB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