Latest web development tutorials

NPM 소개를 사용

NPM은 일반적인 사용 시나리오에 배포 다음과 같다을 많은 문제 NodeJS 코드를 해결할 수 있습니다 함께 설치 NodeJS 패키지 관리 도구를 동반한다 :

  • NPM은 사용자가 로컬 사용을 위해 다른 사람에 의해 작성된 타사 패키지를 서버에서 다운로드 할 수 있습니다.
  • 그것은 사용자가 NPM에서 로컬 서버를 사용하는 다른 사람에 의해 작성된 명령 줄 프로그램을 다운로드하고 설치할 수 있습니다.
  • 그것은 사용자가 NPM를 사용하는 다른 사람을 위해 서버에 업로드 자신의 패키지 또는 명령 줄 프로그램을 작성할 수 있습니다.

nodejs의 새 버전이 NPM을 통합했기 때문에, 그래서 NPM 전에도 설치 될 수있다. 또한 성공적인 설치 여부를 테스트하기 위해"NPM -v"를 입력 할 수 있습니다.다음 명령은, 버전은 신속한 설치가 성공적으로 나타납니다

$ npm -v
2.3.0

다음과 같이 NPM의 이전 버전을 설치하는 경우, 그것은 쉽게 명령에 NPM을 통해 업그레이드 할 수 있습니다, 명령은 다음과 같습니다

$ sudo npm install npm -g
/usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js
[email protected] /usr/local/lib/node_modules/npm

윈도우 시스템 경우, 다음 명령을 사용합니다 :

npm install npm -g

NPM 명령 모듈을 사용하여 설치

다음과 같이 NPM 설치 Node.js를 모듈 구문은 다음과 같습니다

$ npm install <Module Name>

다음의 예는, 우리는 NPM 공통 프레임 워크 Node.js를 웹 모듈Express를 설치하는 명령을 사용합니다 :

$ npm install express

설치가 완료되면, 패킷이 프로젝트 디렉토리에 node_modules 디렉토리에 위치 표현, 그래서 단지를 통해 코드는 타사 패키지의 경로를 지정하지 않고, 방식처럼( '표현')가 필요합니다.

var express = require('express');

글로벌 설치 및 로컬 설치

NPM은 두 개의이다 (글로벌), 로컬 설치에 글로벌 설치 (로컬)을 패키지를 설치 명령 줄에서 노크, 유일한 차이점은 -g 같은 단지가없는 것입니다

npm install express          # 本地安装
npm install express -g   # 全局安装

다음과 같은 오류가 경우 :

npm err! Error: connect ECONNREFUSED 127.0.0.1:8087 

해결 방법 :

$ npm config set proxy null

로컬 설치

  • 1.하지 node_modules 디렉토리가 현재 디렉토리 node_modules NPM 명령을 실행 디렉토리에 생성되면, ./node_modules (명령이 NPM을 실행하는 디렉토리)에 배치 된 설치 패키지.
  • 2. 로컬로 설치 패키지를 도입 할 수있다을 통해 필요 ().

글로벌 설치

  • 1.은 / usr / 로컬 또는 설치 디렉토리 노드에 배치 된 설치 패키지.
  • 2. 명령 줄에서 직접 사용할 수 있습니다.

이 두 기능을 원한다면, 당신은 두 장소에 설치하거나 NPM 링크를 사용해야합니다.

다음으로, 우리는 명시 설치 글로벌을 사용

$ npm install express -g

설치 과정에서 다음과 같은 결과를 출력 버전 번호, 모듈의 설치 위치의 첫 번째 줄.

[email protected] node_modules/express
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected], [email protected])
└── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected])

당신은 모든 글로벌 설치된 모듈을 보려면 다음 명령을 사용할 수 있습니다 :

$ npm ls -g

사용 package.json

디렉토리 모듈에 위치 package.json는 패키지의 속성을 정의한다. 이제 파일 package.json 특급 패키지 위치 node_modules / 표현 / package.json의 내용을 살펴 보자 :

