npm使用教程

栏目: Node.js · 发布时间: 7年前

内容简介:npm 之于 Node.js ,就像 pip 之于 Python, gem 之于 Ruby, pear 之于 PHP 。npm 是随同 Node.js 一起安装的包管理工具,能解决 Node.js 代码部署上的很多问题,常见的场景有以下几种:允许用户从 npm 服务器下载别人编写的第三方包到本地使用。

NPM

是什么

npm 之于 Node.js ,就像 pip 之于 Python, gem 之于 Ruby, pear 之于 PHP 。

为什么

npm 是随同 Node.js 一起安装的包管理工具,能解决 Node.js 代码部署上的很多问题,常见的场景有以下几种:

允许用户从 npm 服务器下载别人编写的第三方包到本地使用。

允许用户从 npm 服务器下载并安装别人编写的命令行程序到本地使用。

允许用户将自己编写的包或命令行程序上传到 npm 服务器供别人使用。

npm 的背后,是基于 couchdb 的一个数据库,详细记录了每个包的信息,包括作者、版本、依赖、授权信息等。它的一个很重要的作用就是:将开发者从繁琐的包管理工作(版本、依赖等)中解放出来,更加专注于功能的开发。

怎么用

npm 不需要单独安装。在安装 Node 的时候,会连带一起安装 npm 。但是,Node 附带的 npm 可能不是最新版本,最后用下面的命令,更新到最新版本。

// Window
npm install npm -g
// Linux
$ sudo npm install npm@latest -g

npm init

npm init 用来初始化生成一个新的 package.json 文件。它会向用户提问一系列配置相关的问题,如果你觉得不用修改默认配置,一路回车就可以了。

smartAdmin

{
  "name": "smartadmin-angularjs-app",
  "version": "1.8.1",
  "description": "Demo Application for AngularJS version of SmartAdmin application template",
  "main": "index.html",
  "private": true,
  "scripts": {
    "dev": "gulp dev",
    "prod": "gulp prod"
  },
  "author": "Sunny",
  "license": "MyOrange Inc.",
  "devDependencies": {
    "gulp": "^3.9.0",
    "gulp-concat": "^2.6.0",
    "gulp-angular-templatecache": "^1.7.0",
    "gulp-connect": "^2.2.0",
    "gulp-ng-annotate": "^1.0.0",
    "gulp-uglify": "^1.2.0",
    "event-stream": "~3.3.1",
    "lodash": "^4.0.0"
  }
}

GULP

通过代码优于配置的策略,Gulp 让简单的任务简单,复杂的任务可管理。

利用 Node.js 流的威力,你可以快速构建项目并减少频繁的 IO 操作。

Gulp 严格的插件指南确保插件如你期望的那样简洁高质得工作。

var gulp = require('gulp');
gulp.task('default', function() {
  // 将你的默认的任务代码放在这
});

代码片段

gulp.task('dev', ['vendor', 'js', 'watch', 'connect']);
gulp.task('default', ['dev']);

gulp.task('js', function () {
    return es.merge(gulp.src(source.js.src), getTemplateStream())
        .pipe(concat('app.js'))
        .pipe(gulp.dest(destinations.js));
});

gulp.task('watch', function () {
    gulp.watch(source.js.src, ['js']);
    gulp.watch(source.js.tpl, ['js']);
});

gulp.task('connect', function () {
    connect.server({
        port: 16701
    });
});

gulp.task('vendor', function () {
    _.forIn(scripts.chunks, function (chunkScripts, chunkName) {
        var paths = [];
        chunkScripts.forEach(function (script) {
            var scriptFileName = scripts.paths[script];

            if (!fs.existsSync(__dirname + '/' + scriptFileName)) {

                throw console.error('Required path doesn\'t exist: ' + __dirname + '/' + scriptFileName, script)
            }
            paths.push(scriptFileName);
        });
        gulp.src(paths)
            .pipe(concat(chunkName + '.js'))
            //.on('error', swallowError)
            .pipe(gulp.dest(destinations.js))
    })

});

以上所述就是小编给大家介绍的《npm使用教程》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Developing Large Web Applications

Developing Large Web Applications

Kyle Loudon / Yahoo Press / 2010-3-15 / USD 34.99

As web applications grow, so do the challenges. These applications need to live up to demanding performance requirements, and be reliable around the clock every day of the year. And they need to withs......一起来看看 《Developing Large Web Applications》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

html转js在线工具
html转js在线工具

html转js在线工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具