1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Npmをいじくってみた

Posted at

1.前回までのNodeJS

NodeJSをインストールしたので
http://qiita.com/yuki_saito_/items/0de82f47e03374c8251d

今回はNPM(パッケージマネージャーを使ってみる)
簡単に動作確認するところまで。

2.手順

2.1作業ディレクトリの作成

今回用の作業ディレクトリを作成する。
mkdir -p Nodetest/npm-example
cd Nodetest/npm-example

2.2初期化コマンド

npm init コマンドを事項

コマンド
$npm init 

// こんな入力を求められる

Press ^C at any time to quit.
//プロジェクトの名前
name: (npm-example) example
// バージョン
version: (0.0.0) 
// 説明
description: setumei
//最初のページ
entry point: (index.js) 
test command:
// 連携するGit 
git repository: 
keywords: 
author: Noname
license: (ISC) 

{
  "name": "example",
  "version": "0.0.0",
  "description": "setumei",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Noname",
  "license": "ISC"
}


Is this ok? (yes) yes

そうすると上記のjsonがpackage.jsonとして保存される

2.3 アンダースコアJSのインストール

ためしにアンスコJSを入れてみる

npm install --save underscore

--saveコマンドは、package.jsonに依存を追加するコマンド(プロダクト用)
--save-devコマンドは、package.jsonに依存を追加するコマンド(開発用)
-gコマンドは、package.jsonに依存を追加するコマンド(マシン全体で利用したいとき)

node_modulesフォルダができる(この中にアンスコjsが入っている)

package.json
{
  "name": "example",
  "version": "0.0.0",
  "description": "setumei",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Noname",
  "license": "ISC",
  "dependencies": {
    "underscore": "^1.8.3"
  }
}

はこんな感じで、dependenciesが追加されている

※ためしに、node_modules削除して、npm installコマンドをたたくと
 package.jsonに従ってインストールするので、アンスコJSが再度インストールされる

2.4 動かしてみる

せっかくなので動かしてみる

unsco.js
// Node.jsの外部ライブラリの読み込み
var _ = require('underscore');

_.each([1,2,3],function(elem) {

	console.log(elem);

	});

コマンド
node unsco.js

で起動

ローカル環境の構築とか、色々はかどうりそうだなー

次はGrunt、Gulpあたりかな

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?