视频文件中global 广告界意思tag 是什么意思

扫二维码下载作业帮
拍照搜题,秒出答案,一键查看所有搜题记录
下载作业帮安装包
扫二维码下载作业帮
拍照搜题,秒出答案,一键查看所有搜题记录
英语翻译Globalbagtag was (0) set up six years ago by husband-and-wife team Chris and Alison Truelove on their return from a holiday in Australia.The couple decided to (1) __launch______ Globalbagtag.com,which sells secure tags which travellers attach to their luggage.They started to (2) _______charge___ £9.95 per tag in the first year,followed by a £2.95 subscription (3) ____fee____ for the following years.Customers activate the tag online with their home address on an online database.If luggage is lost,the person who finds it can log on to the Globalbagtag website and report the missing items.Mr Truelove said:' We have seen a huge (4) _growth_________ in orders as travellers realise they cannot rely on the airlines to look after their bags.It is actually a very simple (5) ________concept______ Globalbagtag is now planning to (6) ___branch out_________ into the stickers for items including mobile phones,MP3 players,keys and laptops.
作业帮用户
扫二维码下载作业帮
拍照搜题,秒出答案,一键查看所有搜题记录
Globalbagtag(全球包裹追踪)由一对夫妻Chris and Alison Truelove六年前从澳大利亚回来后创立.这对夫妻决定创办Globalbagtag.com这个网站,用以销售游客在他们包裹上粘贴的安全标签.头一年他们以每个标签9.95磅的价格出售,第二年则花费2.95磅定金即可.消费者们只需网上激活他们的标签,填写家庭住址到线上数据库.如果包裹丢失,找到包裹的人可以登录到Globalbagtag这个网站,告知丢失的物品.Truelove先生说,我们正面临着订单数量的大量增长,因为消费者们发现他们不能够依赖航空公司来帮他们找回遗失的包裹.这个网站计划要做的其实是一个很简单的想法,那就是把这种标签业务扩展到手机、MP3,钥匙和笔记本电脑.答题不易,不是翻译机,望采纳,谢谢
为您推荐:
其他类似问题
扫描下载二维码博客访问: 269466
博文数量: 88
博客积分: 2677
博客等级: 少校
技术积分: 892
注册时间:
分类: LINUX 14:25:00
自己顺便翻译的,对付着看吧
gtags - create tag files for
gtags [-ciIOqvw][-d
tag-file][-f file][-n number][dbpath]
Gtags 递归收集在当前路径下的源文件, 得到标记和向tag(`GTAGS',
`GRTAGS', `GSYMS' and `GPATH')文件中写相互参照数据. 应该在source
tree的root位置执行这个命令.
支持C, C++, yacc,
java, PHP,Assembly 源文件.
`.c' or `.h'
被假设为 C source
`.c++' `.cc'
`.cpp' `.cxx' `.hxx' `.hpp'
`.C' `.H' 被假设为C++ source files. `.y' 被假设为 YACC source files.
&`.java'被假设为Java source files.
`.php' `.php3'
`.phtml'被假设为PHP source files.
or `.S'被假设为Assembly source files.
其他文件被假设为text
提供下列选项 The following options are
available:
`-c', `--compact'
用压缩格式(compact format)生成. 这个选项不影响 GRTAGS and GSYMS, 因为他们总是用压缩格式(compact format)生成.
`--config'[=name]
显示config变量名. 如果名字未被指定那么显示完整的config入口.
`-d', `--dump' tag-file
删掉一个tag文件. 输出格式是 'keydata'. for
debugging.
`-f', `--file' file
从文件名列表读取源文件中的候选文件.默认:当前路径下所有的文件均为候选文件. 如果文件时 `-', 那么从标准输入读取. 文件名必须被newline分隔.
`--gtagsconf' file
从file读取用户的configuration.
`--gtagslabel' label
label 是被用作configuration文件的label. 默认是 default.
`-I', `--idutils'
也从idutils(1)生成索引文件.
`-i', `--incremental'
渐进更新tag文件.最好用 global(1) 加 -u 选项.
`-n', `--max-args' number
参数的最大number for gtags-parser(1). 默认:, gtags 调用 gtags-parser 加参数, 尽可能多的减少调用频率.
`-O', `--objdir'
用 objdir 像 dbpath那样. 如果 `$MAKEOBJDIRPREFIX' 路径存在, gtags 创建
`$MAKEOBJDIRPREFIX/' 文件夹 并生成tag文件在此. 如果 dbpath 被指定, 这个选项将被忽略.
`--single-update' file
更新 tag 文件为单个文件. 文件被更新,据估计文件被更新,并且其他的文件不被更新. 这个文件必须是当前目录的相对路径名. 这个选项暗含着 -i 选项. 如果这个文件是新的那么`--single-update' 被忽略, 并且这个处理将自动地被切换到正常的增量更新.
`-q', `--quiet'
`-v', `--verbose'
`-w', `--warning'
打印警告消息.
这个目录是tag文件被生成的目录. 默认是当前目录. 他是有用的,当你的源目录被保存在一个只读设备中时(例如,CDROM).
&&&&&&& $ ls
&&&&&&& Makefile&&&&& src/&&&
global -x main
&&&&&&& main&&&&&&&&&&&&& 10 src/main.c& main (argc, argv) {
for object definitions.
for object references.
for other symbols(符号,标志).
for path of source files.
`/etc/gtags.conf', `$HOME/.globalrc'
Configuration
下面的环境变量影响gtags的执行:
如果这个值被设置, 他的值被用于configuration文件. 默认是 `$HOME/.globalrc'.
GTAGSLABEL
如果这个值被设置, 他的值被用于 the label of configuration 文件. The default is default.
GTAGSCACHE
如果这个值被设置, 他的值被用于the size of B-tree cache(缓存). 默认是 (bytes).
GTAGSFORCECPP
如果这个值被设置, 每一个后缀是 'h' 的文件将被当做C++ source 文件.
MAKEOBJDIRPREFIX
如果这个值被设置, `$MAKEOBJDIRPREFIX' 被用于 一个目标的前缀目录. The default is `/usr/obj'.
The following configuration variables affect the
execution of gtags. You can see the default value for each variable with the
`--config' option.
GTAGS(string)
variable is set, its value is used as the command line of the parser for GTAGS.
The default is `gtags-parser %s'.
GRTAGS(string)
variable is set, its value is used as the command line of the parser for
GRTAGS. The default is `gtags-parser -r %s'.
GSYMS(string)
variable is set, its value is used as the command line of the parser for GSYMS.
The default is `gtags-parser -s %s'.
icase_path(boolean)
case distinctions in the path. Suffixes check are affected by this capability.
langmap(comma separated list)
mapping. Each comma-separated map consists of the language name, a colon, and a
list of file extensions. Default mapping is
'c:.c.h,yacc:.y,asm:.s.S,java:.java,cpp:.c++.cc.cpp.cxx.hxx.hpp.C.H,php:.php.php3.phtml'.
skip(comma separated list)
skips files which are listed in this list. As a special exception, gtags
collect values from multiple skip variables. If the value ends with '/', it
assumed as a directory and gtags skips all files under it. If the value start
with '/', it assumed relative path from the root of source directory.
suffixes(comma separated list)
of target source file. As a special exception, gtags collect values from
multiple suffixes variables. This variable is obsoleted. If the langmap
variable is defined gtags no longer refers this.
如果发生错误Gatag返回一个非0值。否者返回0.
详细信息有重要的级别. The most important
level is 0, the second is 1 and so on. All the message has level numbers
leading(领先) blanks.
gtags-parser(1), global(1), htags(1).
GNU GLOBAL source code tag system
(http://www.gnu.org/software/global/).
`GTAGS', `GRTAGS' and `GSYMS' 非常大. 在使用这个命令之前请检查磁盘空间.
Assembly support is far from complete. It extracts only
ENTRY() and ALTENTRY() from source file. 也许仅支持FreeBSD 和Linux kernel source 有效.
他们不并行控制tag文件.
Tama Communications Corporation通信公司.
The gtags command appeared in FreeBSD 2.2.2.
阅读(1122) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
请登录后评论。PHP中TAL模板引擎语法的解析(代码)
原创
 10:25:52
322
这篇文章给大家介绍的内容是关于php TAL模板引擎语法,内容很详细,有需要的朋友可以参考一下,希望可以帮助到大家。PHP 数据本文档的使用到的数据($talData)//定义talData变量
$talData = array();
$talData['title'] = 'title1';
$talData['key'] = 'key1';
$talData['href'] = 'xxx.html';
$talData['html'] = '&h1&h1标题&/h1&';
$talData['fun'] = function($key=''){
return $key === 'key1'? true :
//两维数组
$talData['list1'] = array(
array('id'=&'1', 'title'=&'标题1'),
array('id'=&'2', 'title'=&'标题2'),
array('id'=&'3', 'title'=&'标题3'),
array('id'=&'4', 'title'=&'标题4')
);//输出的数据结构
{&title&:&title1&,&key&:&key1&,&href&:&xxx.html&,&html&:&
h1\u&\/h1&&,&fun&:{},&list1&:[{&id&:&1&,&title&:&\u&},{&id&:&2&,&title&:&\u&},{&id&:&3&,&title&:&\u&},{&id&:&4&,&title&:&\u&}]}属性优先权define
定义变量conditionrepeatcontent or replacaeattributes
属性omit-tagTAL有效空间定义变量定义全局(global)变量title和局部变量 key&p tal:define=&global title talData/ key talData/key&&
${title} && ${key}
${title} && ${key}
&!-- 结果 --&
&p&title1 && key1&/p&
title1 &&定义默认值变量&p tal:define=&global fname string:fname1 lname string:lname1&&
${fname} && ${lname}
${fname} && ${lname}
&!-- 结果 --&
&p&fname1 string && lname1 string&/p&
fname1 string &&TAL有效空间定义变量定义拼接变量&p tal:define=&global hello string:hello $fname welcome on this page&&
&p tal:define=&global hello string:hello ${fname} welcome on this page&&
&!-- 结果 --&
&p&hello fname1 string welcome on this page&/p&定义变量,执行php方法&p tal:define=&global rand php: rand(1, 4)&&
&!-- 结果 --&
&p&2&/p&TAL有效空间输出内容tal:replace=&数据& 将用值替代整个标签,如果没有赋值将替空&tal:block&
&p&渲染1&/p&
&/tal:block&
&tal:block tal:replace=&&&
不渲染这里
&/tal:block&
&tal:block tal:replace=&talData/title&&
&p&不渲染这里&/p&
&/tal:block&
&p&${talData/title}&/p&
&!-- 结果 --&
&p&渲染1&/p&
&p&title1&/p&tal:content=&数据& 将会替代标签内的内容&p tal:content=&talData/title&&
test data&span&tips&/span&
&!-- 结果 --&
&p&title1&/p&TAL有效空间输出内容${数据}&p class=&p p_${talData/title}&&${talData/title}&/p&
&!-- 结果 --&
&p class=&p p_title1&&title1&/p&tal:attributes 将会改变tag的属性或值&a href=&http://www.foo.com& title=&some foo link& tal:attributes=&href talData/ title talData/title& tal:content=&talData/key&&sample link&/a&
&!-- 结果 --&
&a href=&xxx.html& title=&title1&&key1&/a&tal:on-error=&string: 出错时显示内容& ;出错时显示error内容,并且没有当前标签;&span tal:on-error=&string: 当前数据不存在& tal:content=&talData/key&&key 已经是定义了&/span&
&span tal:on-error=&string: 当前数据不存在& tal:content=&talData/ky&&ky 没有定义的&/span&
&!-- 结果 --&
&span&key1&/span&
当前数据不存在TAL有效空间判断条件tal:condition=&数据&,条件是true,标签和它的内容就会被显示&p tal:condition=&php: true& tal:content=&talData/title&&标题&/p&
&p tal:condition=&php: talData['key'] == 'key1'& tal:content=&talData/key&&关键词&/p&
&p tal:condition=&php: key == 'key1'& tal:define=&key talData/key& tal:content=&talData/key&&关键词&/p&
&p tal:condition=&talData/key& tal:content=&talData/key&&关键词&/p&
&p tal:condition=&talData/fun& tal:content=&talData/key&&关键词&/p&
&!-- 结果 --&
&p&title1&/p&
&p&key1&/p&
&p&key1&/p&
&p&key1&/p&TAL有效空间循环tal:repeat=&赋值变量 数据&;属性循环它的元素和内容直到结束&ul tal:repeat=&item talData/list1&&
&li tal:condition=&php: repeat.item.index == 0&&
repeat/item/key: ${repeat/item/key} ;(如果talData/list1是一组联合对象,返回item的键)&br/&
repeat/item/index: ${repeat/item/index} ;(返回索引号(0~count-1))&br/&
repeat/item/number: ${repeat/item/number} ;(返回索引号(1~count))&br/&
repeat/item/even: ${repeat/item/even} ;(如果是偶数,true)&br/&
repeat/item/odd: ${repeat/item/odd} ;(如果是奇数,true)&br/&
repeat/item/start: ${repeat/item/start} ;(如果是第一个元素,true)&br/&
repeat/item/end: ${repeat/item/end} ;(如果是最后一个元素,true)&br/&
repeat/item/length: ${repeat/item/length} ;(返回talData/list1里面元素个数)&br/&
-------------
&li tal:condition=&php: repeat.item.index & 0& tal:content=&item/title&&列表li&/li&
&!-- 结果 --&
repeat/item/key: 0 ;(如果talData/list1是一组联合对象,返回item的键)&br/&
repeat/item/index: 0 ;(返回索引号(0~count-1))&br/&
repeat/item/number: 1 ;(返回索引号(1~count))&br/&
repeat/item/even: 1 ;(如果是偶数,true)&br/&
repeat/item/odd: 0 ;(如果是奇数,true)&br/&
repeat/item/start: 1 ;(如果是第一个元素,true)&br/&
repeat/item/end: 0 ;(如果是最后一个元素,true)&br/&
repeat/item/length: 4 ;(返回talData/list1里面元素个数)&br/&
&li&标题2&/li&
&li&标题3&/li&
&li&标题4&/li&TAL有效空间标签渲染与否tal:omit-tag=&condition&;要求phptal解析时忽略实体的开关闭标签,它的内容仍然会显示.&p tal:omit-tag=&php: false&&
omit-tag值为真,不出现当前p标签,否则就出现当前p标签
&p tal:omit-tag=&php: true&&
omit-tag值为真,不出现当前p标签,否则就出现当前p标签
&!-- 结果 --&
&p&omit-tag值为真,不出现当前p标签,否则就出现当前p标签&/p&
omit-tag值为真,不出现当前p标签,否则就出现当前p标签&tal:block&;代替标签,标签永远不输出&tal:block tal:repeat=&x php:range(1,10)& tal:content=&x&&文字会出现十次&/tal:block&
&!-- 结果 --&
METAL空间 支持宏metal:define-macro 定义宏&!-- 在pWeb/_macro/common.html文件中代码 --&
&tal:block metal:define-macro=&WinPageData&&
&script type=&text/javascript&&
window.WinPageData = ${structure WinPageData};
&/tal:block&metal:use-macro 调用宏&tal:block metal:use-macro=&pWeb/_macro/common.html/WinPageData&&
当前内容会被宏替换掉
&script type=&text/javascript&&
window.WinPageData = {};
&/tal:block&
&p metal:use-macro=&${路径变量}/macros.html/main_menu&&&/p&METAL空间 支持宏metal:define-slot 定义宏标签替换&!-- 在pWeb/_macro/mlog.html文件中代码 --&
&tal:block metal:define-macro=&mobile&&
&metal:block define-slot=&mlog_page&&
var mlog_page = 'mobile_other';
&/metal:block&
var mlog_track = function(){};
&script src='xxx.js'&&/script&
&/tal:block&metal:fill-slot
使用宏标签替换&tal:block metal:use-macro=&pWeb/_macro/mlog.html/mobile&&
&tal:block metal:fill-slot=&mlog_page&&
var mlog_page = 'mobile_index';
&/tal:block&
&/tal:block&METAL空间 支持宏宏获取参数&!-- 在pWeb/_macro/mlog.html文件中代码 --&
&tal:block metal:define-macro=&conent&&
&p&${title}&/p&
&p&${key}&/p&
&/tal:block&宏传参数(tal:define)&tal:block metal:use-macro=&pWeb/_macro/mlog.html/conent&
tal:define=&title '标题'; key '关键词'&&&/tal:block&phptal空间phptal:debugphptal:cache 使整个元素(包括标签)保存在磁盘而不重新解析直到cache失效,有效期的格式由数字和'd', 'h','m'或's'组成.&p class=&footer& phptal:cache=&3h&&...&/p&
有效期可以有选择的跟随'per'参数来定义多少个缓存被共享,使用'per url'分别针对每个url复制元素.
&ol id=&breadcrumbs& phptal:cache=&1d per url&&...&/ol&使用php:语法如下所述同正规表达式,除了-&被替换成.及变量不需要前缀$,使用空格装配由点分隔的字符串.php:htmlentities(foo)
php:'string ${varReplaced}'
php:'string ${some.path().to[0].var}'
php:foo.a.b.c(e).htmlentities(SomeClass::staticMethod())
php:SomeClass::ConstOfClass
php:SomeClass::$staticVar使用not:、exists:语法not:可以用于tal:condition中,相反判断&span tal:condition=&not: logged&&not logged&/span&
&!-- 结果 --&
&span&not logged&/span&exists:可以用于tal:condition中,判断存在&span tal:condition=&exists: talData/title& tal:content=&talData/title&&存在&/span&
&!-- 结果 --&
&span&title1&/span&使用default、structure语法default默认值;在 tal:define、tal:content、tal:attributes 使用&span tal:define=&myVar talData/title | default&&
default my var value
&p tal:content=&some/var | other/path | default&&
没有var,没有找到path
&a href=&unknown.html& title=&Unknown page& tal:attributes=&href item/href | title talData/title | default& tal:content=&item/title | default&&Unknown page&/a&
&!-- 结果 --&
&p&没有var,没有找到path&/p&
&a href=&unknown.html& title=&title1&&Unknown page&/a&使用structure语法允许将包括html/xml等变量输出显示;注意存在XSS攻击风险,谨慎使用在tal:content中使用&p tal:content=&talData/html&&&/p&
&p tal:content=&structure talData/html&&&/p&
&!-- 结果 --&
&p&&h1&h1标题&/h1&&/p&
&p&&h1&h1标题&/h1&&/p&在${}中使用&p&${talData/html}&/p&
&p&${structure talData/html}&/p&
&!-- 结果 --&
&p&&h1&h1标题&/h1&&/p&
&p&&h1&h1标题&/h1&&/p&相关文章推荐:以上就是PHP中TAL模板引擎语法的解析(代码)的详细内容,更多请关注php中文网其它相关文章!
江湖传言:PHP是世界上最好的编程语言。真的是这样吗?这个梗究竟是从哪来的?学会本课程,你就会明白了。
PHP中文网出品的PHP入门系统教学视频,完全从初学者的角度出发,绝不玩虚的,一切以实用、有用...
ThinkPHP是国内最流行的中文PHP开发框架,也是您Web项目的最佳选择。《php.cn独孤九贱(5)-ThinkPHP5视频教程》课程以ThinkPHP5最新版本为例,从最基本的框架常识开始,将...
《php.cn原创html5视频教程》课程特色:php中文网原创幽默段子系列课程,以恶搞,段子为主题风格的php视频教程!轻松的教学风格,简短的教学模式,让同学们在不知不觉中,学会了HTML知识。
本套教程,以一个真实的学校教学管理系统为案例,手把手教会您如何在一张白纸上,从零开始,一步一步的用ThinkPHP5框架快速开发出一个商业项目。
所有计算机语言的学习都要从基础开始,《PHP入门视频教程之一周学会PHP》不仅是PHP的基础部分更主要的是PHP语言的核心技术,是学习PHP必须掌握的内容,任何PHP项目的实现都离不开这部分的内容,通...
本课以最新版ThinkPHP5.0.10为基础进行开发,全程实录一个完整企业点,从后台到前台,从控制器到路由的全套完整教程,不论是你是新人,还是有一定开发经验的程序员,都可以从中学到实用的知识~~
ThinkPHP是一个快速、开源的轻量级国产PHP开发框架,是业内最流行的PHP框架之一。本课程以博客系统为例,讲述如何使用TP实战开发,从中学习Thinkphp的实践应用。模版下载地址:http:/...
本课程是php实战开发课程,以爱奇艺电影网站为蓝本从零开发一个自己的网站。目的是让大家了解真实项目的架构及开发过程
本课以一个极简的PHP开发框架为案例,向您展示了一个PHP框架应该具有的基本功能,以及具体的实现方法,让您快速对PHP开发框架的底层实现有一个清楚的认识,为以后学习其实的开发框架打下坚实的基础。
javascript是运行在浏览器上的脚本语言,连续多年,被评为全球最受欢迎的编程语言。是前端开发必备三大法器中,最具杀伤力。如果前端开发是降龙十八掌,好么javascript就是第18掌:亢龙有悔。...
本站9月直播课已经结束,本套教程是直播实录,没有报上名或者漏听学员福利来了,赶紧看看吧,说不定这里就有你的菜
轻松明快,简洁生动,让你快速走入HTML5的世界,体会语义化开发的魅力
JavaScript能够称得上是史上使用最广泛的编程语言,也是前端开发必须掌握的三技能之一:描述网页内容的HTML、描述网页样式的CSS以及描述网页行为的JavaScript。本章节将帮助大家迅速掌握...
Bootstrap 是最受欢迎的 HTML、CSS 和 JS 框架,用于开发响应式布局、移动设备优先的 WEB 项目。为所有开发者、所有应用场景而设计,它让前端开发更快速、简单,所有开发者都能快速上手...
《php.cn独孤九贱(2)-css视频教程》课程特色:php中文网原创幽默段子系列课程,以恶搞,段子为主题风格的php视频教程!轻松的教学风格,简短的教学模式,让同学们在不知不觉中,学会了CSS知识...
《php用户注册登录系统》主要介绍网站的登录注册功能,我们会从最简单的实现登录注册功能开始,增加验证码,cookie验证等,丰富网站的登录注册功能
jQuery是一个快速、简洁的JavaScript框架。设计的宗旨是“write Less,Do More”,即倡导写更少的代码,做更多的事情。它封装JavaScript常用的功能代码,提供一种简便的...
《PHP学生管理系统视频教程》主要给大家讲解了HTML,PHP,MySQL之间的相互协作,实现动态的网页显示和获取数据.
《弹指间学会HTML视频教程》从最基本的概念开始讲起,步步深入,带领大家学习HTML,了解各种常用标签的意义以及基本用法,学习HTML知识为以后的学习打下基础
《最新微信小程序开发视频教程》本节课程是由微趋道录制,讲述了如何申请一个微信小程序,以及开发中需要使用哪些工具,和需要注意哪些等。
全栈工程师
文章总浏览数------用手机观看
《部落冲突coc》骚浪贱哥布林打完黑油还不给盾,带你领略邪教的魅力!
稿件投诉分享到动态微博QQQQ空间贴吧将视频贴到博客或论坛视频地址复制嵌入代码复制微信扫一扫分享
相关视频推荐
28:14754314:48869106:48943203:421227414:51885424316:0827891612:5425634016:39623101:1870422616:0023426316:2121014:1037463701:0533072117:1645169624:34614516101:41188015:38662110:011.3万53124:34176715miniOFF返回旧版npm 是什么?
npm 为你和你的团队打开了连接整个 JavaScript 天才世界的一扇大门。它是世界上最大的软件注册表,每星期大约有 30 亿次的下载量,包含超过 600000 个 包(package) (即,代码模块)。来自各大洲的开源软件开发者使用 npm 互相分享和借鉴。包的结构使您能够轻松跟踪依赖项和版本。
下面是关于 npm 的快速介绍:
npm 由三个独立的部分组成:
注册表(registry)
命令行工具 (CLI)
是开发者查找包(package)、设置参数以及管理 npm 使用体验的主要途径。
注册表 是一个巨大的数据库,保存了每个包(package)的信息。
通过命令行或终端运行。开发者通过 CLI 与 npm 打交道。
用 npm 可以 . . .
Adapt packages to your apps, or incorporate them as they are.
Download standalone tools you can use right away.
Run packages without downloading using .
Share code with any npm user, any where.
Restrict code to specific developers.
Form virtual teams (orgs).
Manage multiple versions of code and code dependencies.
Update applications easily when underlying code is updated.
Discover multiple ways to solve the same puzzle.
Find other developers who are working on similar problems.
These are just a few examples of ways developers use npm. If you'd like to add a bullet point, comment .
如何查找包(package)
To find packages, start with the npm .
While browsing, you'll find:
node modules, which can be used on the server side.
packages that add commands for you to use in the command line.
packages that can be used on the front end of websites.
(To understand the difference between node modules and packages, click ).
实例:查找一个包(package)
For example, suppose you wanted to use bar codes (QR codes) in your application. Rather than spend weeks figuring out how to do this, why not see if someone has posted a package that creates QR codes? Start by entering a value in the search bar:
Related options will appear:
Or, click, to filter the possibilities even more:
When you choose a package to explore, more information appears. This information is written by the package author(s) so details vary.
Now, you can read the developer's instructions to discover how to use this package.
Now that you know what npm is, and a bit about how to use it, it's time to get started.
. We look forward to seeing what you will create!
To learn more about npm as a product, new features on the way, and interesting uses of npm, sign up for our newsletter at .
To explore additional features that you might wish to use as your project evolves, click .
Last modified February 17, 2018
Found a typo? Send a
如何安装 npm 并管理 npm 版本
npm is written in Node.js, so you need to install Node.js in order to use npm. You can install npm via the Node.js website, or by installing a Node Version Manager or NVM. This chapter explains both options.
If you just want to get started exploring npm, using the Node.js installation method is fastest. If you are an advanced developer ready to jump in and work with versions, use the node version manager. If you aren't sure, please read this chapter before you decide. You can always change how you run npm in the future.
从 Node.js 网站安装 npm
1. 安装 Node.js 和 npm
If you're using OS X or Windows, use one of the installers from the . Be sure to install the version labeled LTS. Other versions have not yet been tested with npm.
If you're using Linux, you can find installers by scrolling on the , or, check
to see if there's a more recent version that works with your system.
After installing, run node -v. The version should be v8.9.1 or higher.
2. 更新 npm
When you install node.js, npm is automatically installed. However, npm gets updated more frequently than Node.js, so be sure that you have the latest version.
run npm -v.
To be sure that this matches the latest version, scroll to the bottom of this page.
If the version you see does not match the latest version, run:
npm install npm@latest -g.
This will install the latest official, tested version of npm.
To install a version that will be released in the future, run:
npm install npm@next -g.
Using a Version Manager to install Node.js and npm
Since npm and node.js products are managed by different entities, updates and maintenance can become complex. Also, the Node.js installation process installs npm in a directory that only has local permissions. This can cause permissions errors when you attempt to run packages globally.
To solve both these issues, many developers opt to use a node version manager, or nvm, to install npm. The version manager will avoid permissions errors, and will solve the complexities of updating Node.js and npm.
In addition, developers can use an nvm to test their applications on multiple versions of npm. The nvm enables you to easily switch npm as well as node versions. This makes it easier to ensure that your applications work for most users, even if they are using other versions of npm. If you decide to install a version manager, use the instructions for the version manager you select to learn how to switch versions, and to learn how to keep up-to-date with the latest version of npm.
Apple macOS
to learn how to install nvm for MacOs.
Microsoft Windows
To install and manage npm and Node.js on Windows, we suggest .
to learn how to install nvm for Linux.
Experimenting with the Next Release
For more advanced users
If you want to try the next, unreleased version of npm to test that packages you have created will work with the planned next release of npm, use this command:
npm install npm@next -g
This may simply reinstall the current version, depending on the development cycle. Also, this early version is not final. So features may or may not match what is ultimately released.
Learn More
To learn more about how to use nvm, click .
Last modified February 17, 2018
Found a typo? Send a
How to Prevent Permissions Errors
If you see an EACCES error when you try to install a package globally, read this chapter. This error can often be avoided if you change the directory where npm is installed. To do this, either:
Reinstall npm with a version manager (recommended),
Change npm's default directory manually.
Option One: Reinstall with a Node Version Manager
This is the best way to avoid permissions issues. This is described in . You do not need to remove your current version of npm or Node.js before installing a node version manager.
Option Two: Change npm's Default Directory
This section does not apply to Microsoft Windows. This chapter will be updated to address Windows in the near future.
To minimize the chance of permissions errors, you can configure npm to use a different directory. In this example, it will be a hidden directory on your home folder.
Back-up your computer before you start.
Make a directory for global installations:
mkdir ~/.npm-global
Configure npm to use the new directory path:
npm config set prefix '~/.npm-global'
Open or create a ~/.profile file and add this line:
export PATH=~/.npm-global/bin:$PATH
Back on the command line, update your system variables:
source ~/.profile
Test: Download a package globally without using sudo.
npm install -g jshint
Instead of steps 2-4, you can use the corresponding ENV variable (e.g. if you don't want to modify ~/.profile):
NPM_CONFIG_PREFIX=~/.npm-global
Tip: Consider npx
If you are using npm version 5.2 or greater, explore
as an alternative way to run global commands, especially if you just need a command occassionally. Click here to read an excellent article about .
Last modified February 17, 2018
Found a typo? Send a
如何安装本地包
有两种方式用来安装 npm 包:本地安装和全局安装。至于选择哪种方式来安装,取决于我们如何使用这个包。
如果你自己的模块依赖于某个包,并通过 Node.js 的 require 加载,那么你应该选择本地安装,这种方式也是 npm install 命令的默认行为。
如果你想将包作为一个命令行工具,(比如 grunt CLI),那么你应该选择。
想要了解更多关于 install 命令行的行为,可以查看 。
安装一个包
可以使用下面的命令来安装一个包:
& npm install &package_name&
上述命令执行之后将会在当前的目录下创建一个 node_modules 的目录(如果不存在的话),然后将下载的包保存到这个目录下。
为了确认 npm install 是正常工作的,可以检查 node_modules 目录是否存在,并且里面是否含有你安装的包的文件夹。
安装一个叫做 lodash 的包。安装成功之后,如果 node_modules 目录下存在一个名为 lodash 的文件夹,则说明成功安装了这个包。
Microsoft Windows:
C:\ npm install lodash
C:\ dir node_modules
#=& lodash
macOS, Ubuntu, Debian
& npm install lodash
& ls node_modules
#=& lodash
哪个版本的包会被安装了?
在本地目录中如果没有 package.json 这个文件的话,那么最新版本的包会被安装。
如果存在 package.json 文件,则会在 package.json 文件中查找针对这个包所约定的,然后安装符合此规则的最新版本。
使用已安装的包
一旦将包安装到 node_modules 目录中,你就可以使用它了。比如在你所创建的 Node.js 模块中,你可以 require 这个包。
创建一个名为 index.js 的文件,并保存如下代码:
&index.jsvar&lodash&=&require('lodash');&var&output&=&lodash.without([1,&2,&3],&1);console.log(output);
运行 node index.js 命令。应当输出 [2, 3]。
如果你没能正确安装 lodash,你将会看到如下的错误信息:
module.js:340
Error: Cannot find module 'lodash'
可以在 index.js 所在的目录中运行 npm install lodash 命令来修复这个问题。
Last modified February 17, 2018
Found a typo? Send a
Working with package.json
The best way to manage locally installed npm packages is to create a
package.json file.
A package.json file:
lists the packages that your project depends on.
allows you to specify the versions of a package that your project
can use using .
makes your build reproducible, and therefore much easier
to share with other developers.
Requirements
A package.json must have:
all lowercase
one word, no spaces
dashes and underscores allowed
in the form of x.x.x
For example:
&name&: &my-awesome-package&,
&version&: &1.0.0&
Creating a package.json
There are two basic ways to create a package.json file.
1. Run a CLI questionnaire
To create a package.json with values that you supply, run:
& npm init
This will initiate a command line questionnaire that will conclude with the
creation of a package.json in the directory in which you initiated the command.
2. Create a default package.json
To get a default package.json, run npm init with the --yes
or -y flag:
& npm init --yes
This method will generate a default package.json using information extracted from the current directory.
& npm init --yes
Wrote to /home/ag_dubs/my_package/package.json:
&name&: &my_package&,
&description&: &&,
&version&: &1.0.0&,
&main&: &index.js&,
&scripts&: {
&test&: &echo \&Error: no test specified\& && exit 1&
&repository&: {
&type&: &git&,
&url&: &https://github.com/ashleygwilliams/my_package.git&
&keywords&: [],
&author&: &&,
&license&: &ISC&,
&url&: &https://github.com/ashleygwilliams/my_package/issues&
&homepage&: &https://github.com/ashleygwilliams/my_package&
name: the current directory name
version: always 1.0.0
description: info from the readme, or an empty string &&
main: always index.js
scripts: by default creates an empty test script
keywords: empty
author: empty
bugs: info from the current directory, if present
homepage: info from the current directory, if present
You can also set several config options for the init command. Some useful ones:
& npm set init.author.email &&
& npm set init.author.name &ag_dubs&
& npm set init.license &MIT&
If there is no description field in the package.json, npm uses the first line of the README.md or README instead. The description helps people find your package when searching npm, so it's definitely useful to make a custom description in the package.json to make your package easier to find.
How to Customize the package.json questionnaire
If you expect to create many package.json files, you might wish to customize the questions asked during the init process, so that the files always contain key information that you expect. You can customize the fields as well as the questions that are asked.
To do this, you create a custom .npm-init.js in your home directory
~/.npm-init.js.
A simple .npm-init.js might look something like this:
module.exports = {
customField: 'Custom Field',
otherCustomField: 'This field is really cool'
Running npm init with this file in your home directory would output a package.json that included these lines:
customField: 'Custom Field',
otherCustomField: 'This field is really cool'
You can also customize the questions by using the prompt function.
module.exports = prompt(&what's your favorite flavor of ice cream, buddy?&, &I LIKE THEM ALL&);
To learn more about how to create advanced customizations, check out the docs for
Specifying Dependencies
To specify the packages your project depends on, you need to
list the packages you'd like to use in your package.json file. There are
2 types of packages you can list:
&dependencies&: These packages are required by your application in production.
&devDependencies&: These packages are only needed for development and testing.
Manually editing your package.json
You can manually edit your package.json. You'll need to create an attribute
in the package object called dependencies that points to an object. This object will hold attributes that name the packages you'd like to use. It will point to a
expression that specifies the versions of that project that are compatible with your project.
If you have dependencies you only need to use during local development,
follow the same instructions as above but use the attribute called devDependencies.
For example, the project below uses any version of the package my_dep that matches major version 1 in production and requires any version of the package my_test_framework that matches major version 3, but only for development:
&name&: &my_package&,
&version&: &1.0.0&,
&dependencies&: {
&my_dep&: &^1.0.0&
&devDependencies& : {
&my_test_framework&: &^3.1.0&
The --save and --save-dev install flags
The easier (and more awesome) way to add dependencies to your package.json is to do
so from the command line, flagging the npm install command with either --save or
--save-dev, depending on how you'd like to use that dependency.
To add an entry to your package.json's dependencies:
npm install &package_name& --save
To add an entry to your package.json's devDependencies:
npm install &package_name& --save-dev
Managing dependency versions
npm uses Semantic Versioning, or, as we often refer to it, SemVer, to manage versions and ranges of versions of packages.
If you have a package.json file in your directory and you run npm install, npm will look at the dependencies that are listed in that file and download the latest versions, using semantic versioning.
Learn More
To understand more about the power of package.json, see the video &Installing npm packages locally& which you can find in .
To learn more about semantic versioning, see .
Last modified February 17, 2018
Found a typo? Send a
如何更新本地安装的包
定期更新你的应用所依赖的包(package)是个好习惯。因为依赖包的开发者更新了代码,你的应用也就能够获得提升。
为了完成这个任务需要:
在 package.json 文件所在的目录中执行 npm update 命令。
执行 npm outdated 命令。不应该有任何输出。
Last modified February 17, 2018
Found a typo? Send a
如何卸载本地安装的包
如需删除 node_modules 目录下面的包(package),请执行:
npm uninstall &package&:
npm uninstall lodash
如需从 package.json 文件中删除依赖,需要在命令后添加参数 --save:
npm uninstall --save lodash
注意:如果你将安装的包作为 &devDependency&(也就是通过 --save-dev 参数保存的),那么 --save 无法将其从 package.json 文件中删除。所以必须通过 --save-dev 参数可以将其卸载。
为了确定 npm uninstall 命令执行成功,请找到 node_modules 目录,并在此目录下检查你所卸载的包(package)所对应的目录是否消失了。
通过如下命令:
ls node_modules 在 Unix 系统中使用,例如 &OSX&
dir node_modules 在 Windows 系统中使用。
安装 lodash。为了确认安装成功,请检查 node_modules 目录下是否存在 lodash 目录。
通过 npm uninstall 命令卸载 lodash。为了确认执行成功,请检查 node_modules 目录下不存在 lodash 目录。
安装 Lodash
& npm install lodash
& dir node_modules
# Unix 系统使用 `ls node_modules` 命令
卸载 Lodash
#=& lodash
& npm uninstall lodash
& dir node_modules
# Unix 系统使用 `ls node_modules` 命令
Last modified February 17, 2018
Found a typo? Send a
如何安装全局包
有两种方式用来安装 npm 包:本地安装和全局安装。选用哪种方式来安装,取决于你如何使用这个包。
如果你想将其作为一个命令行工具,那么你应该将其安装到全局。这种安装方式后可以让你在任何目录下使用这个包。比如 grunt 就应该以这种方式安装。
If you want to depend on the package from your own module, then install it . This is the choice you would use if you are using require statements, for example.
将包安装到全局,你应该使用 npm install -g &package& 命令,例如:
npm install -g jshint
如果你遇到 EACCES 错误,。
小技巧:如果你安装的 npm 是 5.2 或更高版本,可以使用 npx 运行全局安装的包。
Last modified February 17, 2018
Found a typo? Send a
如何更新全局安装的包
Requires version 2.6.1 or greater. See below if you are using an older version.
To update global packages, type:
npm update -g &package&
For example, to update a packages called jshint, you'd type:
npm update -g jshint
To find out which packages need to be updated, type:
npm outdated -g --depth=0.
To update all global packages, type:
npm update -g.
If you are using version 2.6.0 or less
For npm versions less than 2.6.1, run
to update all outdated global packages.
However, please consider upgrading to the latest version of npm. To do this, type:
npm install npm@latest -g.
Last modified February 17, 2018
Found a typo? Send a
如何卸载全局安装的包
通过如下命令将包(package)安装到全局:
npm uninstall -g &package&
例如安装 jshint 包到全局,使用如下命令:
npm uninstall -g jshint
Last modified February 17, 2018
Found a typo? Send a
如何创建 Node.js 模块
Node.js 模块是一种可以发布到 npm 的包。当你创建一个新模块时,创建 package.json 文件是第一步。
你可以使用 npm init 命令创建 package.json 文件。命令行中将会提示 package.json 字段中需要你输入的值。名称(name) 和 版本(version) 这两个字段是必填的。你还需要输入 入口文件字段(main) 字段,当然,可以使用默认值 index.js。这些步骤在 有详细的讲解。
如果你想为作者(author)字段添加信息,你可以使用以下格式(邮箱、网址都是选填的):
Your Name && (http://example.com)
创建 package.json 文件之后,还需要创建模块的入口文件。如果使用默认值,文件名是 index.js。
在此文件中,添加一个函数,作为 exports对象的一个属性。这样,require 此文件之后,这个函数在其他代码中就可以使用了。
exports.printMsg = function() {
console.log(&This is a message from the demo package&);
将你的包发布到 npm。
在你的项目外新建一个目录。
然后 cd 进入这个新目录。
执行 npm install &package& 命令。
创建一个 test.js 文件,require 这个包,并调用其中的方法。
执行 node test.js 命令。是否输出 console.log 中填写的信息?
To understand types of packages, click .
Last modified February 17, 2018
Found a typo? Send a
How to Publish & Update a Package
You can publish any directory that has a package.json file. This chapter explains how to publish a package for the first time, and how to update it later.
How to Publish a Package
Preparation
Understand npm policies
Before you begin, it's a good idea to review npm's policies, in case you have questions about site etiquette, naming, licensing, or other .
Create a User Account
To publish, you must be a user on the npm registry. If you aren't a user, create an account by using
npm adduser. If you created a user account on the site, use npm login to access your account from your terminal.
Type npm whoami from a terminal to see if you are already logged in (technically, this also means that your credentials have been stored locally).
Check that your username has been added to the registry at .
For example,
https://www.npmjs.com/~carolynawombat
Review the Package Directory
Review the Contents
Note that everything in the directory will be included unless it is ignored by a local .gitignore or .npmignore file. To learn how to use these commands, see .
Review the package.json File
to be sure that the details you want are reflected in your package.
Choose a name
Choose a unique name for your package. Try to choose a descriptive name that:
isn't already owned by somebody else
is not spelled like another name, except with typos
will not confuse others about the authorship
meets . For example, do not name your package something offensive, and don't use someone else's trademarked name.
Specify the name in the appropriate line of the package.json file.
Note: The first 3 caveats don't apply if you are using .
Include Documentation (readme.md)
npm recommends that you include a readme file to document your package. The readme file must have the filename readme.md.
The file extension .md indicates that the file is a
file. This file will appear on the npm website when someone finds your package.
Before you begin, please look at some of the package pages to get ideas for the information you can add to your readme file, and to see why this is so important.
Create a file using any text editor.
Save it in the project directory with the name readme.md
When you publish, this documenation will display on the web page where people download your package.
Use npm publish to publish the package.
Go to https://npmjs.com/package/&package&. You should see a page all about your new package. It might look a bit like this:
Congratulations!
How to Update a Package
How to Update the Version Number
When you make changes, you can update the package using
npm version &update_type&
where &update_type& is one of the
release types, patch, minor, or major.
This command will change the version number in package.json.
Note: this will also add a tag with the updated release number to your git repository if you have linked one to your npm account.
After updating the , run npm publish again.
Test: Go to https://npmjs.com/package/&package&. The package number should be updated.
How to Update the Read Me File
The README displayed on the site will not be updated unless a new version of your package is published, so you need to run npm version patch and npm publish to update the documentation displayed on the site.
Learn More
To find out more about node modules and packages, see .
To learn about semantic versioning, click .
To learn more about tags, click .
To learn more about package.json files, click .
To learn more about naming, including how npm protects you against typosquat confusion, click
Last modified February 17, 2018
Found a typo? Send a
如何使用语义化版本
It's important to communicate the extent of changes in a new release of code, because sometimes updates can break code that a package needs (called dependencies).Semantic versioning (semver) is a standard that was designed to solve this problem.
Semver for Publishers
If a project is going to be shared with others, it should start at 1.0.0, (though some projects on npm don't follow this rule).
After this, changes should be handled as follows:
Semver for Consumers
针对使用者的语义化版本
If you were starting with a package 1.0.4, this is how you would specify the ranges:
Patch releases: 1.0 or 1.0.x or ~1.0.4
Minor releases: 1 or 1.x or ^1.0.4
Major releases: * or x
Learn More
For more about using semantic versioning with package.json files, see .
For another way to label releases, learn about , and .
Last modified February 17, 2018
Found a typo? Send a
How to Work with Scoped Packages
Requires npm version 2 or greater
Scopes are used to group related packages together, and to create a namespace, like a domain, for npm modules. This is explained in more detail .
If a package's name begins with @, then it is a scoped package. The scope is everything in between the @ and the slash.
@scope/project-name
Each npm user has their own scope.
@username/project-name
You can find more in depth information about scopes in the .
How to Initialize a Scoped Package
To create a scoped package, you simply use a package name that starts with your scope.
{&&&name&:&&@username/project-name&}
If you use npm init, you can add your scope as an option to that command.
npm init --scope=username
If you use the same scope all the time, you will probably want to set this option in your
npm config set scope username
Publishing a Scoped Package
By default, scoped packages are private. To publish private modules, you need to be a paid
Public scoped modules are free and don't require a paid subscription. To publish a public scoped module, set the access option when publishing it. This option will remain set for all subsequent publishes.
npm publish --access=public
Using a Scoped Package
To use a scoped package, simply include the scope wherever you use the package name.
In package.json:
{&&&dependencies&:&{&&&&&@username/project-name&:&&^1.0.0&&&}}
On the command line:
npm&install&@username/project-name&--save
In a require statement:
var&projectName&=&require(&@username/project-name&)
For information about using scoped private modules, visit .
Last modified February 17, 2018
Found a typo? Send a
How to Label Packages with Dist-tags
Distribution tags (dist-tags) supplement
(e.g., v0.12). Use them to
organize and label different versions of packages. In addition to being more human-readable than semver numbering, tags allow publishers to distribute their packages more effectively.
Adding tags
To add a tag to a specific version of your package, use:
npm dist-tag add &pkg&@&version& [&tag&]
Publishing with tags
By default, npm publish will tag your package with the latest tag. If you
use the --tag flag, you can specify another tag to use. For example, the
following will publish your package with the beta tag:
npm publish --tag beta
Installing with tags
Like npm publish, npm install &pkg& will use the latest tag by default.
To override this behavior, use npm install &pkg&@&tag&. The following example will install the somepkg at the version that has been tagged with beta.
npm install somepkg@beta
Because dist-tags share the same namespace as semver, avoid using tag
names that may cause a conflict. The best practice is to avoid using tags
beginning with a number or the letter &v&.
Learn More
for more about tags.
for more about semantic versioning.
Last modified February 17, 2018
Found a typo? Send a
How to Use Two-Factor Authentication
Requires npm version 5.5.1 or greater
To meet the increasing need for strong digital security, npm introduced two-factor authentication (2FA) with version 5.5.1. Two-factor authentication prevents unauthorized access to your account by confirming your identity using two methods:
something you know (such as your username and password)
something you have (such as a phone or tablet)
For example, if your bank uses 2FA, the first time you logged in to your online banking system, the bank sent a code to your cell phone number, then prompted you to enter the code online. This proved that the cell phone was in your possession, and linked it to your account for authentication. After that, whenever the bank detects anything unusual, such as a login from a different laptop, it will send a temporary code to your phone that you must enter before you can login. This provides an extra layer of security because, even if someone obtains your login credentials, they are unlikely to have your device in their possession as well. Even if someone finds your phone, they are unlikely to also be able to hack your laptop's password.
Two-factor authentication multiplies the protection against attacks, and we recommend that you implement this with your npm account.
Preparation
To enable 2FA with your npm account, you will need an application that can generate a One Time Password, or OTP. For example,
, can generate one time passwords (OTP's).
These products use a Time-Based One-Time Password Algorithm (TOTP) to create temporary codes.
Install the application on a mobile device or a second laptop that will always be available when you work in your npm account. (Note: npm does not use SMS (text-to-phone) as a method for authenticating users.)
Levels of Authentication
There are two levels of authentication, auth-only and auth-and-writes.
If you enable 2FA in auth-only mode, npm will require an OTP when you:
remove 2FA
If you enable 2FA in auth-and-writes mode, which is the default, npm will require an OTP when you:
change your profile
create or revoke tokens
publish packages
change access
change your password
make other sensitive changes to packages
remove 2FA
To add the OTP to a command, append it as shown:
npm owner add &user & --otp=123456
Other examples are listed below.
How Do I Enable 2FA?
To require two-factor authentication, type the command that meets the level of security you wish to apply (auth-and-writes is the default).
npm profile enable-2fa
npm profile enable-2fa auth-and-writes
npm profile enable-2fa auth-only
npm will return this message:
npm notice profile Enabling two factor authentication for auth-and-writes
or this message:
& npm notice profile Enabling two factor authentication for auth-only
depending on the setting you provided.
Next, npm will display a QR code:
Add a new account to your authenticator app.
Scan the QR code, or enter the number displayed just below the QR code.
This will configure the authenticator app for future use, linking authentication to the device that generated the authentication.
Using your authenticator app, enter an OTP at the prompt shown:
Add an OTP code from your authenticator:
After you have entered the one-time password,
npm will display this message:
2FA successfully enabled.
Below are your recovery codes, please print these out.
You will need these to recover access to your account
if you lose your authentication device.
After you have applied two-factor authentication, you can use the npm profile get command to confirm that it has been set.
Example: Setting Profile Values after Enabling 2fa
Once you have installed 2FA, you will need to enter an OTP for security-relevant commands. For example, whenever you use the command npm profile set you will need to enter an OTP before you can set or change a value, such as your fullname:
$npm profile set fullname Carolyn A. Wombat
Enter OTP: 567452
Set fullname to Carolyn A. Wombat
Use `npm profile get' to confirm the new setting.
Note to our readers: We have reset the account
neither the QR nor the codes are still active. But thank you to those alert and kind wombats who have asked us about this.
Recovery Codes
As described above, after you set up two-factor authentication, a series of recovery codes will appear on your screen. Please print them and save them as described. Note: Some authenticator applications provide a method for you to store recovery codes.
Tip: Save these codes in a different location than the device you use to authenticate. For example, if you get your OTP from a tablet, don't save the codes in a case with your tablet.
The recovery procedure is explained below.
How to Remove Two-Factor Authentication from your Profile
To remove 2FA from your profile, type this command:
npm profile disable-2fa
npm will prompt for your password:
& npm password:
Enter your npm password as prompted, then npm will display:
&Enter one-time password from your authenticator: 123456
npm will confirm:
Two factor authentication disabled.
How to Send an OTP Value from the Command Line
If you have enabled 2FA auth-and-writes, you will need to send the OTP from the command line for certain commands. To do this, append
--otp=123456 (where 123456 is the code generated by your authenticator) at the end of the command. Here are a few examples:
npm publish [&tarball&|&folder&][--tag &tag&] --otp=123456
npm owner add &user & --otp=123456
npm owner rm &user& --otp=123456
npm dist-tags add &pkg&@&version& [&tag&] --otp=123456
npm access edit [&package&) --otp=123456
npm unpublish [&@scope&/]&pkg&[@&version&] --otp=123456
What to Do if You Misplace Your Second Device
If you cannot locate the device that provided second-factor authentication:
Find the recovery codes you saved when you enabled 2FA.
If you are logged out, login normally using your login and npm password. When prompted for an OTP, enter a recovery code. (Note that this might create a second authenticator account with the same npm account name. Be sure to use the newly created account.)
Once you are logged in, type npm profile disable-2fa and enter your npm password if prompted.
Enter an unused recovery code when you see this prompt:
&Enter one-time password from your authenticator:
npm will confirm that two-factor authentication has been disabled.
type npm profile enable-2fa to re-enable 2FA, assign a different device to your account, and generate new recovery codes.
If you have misplaced your recovery codes, please contact npm customer support.
What if You See an Error after Entering the OTP?
If you are entering what seems to be a valid OTP but you see an error, be sure that you are using the correct authenticator account. In the screen shot below, the current account in Authy was set incorrectly because the developer had multiple npm test accounts. This will cause the OTP to fail. Also, as stated earlier, when you reset 2fa after it has been disabled, the authenticator might create a second account with the same name. Please see the authenticator documentation to delete the old account.
Settings you define using the Command Line Interface (CLI) will also apply to the website. At this time, you cannot activate 2FA from web interface.
Last modified February 17, 2018
Found a typo? Send a
How to Work with Security Tokens
Requires npm version 5.5.1 or greater
Whenever you login to npm, a security token is generated. Tokens authenticate your account, and give you rights to publish and access your modules.
Because a token is simply a hexadecimal string, you can use the token in other tools, such as continuous integration testing environments, so that the tool can run with the access it needs to complete tasks. For example, Travis-CI provides an environment variable that you can assign to a token value. This gives Travis-CI the ability to run npm as your npm user, including the ability to install private packages to which you have access.
Read this chapter to learn how to manage security tokens.
Note: There are additional steps required if you wish to use tokens for testing and other special purposes. These steps are not in the scope of this chapter.
Token commands empower you to:
View tokens for easier tracking and management.
Create new tokens, specifying read-only or full-permission.
Delete/revoke tokens.
Limit access according to IP address ranges (CIDR).
How to View the Tokens On Your Account:
To view the tokens associated with your account, type:
npm token list.
The following table explains the token list.
A token can be both read-only as well as CIDR-whitelisted.
If you have enabled two-factor authentication on your profile, you have implemented an additional layer of security.
Note: The token list shows a truncated version of the token.
if you need to see the full token.
How to Create New Tokens
npm token create [--read-only] [--cidr=list]
Before you create a new token, decide which type of token you want:
read-only (installation/distribution rights)
full permission (publishing rights)
CIDR whitelist (restricted by ip address)
The default setting for new tokens is full-permission.
Read-only tokens allow installation and distribution.
Full-permission tokens allow installation, distribution, publishing, and all rights that you have on your account
CIDR whitelist tokens can only be used from specified ip address ranges. Use this to restrict tokens to a single company, or a specified developer team, for example. At this time, only IPv4 is supported.
When a token is read-only, it cannot be used to make changes to a package. If a token is not explicitly set to read-only, it has full permissions, including publish and modification rights.
How to Create a New Full-Permission token:
To create a new full permission token, type:
'npm token create'
If you have set up two-factor authentication, you will be prompted for your npm password, followed by an OTP. npm will display this table:
TIP: Save a screen shot of the token field, as this will be your only chance to view it.
Note that read-only defaults to false.
How to Create a New Read-Only Token
To create a new read-only token, type:
npm token create --read-only
If you have set up two-factor authentication, you will be prompted for an npm password, followed by an OTP. npm will display this table:
Note that read-only is set to true.
How to Create a New CIDR-Restricted Token
To limit the token so that it can only be used from specified ip addresses, you can create a CIDR-restricted token. CIDR is an acronym for Classless Inter-Domain Routing. The
will get you started.
Using a CIDR restriction makes it possible for you to force anyone or any system using the token to either physically or remotely be within the specified ip address range.
npm token create --[--cidr=list]
npm token create --cidr=192.0.2.0/24
If you have set up two-factor authentication, you will be prompted for an npm password, followed by an OTP. npm will display this table:
If you see a message like this one:
npm ERR! CIDR whitelist contains invalid CIDR entry: X.X.X.X./YY,Z.Z.. . .
(where the string returned is the one you entered) please ensure that the CIDR string is valid and in the appropriate format. Note: npm only supports IPv4 at this time.
How to Create a CIDR-Restricted Read-Only Token
To create a CIDR-restricted token that is also read-only, type:
npm token create --read-only --cidr=list
How to Revoke Tokens
You can delete (revoke) a token, regardless of when it was created. This allows you to gain control of access you may wish to take back.
The command to delete a token is:
npm token delete
Here are the steps:
Type npm token list
Find the token ID affiliated with the token you want to delete.
Type 'npm token delete 123456', where 123456 is the token id. Note: The truncated version of the token will not work.
npm will report 'Removed 1 token'
Type 'npm token list' to confirm that the token has been removed.
The following screen shot demonstrates these steps:
Note: All tokens shown in screen illustrations have been revoked.
Note: In certain cases, there may be a delay of up to an hour before a token is successfully revoked. npm is currently working to make revocation occur immediately in all cases.
Last modified February 17, 2018
Found a typo? Send a
How to Change Profile Settings from the CLI
Requires npm version 5.5.1 or greater.
To view and set profile properties from the Command Line Interface (CLI), use these commands:
npm profile get
npm profile set &prop& &value&
Viewing & Setting Profile Values
To see your current profile settings, type:
npm profile get
npm displays your profile settings in a table:
You can set or modify the following profile properties from the command line:
How to Set a Password from the Command Line
To set a password, type:
npm profile set password
npm will prompt you for your current password, then you can set the new password.
The password must follow these guidelines:
longer than 7 characters
does not contain part of your username
is not one of these
These rules protect your password by making it difficult to guess.
How to Set Other Profile Properties
To set other values, append them to the end of the line as shown:
$npm profile set fullname nori pat marsupial
npm will prompt for credentials, including a one-time-password if you have enabled two-factor authentication. npm will then
confirm that the change has occurred:
Set fullname to nori pat marsupial
Enabling and Disabling Two-Factor Authentication
You can also enable and disable two-factor authentication from the command line, using the steps described .
Enable two-factor authentication if you would like to make it more difficult for attackers to gain access to your account and your publishing rights.
Please refer to the
for more details and examples.
Last modified February 17, 2018
Found a typo? Send a
Understanding Packages and Modules
Node.js and npm have very specific definitions of packages and modules, which are easy to mix up. We'll discuss those definitions here, make them distinct, and explain why certain default files are named the way they are.
Quick Summary
A package is a file or directory that is described by a package.json.
This can happen in a bunch of different ways! For more info, see
A module is any file or directory that can be loaded by Node.js'
require(). Again, there are several configurations that allow this to
happen. For more info, see , below.
What is a package?
A package is any of the following:
a) A folder containing a program described by a package.json file.
b) A gzipped tarball containing (a).
c) A url that resolves to (b).
d) A &name&@&version& that is published on the registry with (c).
e) A &name&@&tag& that points to (d).
f) A &name& that has a latest tag satisfying (e).
g) A git url that, when cloned, results in (a).
Noting all these package possibilities, it follows that even if you never
publish your package to the public registry, you can still get a lot of
benefits of using npm:
If you just want to write a node program, and/or,
If you also want to be able to easily install it elsewhere after packing
it up into a tarball.
Git urls can be of the form:
git://github.com/user/project.git#commit-ish
git+ssh://user@hostname:project.git#commit-ish
git+http://user@hostname/project/blah.git#commit-ish
git+https://user@hostname/project/blah.git#commit-ish
The commit-ish can be any tag, sha, or branch which can be supplied as
an argument to git checkout. The default is master.
What is a module?
A module is anything that can be loaded with require() in a Node.js
program. The following are all examples of things that can be
loaded as modules:
A folder with a package.json file containing a main field.
A folder with an index.js file in it.
A JavaScript file.
Most npm packages are modules
Generally, npm packages that are used in Node.js program are loaded
with require, making them modules. However, there's no requirement
that an npm package be a module!
Some packages, e.g., cli packages, only contain an executable
command-line interface and don't provide a main field for use in
Node.js programs. These packages are not modules.
Almost all npm packages (at least, those that are Node programs)
contain many modules within them (because every file they load with
require() is a module).
In the context of a Node program, the module is also the thing that
was loaded from a file. For example, in the following program:
var req = require('request')
we might say that &The variable req refers to the request module&.
File and Directory Names in the Node.js and npm Ecosystem
So, why is it the node_modules folder, but package.json file?
Why not node_packages or module.json?
The package.json file defines the package. (See
The node_modules folder is the place Node.js looks for modules.
(See , above.)
For example, if you create a file at node_modules/foo.js and then
had a program that did var f = require('foo.js'), it would load
the module. However, foo.js is not a &package& in this case
because it does not have a package.json.
Alternatively, if you create a package which does not have an
index.js or a &main& field in the package.json file, then it is
not a module. Even if it's installed in node_modules, it can't be
an argument to require().
Last modified February 17, 2018
Found a typo? Send a
Working with private modules
With npm private modules, you can use the npm registry to host your own private code and the npm command line to manage it. This makes it easy to use public modules like Express and Browserify side-by-side with your own private code.
Before we start
You need a version of npm greater than 2.7.0, and you'll need to log in to npm again.
sudo&npm&install&-g&npmnpm&login
Setting up your package
All private packages are scoped.
Scopes are a new feature of npm. If a package's name begins with @, then it is a scoped package. The scope is everything in between the @ and the slash.
@scope/project-name
When you sign up for private modules as an individual user, your scope is your username.
@username/project-name
If you use npm init to initialize your packages, you can pass in your scope like this:
npm init --scope=&your_scope&
If you use the same scope most of the time, you'll probably want to set it in your default configuration instead.
npm config set scope &your_scope&
Publishing your package
Publishing your package is easy.
npm publish
By default, scoped packages are published as private. You can read more about this in the .
Once it's published, you should see it on the website with a private flag.
Giving access to others
If you want to give access to someone, they need to be subscribed to private modules as well. Once they are, you can give them read or read-write access.
You can control access to the package on the access page. To get to the page, click on the Collaborators link or the plus button.
Add collaborators by entering the username and hitting enter.
You can also add collaborators on the command line:
npm&owner&add&&user&&&package&name&
Installing private modules
To install a private module, you must have access to the package. Then you can use install with the scoped package name.
npm install @scope/project-name
You also use the scoped package name when requiring it.
var project = require('@scope/project-name')
Switching from private to public
All scoped packages default to private. This ensures that you don't make something public by accident. You can change this on the access page.
You can also manage package access via the command line:
npm&access&restricted&&package_name&
The package will be removed from listings on the site within a few minutes of making it private.
Last modified February 17, 2018
Found a typo? Send a
Downloading modules to CI/deployment servers
If you are using deployment servers or testing with CI servers, you'll need a way to download your private modules to those servers. To do this, you can set up an
file which will authenticate your server with npm.
Getting an authentication token
One of the things that has changed in npm is that we now use auth tokens to authenticate in the CLI. To generate an auth token, you can log in on any machine. You'll end up with a line in your
file that looks like this:
//registry.npmjs.org/:_authToken=0-
The token is not derived from your password, but changing your password will invalidate all tokens. The token will be valid until the password is changed. You can also invalidate a single token by logging out on a machine that is logged in with that token.
Setting up environment variables
To make this more secure when pushing it up to the server, you can set this token as an environment variable on the server. For example, in He}

我要回帖

更多关于 ptaglobal什么意思 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信