{
  "name": "express",
  "description": "Fast, unopinionated, minimalist web framework",
  "version": "4.13.3",
  "author": {
    "name": "TJ Holowaychuk",
    "email": "[email protected]"
  },
  "contributors": [
    {
      "name": "Aaron Heckmann",
      "email": "[email protected]"
    },
    {
      "name": "Ciaran Jessup",
      "email": "[email protected]"
    },
    {
      "name": "Douglas Christopher Wilson",
      "email": "[email protected]"
    },
    {
      "name": "Guillermo Rauch",
      "email": "[email protected]"
    },
    {
      "name": "Jonathan Ong",
      "email": "[email protected]"
    },
    {
      "name": "Roman Shtylman",
      "email": "[email protected]"
    },
    {
      "name": "Young Jae Sim",
      "email": "[email protected]"
    }
  ],
  "license": "MIT",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/strongloop/express.git"
  },
  "homepage": "http://expressjs.com/",
  "keywords": [
    "express",
    "framework",
    "sinatra",
    "web",
    "rest",
    "restful",
    "router",
    "app",
    "api"
  ],
  "dependencies": {
    "accepts": "~1.2.12",
    "array-flatten": "1.1.1",
    "content-disposition": "0.5.0",
    "content-type": "~1.0.1",
    "cookie": "0.1.3",
    "cookie-signature": "1.0.6",
    "debug": "~2.2.0",
    "depd": "~1.0.1",
    "escape-html": "1.0.2",
    "etag": "~1.7.0",
    "finalhandler": "0.4.0",
    "fresh": "0.3.0",
    "merge-descriptors": "1.0.0",
    "methods": "~1.1.1",
    "on-finished": "~2.3.0",
    "parseurl": "~1.3.0",
    "path-to-regexp": "0.1.7",
    "proxy-addr": "~1.0.8",
    "qs": "4.0.0",
    "range-parser": "~1.0.2",
    "send": "0.13.0",
    "serve-static": "~1.10.0",
    "type-is": "~1.6.6",
    "utils-merge": "1.0.0",
    "vary": "~1.0.1"
  },
  "devDependencies": {
    "after": "0.8.1",
    "ejs": "2.3.3",
    "istanbul": "0.3.17",
    "marked": "0.3.5",
    "mocha": "2.2.5",
    "should": "7.0.2",
    "supertest": "1.0.1",
    "body-parser": "~1.13.3",
    "connect-redis": "~2.4.1",
    "cookie-parser": "~1.3.5",
    "cookie-session": "~1.2.0",
    "express-session": "~1.11.3",
    "jade": "~1.11.0",
    "method-override": "~2.3.5",
    "morgan": "~1.6.1",
    "multiparty": "~4.1.2",
    "vhost": "~3.0.1"
  },
  "engines": {
    "node": ">= 0.10.0"
  },
  "files": [
    "LICENSE",
    "History.md",
    "Readme.md",
    "index.js",
    "lib/"
  ],
  "scripts": {
    "test": "mocha --require test/support/env --reporter spec --bail --check-leaks test/ test/acceptance/",
    "test-ci": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --require test/support/env --reporter spec --check-leaks test/ test/acceptance/",
    "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --require test/support/env --reporter dot --check-leaks test/ test/acceptance/",
    "test-tap": "mocha --require test/support/env --reporter tap --check-leaks test/ test/acceptance/"
  },
  "gitHead": "ef7ad681b245fba023843ce94f6bcb8e275bbb8e",
  "bugs": {
    "url": "https://github.com/strongloop/express/issues"
  },
  "_id": "[email protected]",
  "_shasum": "ddb2f1fb4502bf33598d2b032b037960ca6c80a3",
  "_from": "express@*",
  "_npmVersion": "1.4.28",
  "_npmUser": {
    "name": "dougwilson",
    "email": "[email protected]"
  },
  "maintainers": [
    {
      "name": "tjholowaychuk",
      "email": "[email protected]"
    },
    {
      "name": "jongleberry",
      "email": "[email protected]"
    },
    {
      "name": "dougwilson",
      "email": "[email protected]"
    },
    {
      "name": "rfeng",
      "email": "[email protected]"
    },
    {
      "name": "aredridel",
      "email": "[email protected]"
    },
    {
      "name": "strongloop",
      "email": "[email protected]"
    },
    {
      "name": "defunctzombie",
      "email": "[email protected]"
    }
  ],
  "dist": {
    "shasum": "ddb2f1fb4502bf33598d2b032b037960ca6c80a3",
    "tarball": "http://registry.npmjs.org/express/-/express-4.13.3.tgz"
  },
  "directories": {},
  "_resolved": "https://registry.npmjs.org/express/-/express-4.13.3.tgz",
  "readme": "ERROR: No README data found!"
}

Package.json 속성 설명

  • 이름 - 패키지 이름.

  • 버전 - 패키지의 버전 번호입니다.

  • 설명 - 패키지의 설명입니다.

  • 공식 웹 사이트의 URL을 패키지- 홈페이지.

  • 저자 - 패키지 이름의 저자.

  • 다른 참여자 이름 패키지- 참여자.

  • 종속 - 종속의 목록입니다.종속성가 설치되지 않은 경우, NPM은 자동으로 node_module 디렉토리에 종속 패키지를 설치합니다.

  • 저장소 - 형 패키지 코드 저장 영역, 자식 또는 SVN 중, Github에서 볼 수 자식.

  • 메인 - 메인 필드는 프로그램의 주요 프로젝트에 대한 포인터 모듈 ID입니다.즉, 이름을 명시 팩 경우, 사용자가 설치 한 다음 ( "표현") 필요로한다.

  • 키워드 - 키워드


언 로딩 모듈

우리는 Node.js를 모듈을 제거하려면 다음 명령을 사용할 수 있습니다.

$ npm uninstall express

제거 후에는이 다음 / node_modules / 카탈로그 패키지로 이동하거나 다음 명령을 사용할 수 있습니다 :

$ npm ls

업데이트 모듈

우리는 모듈을 업데이트하려면 다음 명령을 사용할 수 있습니다 :

$ npm update express

검색 모듈

다음 검색 모듈을 사용합니다 :

$ npm search express

모듈을 만들고

