Created
October 6, 2016 06:23
-
-
Save helanan/a08263d78a437e462cbd8f92804b3fa7 to your computer and use it in GitHub Desktop.
NPM Cheat Sheet (Node Package Manager)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Why use NPM? | |
Makes it easy for Javascript developers to share the code they've created to solve particular problems and for others to reuse that code in their own applications. | |
-check to see if they've made updaes and then download those updates | |
Essentially NPM contains reusable bits of code in either packages or modules. | |
A package is just a directory with one or more files in it and a file called "package.json" with some meta data about this package. | |
NPM is a great Javascript resource for 3 main reasons: | |
1) There are lots of modules on the server side a developer can access | |
2) Plenty of commands to use in the command line | |
3) Offers front end packages for use | |
NPM referrs to 3 seperate things: | |
1)Website | |
2)Registry (a large databse of info regarding NPM) | |
3)Client (when a developer decides to share their code) | |
Upgrade commands: | |
[sudo] npm install npm@latest -g | |
Errors: | |
EACCES error - when trying to install globally, indicates that you do not have permission to write to the directories that npm uses to store global packages and commands | |
The Fix: | |
1) Change the permissions to npm's default directory | |
2) Install npm's default directory to another directory | |
3) Install node with a package manager that takes care of this for you (Homebrew) | |
Using/Creating A Package.JSON | |
1) Serves as documentation for what packages your project depends on | |
2) Allows you to specify the versions of a package that your project can use using semantic versioning rules | |
3) Makes your build reproducable which means that its way easier to share with other developers | |
-Should be at 1.0.0 (version) by default | |
-Patch release: increment by 1 on last number, represents bug fixes, or minor changes | |
-New features which dont break existing features: Minor release, increment the middle number, e.g. 1.10 | |
-Changes which break backwards compatability: Major release, increment the first number, e.g. 2.0.0 | |
Semver for Consumers: specifies which kind of updates your app can accept in the package.json file | |
1) Patch release: 1.0 or 1.0x or ~1.0.4 | |
2) Minor releases: 1 or 1.x or ^1.0.4 | |
3) Major releases: * or x | |
Requirements for Package.json | |
1) "name" | |
-all lowercase | |
-one word, no spaces | |
-dashes and underscores allowed | |
2) "version" | |
-in the form of x.x.x | |
-follows (semver spec) | |
Example: | |
{ | |
"name": "my-awesome-package", "version": "1.0.0" | |
} | |
To Create: | |
>npm init | |
-can use >npm init --yes | |
-will ask on question, author, otherwise fill in default values | |
OR | |
>npm init --yes | |
Wrote to /home/ag_dubs/my_package/package.json | |
Remember: | |
1) Name --> defaults to author name | |
2) Version: always 1.0.0 | |
3) Main: always index.js | |
4) scripts: default creates empty test script | |
5) Keywords: empty | |
6) Author: whatever your provided the cli | |
7) License: ISC | |
8) Repository: will pull in from the current directory, if present | |
9) Bugs: will pull in from the current directory, if present | |
10) Homepage: will pull in info from the current directory | |
EXAMPLE: | |
{ | |
"name": "my_package", | |
"version": "1.0.0", | |
"dependencies": { | |
"my_dep": "^1.0.0" | |
}, | |
"devDependencies" : { | |
"my_test_framework": "^3.1.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
email me for questions regarding npm: [email protected]