Npm cheat sheet

// creating npm package

npm init

// package.json

{
    "name": "package-json",
    "version": "1.0.0",
    "description": "package description",
    "email": "bippan1407@gmail.com",
    "url": "https://bippan.vercel.app/",
    "keywords": ["vue", "javascript"],
    "homepage": "The url to the project homepage.",
    "bugs": {
        "url": "https://github.com/owner/project/issues",
        "email": "project@hostname.com"
    },
    "license": "MIT",
    "funding": [
    {
      "type": "individual",
      "url": "http://example.com/donate"
    },
    "http://example.com/donateAlso",
    {
      "type": "patreon",
      "url": "https://www.patreon.com/my-account"
    }
  ],
  "files": ["src"],
  "main": "index.js",
  "browser": "index.js", // module to be used on client side
  "bin": {
    "myapp": "./cli.js" //Please make sure that your file(s) 
    // referenced in bin starts with #!/usr/bin/env node, 
    // otherwise the scripts are started without the node executable!
  },
  "man": ["./man/foo.1", "./man/foo.2"],
  "directories": { // used to define the structure of package
       "lib": "src/lib",
       "bin": "local/binaries",
       "jars": "java" 
   },
   "respository": {
        "type": "git",
        "url": "https://github.com/npm/cli.git",
        "directory": "packages/vue-dom" // If the package.json for your 
        // package is not in the root directory (for example if it 
        // is part of a monorepo), you can specify the directory in which it lives
   },
   "scripts": {
        "dev": "nodemon index.js",
   },
   "dependencies": {
    "foo": "1.0.0 - 2.9999.9999",
    "asd": "git+https://isaacs@github.com/npm/cli.git", // url as dependency
   },
   "devDependencies": {
    "nodemon": "~1.6.3"
   },
   "peerDependencies": { // In some cases, you want to express the compatibility
   // of your package with a host tool or library, while not necessarily
   // doing a require of this host. This is usually referred to as a plugin
    "boo-bar": "1.0.0"
   },
   "peerDependenciesMeta": {
    "soy-milk": {
      "optional": true // peer dependencies to be marked as optional
    }
  },
 "bundleDependencies": ["renderized", "super-streams"],
 "optionalDependencies" : {
    "foo-bar": 1.0.0
 },
  "overrides": {
    "foo": {
      ".": "1.0.0",
      "bar": "1.0.0"
    }
  },
  "engines": {
    "node": ">=0.10.3 <15",
    "npm": "~1.0.20"
  },
   "os": ["darwin", "linux"],
   "cpu": ["x64", "ia32"],
   "private": true, //  npm will refuse to publish it
    "workspaces": ["./packages/*"]
}

Links to know more about fields used in package.json


// npm update package verion
npm version patch

// publish npm package
npm publish

// To see what will be included in your package
npm pack --dry-run

// Login to a registry user account
npm login

// Installing local module using npm  
npm link
-- OR --
cd ./project-dir
npm link ../package-dir

Files included in package

  • If there is a .gitignore or .npmignore file, then ignored files in that and all child directories will be excluded from the package. If both files exist, then the .gitignore is ignored, and only the .npmignore is used.

.npmignore files follow the same pattern rules as .gitignore files