在JavaScript中安装和更新依赖项

安装和更新JavaScript依赖项



再一次问好!一篇文章中,我们开始研究在JavaScript中管理依赖项的过程,梳理了以下基础知识:什么是npm软件包,软件包清单是什么样的,在其中写入依赖项的字段以及原则上什么是依赖项树以及语义版本控制(semver)的基础。如果您错过了上一篇文章,那么我建议从此开始。



semver, , .



npm shell autocomplete



npm . , shell.



, :



  • Bash:



    npm completion >> ~/.bashrc
    source ~/.bashrc


  • Z shell:



    npm completion >> ~/.zshrc
    source ~/.zshrc




shell- . , : npm smth… [TAB], shell .





, npm ( package.json). , npm init. .



: npm init -y, . npm , .





npm init, (npm initializers). boilerplate-.



: npm init <initializer>, <initializer> — (: esm react-app).



— npm- create-, npm registry . , .



React- create-react-app: npm init react-app -- my-react-app. CLI, npm init , . React, , , , , ( : my-react-app).



registry. , — npm registry. .





, : dependencies, devDependencies, peerDependencies optionalDependencies. : npm install <package-name> : npm i <package-name>.



: npm i lodash.



lodash dependencies .



: npm i lodash express passport.



install :



  • -P, --save-prod

    dependencies ( )
  • -D, --save-dev

    devDependencies
  • -O, --save-optional

    optionalDependencies


peerDependencies, . . npm . , dependencies npm install, peerDependencies, ( IDE npm).



, , npm , . . : ( Git).



npm install , . . : .




- , «@»:



  • npm i lodash@3.9.2
  • npm i lodash@3


. .





, , , git clone?



npm install npm i , npm , .



:



  • --only=prod[uction]
  • --only=dev[elopment]


, , , Node.js, .





. npm : npm ls.



: npm ls [<package-name>], <package-name> , .



, ( , ):





npm ls .



, «» , : npm ls lodash.





lodash npm ls lodash.



, npm ls depth. :



npm ls --depth=0


, .



dev prod , dependencies devDependencies:



  • npm ls --dev[elopment]
  • npm ls --prod[uction]


json , npm , . ( ):



npm ls --json




npm (semver). : , 1.2.3 1.2.4 (patch update) 1.3.0 (minor update), , . . semver . 1.2.3 2.0.0 , (changelog) , , , , .



, semver , , - patch minor ( , ).



, , , , , ( ).




semver . , npm : patch, minor .



package.json :



{
  "dependencies": {
    "lodash": "^4.17.17"
  }
}


«^» (caret, hat «») semver. , lodash , 5.0.0, . . patch minor , .



, semver, npm, 99% ( npm ), . . .





, , . , . . , :



{
  "dependencies": {
    "lodash": "4.17.17"
  }
}


, lodash 4.17.17, , .



:



  • , , , — ,
  • , , ( ). , , .


.



, — ( ). , . . , .





, , , . npm outdated .





npm outdated , .



:



Package
Current
Wanted , semver
Latest , ( )
Location


npm outdated , , depth , npm , :



npm outdated --depth=10


:



npm outdated --depth=9999




npm npm update.



npm registry semver . registry ( semver), , , . , semver, . . npm update , .



: lodash: ^4.16.4. npm update , 4.17.19, : ^4.17.19.



npm outdated, npm update depth , . , :



npm update --depth=9999


( ) . , .



npm-check



npm outdated npm update npm-check.



:



npm i -g npm-check


, , . , semver, , . : - . !





npm-check : , .





npm-check, : , .



npm-check , - :





npm-check , lodash .



( ) .





, . npm uninstall : rm, r un. , :



npm rm lodash express


, .



Workflow npm-



, , workflow JavaScript-. , , .



  • :

    • npm init

      ()
    • npm init -y

      ( IDE)
  • :

    • npm install <dependency>
    • npm install <dependency-1> <dependency-2>…
    • npm install -D <dev-dependency>…
  • :

    • npm outdated

      ( )
    • npm outdated --depth=9999

      ( )
    • npm update

      ( semver)
    • npm update --depth=9999

      ( semver)
    • npm-check

      ( )
    • npm-check -u

      ( )
  • :

    • npm rm <dependency>




, , . semver .



. , !






All Articles