모듈을 만들고, package.json 파일은 필수적이다. 우리는 결과 파일은 기본 결과를 포함, NPM은 package.json 파일을 생성 할 수 있습니다.

$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg> --save` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
name: (node_modules) w3big                   # 模块名
version: (1.0.0) 
description: Node.js 测试模块(www.w3big.com)  # 描述
entry point: (index.js) 
test command: make test
git repository: https://github.com/w3big/w3big.git  # Github 地址
keywords: 
author: 
license: (ISC) 
About to write to ……/node_modules/package.json:      # 生成地址

{
  "name": "w3big",
  "version": "1.0.0",
  "description": "Node.js 测试模块(www.w3big.com)",
  ……
}


Is this ok? (yes) yes

위의 정보, 당신은 입력에 자신의 상황을해야합니다. 최종 입력 후 "예"package.json 파일을 생성합니다.

그 다음 우리는 NPM 저장소에 등록 된 사용자 (사용 등록 메일)에 다음 명령을 사용할 수 있습니다 :

$ npm adduser
Username: mcmohd
Password:
Email: (this IS public) [email protected]

다음으로, 우리는 모듈을 분리하려면 다음 명령을 사용합니다 :

$ npm publish

위의 단계가 제대로 완료 할 경우 NPM을 설치하는 다른 모듈과 동일하게 사용할 수 있습니다.


버전 번호

NPM 다운로드 코드가 버전 번호에 노출 될 때 방출된다 사용한다. NPM 여기서, 간단히 코드 관리 시맨틱 버전 번호를 사용.

의미론 버전 주 버전 번호, 마이너 버전 번호와 패치 버전 번호를 나타내는 XYZ 세 가지로 나누어진다. 경우, 코드 변경은 다음의 원리에 따른 업데이트 버전 번호.

  • 만이 버그를 해결하는 경우 Z 비트를 업데이트해야합니다.
  • 새로운 기능이지만 이전 버전과 호환되는 경우, Y 비트를 업데이트해야합니다.
  • 하향 호환되지 큰 변화가있는 경우, 그 X 위치를 업데이트해야한다.

버전 번호 후이 확신 성명에서 고정 된 버전 번호에 의존 할뿐만 아니라, 타사 패키지에 의존 할뿐만 아니라, 버전 번호의 범위에 따라 달라집니다. 예를 들어 "변수는 argv"의 경우 : 0.0.x 시리즈는 argv의 최신 버전을 나타냅니다의 "0.0.x는"따라 달라집니다.

모든 버전은 NPM 볼 수있는 대상 번호 범위를 지원하는 공식 문서를 .


NPM 일반적인 명령

이 장의 섹션뿐만 아니라, NPM은 또한 많은 기능을 제공, package.json 또한 많은 다른 유용한 필드가 있습니다.

받는 사람 또한 npmjs.org/doc/~~HEAD=V 공식 문서를 확인하지만, 여기에 일반적으로 사용되는 일부 NPM 명령입니다.

NPM은 모든 명령을 볼 수 NPM 도움말을 사용하여 설치 및 게시 등의 명령을 많이 제공합니다.

  • NPM과 같은 명령을 많이 제공합니다 installpublish 하여, npm help 모든 명령을 볼 수 있습니다.

  • 사용 npm help <command> 과 같은 명령에 대한 자세한 도움말을 보려면 npm help install .

  • 에서 package.json 다음과 같은 디렉터리 사용 npm install . -g 출시 전에 현지 테스트를 위해 사용할 수있는 현재의 명령 줄 프로그램에 로컬로 설치 할 수 있습니다.

  • 사용 npm update <package> 현재 디렉토리에 넣을 수 있습니다 node_modules 모듈이 최신 버전에 해당하는 내부 하위 디렉토리를.

  • 사용 npm update <package> -g 전 세계적으로 최신 버전으로 업데이트를 설치 명령 줄 프로그램에 해당 할 수 있습니다.

  • 사용 npm cache clear 새 릴리스 태그에 대한 동일한 버전 번호를 다루는, NPM 로컬 캐시를 취소 할 수 있습니다.

  • 사용 npm unpublish <package>@<version> > 취소 할 수 있습니다 포스트는 자신의 코드의 버전을 발표했다.


Taobao의 NPM 미러를 사용하여

우리 모두는 공식 미러 NPM의 국내 직접 사용이 매우 느린 것을 알고, Taobao의 NPM 미러를 사용하는이 좋습니다.

Taobao의 NPM 완전한 npmjs.org 미러 이미지, 당신은 공식 서비스와 동기화 최대한 보장하기 위해 대신 공식 버전 (읽기 전용) 10 분 현재 동기화 주파수의이 사용할 수 있습니다.

당신은 Taobao의 사용자 정의 cnpm (gzip으로 압축 지원) 대신 기본 NPM의 명령 줄 도구를 사용할 수 있습니다 :

$ npm install -g cnpm --registry=https://registry.npm.taobao.org

이 모듈은 cnpm 명령을 사용하여 설치할 수 있습니다 :

$ cnpm install [name]

자세한 내용은 찾을 수 있습니다 http://npm.taobao.org/ .