Skip to content

Instantly share code, notes, and snippets.

@Anna-Myzukina
Last active September 10, 2018 04:49
Show Gist options
  • Save Anna-Myzukina/77c77cdc3d543e254bb6d19a1c09b057 to your computer and use it in GitHub Desktop.
Save Anna-Myzukina/77c77cdc3d543e254bb6d19a1c09b057 to your computer and use it in GitHub Desktop.
how-to-npm
registry = http://localhost:15443/
userconfig = /home/anna/.config/how-to-npm/npmrc

how-to-npm

A module to teach you how to module. PREREQUISITES To use this project, you'll need NodeJS. Visit http://www.nodejs.org to download and learn more!

USAGE npm i -g how-to-npm how-to-npm

00 Install npm

The first thing we're going to do is make sure that your npm version is up to date.

 Run `how-to-npm verify`

Some helpful commands:

 npm help ............ Get help with npm
 how-to-npm print .... Re-display the current exercise
 how-to-npm verify ... Verify that you have finished an exercise
 how-to-npm solution . Show the solution for the current exercise

01 Dev Environment

1.mkdir <name directory> -make a new directory 
2.cd - into it
3.npm init- to create a package.json file (You will be prompted
 to answer a number of questions. You can just press enter to accept the
 default for each question.)

02 Login

1.npm whoami-To see who you're logged in as
2.npm adduser-To create your account

03 Start a progect

1.npm init --scope=<username> , and replace <username> with the user
 you created in the last lesson.

04 Install A Module

1.npm install @linclark/pkg - install module @linclark/pkg

The first thing that most people do with npm is install a dependency.
 Dependencies are fetched from the registry, and unpacked in the `node_modules`
 folder.
 To install a module, use the `npm install <modulename>` command.

05 Listing Dependencies

1.npm ls -shows you what you have installed (your dependencies)/If there are
 any problems npm will alert you by returning an "!ERR" message

06 npm Test

 1.create a file called `test.js`
 2.edit your `package.json` file to make your scripts section look like
 this instead:
 "scripts": {
     "test": "node test.js"
   }

every module and project will have a test script that runs to make
 sure everything is good.  In order to help remind you to do this, npm
 puts an "always failing" test in there by default /If you wanted to actually run any tests you'd written in `test.js` with the "test" script, you'd run `npm test`. The docs for npm's scripts property can be found here: https://docs.npmjs.com/misc/scripts

07 Package Niceties

1.create a README.md file, with a few words in it
2.add a "repository" field in your package.json file, with a url
 where people can access the code(You can edit your package.json file by hand, or run `npm init` again)
 
If you type `npm install`, you'll see
 something like this:

     npm WARN package.json %ID% No description
     npm WARN package.json %ID% No repository field.
     npm WARN package.json %ID% No README data

08 Publish

 1.npm publish

it's very easy for all npm users to publish their modules and share them with the world. Packages get into the registry by using the npm publish command

09 Version

 1.npm version - Update your version number

Every package in npm has a version number associated with it. As you release updates to your package, these updates get an updated version number. Version numbers in npm follow a standard called "SemVer". This stands for "Semantic Version". The specification for this standard can be found at http://semver.org.

 The tl;dr version is that for a version like this:

   1.2.3
   ^ ^ ^
   | | `-- Patch version. Update for every change.
   | `---- Minor version. Update for API additions.
   `------ Major version. Update for breaking API changes.

npm has a special command called npm version which will update your package.json file for you, and also commit the change to git if your project is a git repository. You can learn more at npm help version. Or, if you don't trust the machines, you can open up your package.json file by hand, and put some new numbers in the "version" field. The npm registry won't let you publish a new release of your package without updating the version number! Ever! So, get used to the idea of bumping the version whenever you want to publish, even if the change is really minor.

10 Publish Again

 1.how-to-npm verify

11 Dist Tag

 1.npm ls - To find out the name of your current package/version
 2.npm dist-tag add [email protected] beta - will add a new tag (npm dist-tag add <pkg>@<version> [<tag>] )

12 Dist Tag Removal

 1.npm dist-tag ls  - Show all of the dist-tags for a package, defaulting to the package in the current prefix
 2.npm dist-tag rm new-directory test - Clear a tag that is no longer in use from the package(npm dist-tag rm <pkg> <tag> )

Let's delete all the tags that we can, and also point "latest" at something other than the most recent release

13 Outdated

 1.npm outdated - This command will check the registry to see if any (or, specific) installed packages are currently outdated
 2.how-to-npm verify PKG-  where `PKG` is the name of the package that is out of date


 each release is a unique combination of a name and a version, we can detect compatible releases
 programmatically with the `npm outdated` command

14 Update

 1.npm install  - up to date 

15 Rm

 1.npm rm - Remove all the deps (or npm uninstall)you can also use `--save` when removing packages, to also remove them from your package.json file.

16 Finale

 1.how-to-npm verify
0 info it worked if it ends with ok
1 verbose cli [ '/usr/bin/node', '/usr/bin/npm', 'whoami' ]
2 info using [email protected]
3 info using [email protected]
4 verbose stack Error: this command requires you to be logged in.
4 verbose stack at EventEmitter.whoami (/usr/share/npm/lib/whoami.js:41:18)
4 verbose stack at Object.commandCache.(anonymous function) (/usr/share/npm/lib/npm.js:216:13)
4 verbose stack at EventEmitter.<anonymous> (/usr/share/npm/bin/npm-cli.js:74:30)
4 verbose stack at _combinedTickCallback (internal/process/next_tick.js:131:7)
4 verbose stack at process._tickCallback (internal/process/next_tick.js:180:9)
5 verbose cwd /home/anna/new-directory
6 error Linux 4.15.0-33-generic
7 error argv "/usr/bin/node" "/usr/bin/npm" "whoami"
8 error node v8.10.0
9 error npm v3.5.2
10 error code ENEEDAUTH
11 error need auth this command requires you to be logged in.
12 error need auth You need to authorize this machine using `npm adduser`
13 verbose exit [ 1, true ]
{
"name": "new-directory",
"version": "1.0.2",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"@linclark/pkg": {
"version": "1.0.3",
"resolved": "http://registry.npmjs.org/@linclark/pkg/-/pkg-1.0.3.tgz",
"integrity": "sha1-EFIsv2UpBwfGHLXWatGCIcikDbg="
}
}
}
{
"name": "new-directory",
"version": "1.0.2",
"description": "pkg for npm lesson",
"main": "test.js",
"dependencies": {
"@linclark/pkg": "^1.0.3"
},
"devDependencies": {},
"scripts": {
"test": "node test.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Anna-Myzukina/how-to-npm.git"
},
"keywords": [
"npm"
],
"author": "Muzykina",
"license": "ISC",
"bugs": {
"url": "https://github.com/Anna-Myzukina/how-to-npm/issues"
},
"homepage": "https://github.com/Anna-Myzukina/how-to-npm#readme"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment