下面通过三种方法来搭建公司私有npm仓库,每种方式都有自己的优势。
Node.js >= 6.11.3,我的Node版本:node v8.2.1
Linux or OSX,我的系统版本:CentOS Linux release 7.2.1511 (Core)
npm install -g --build-from-source cnpmjs.org cnpm sqlite3
# 如果报错或者警告通过下面方式安装
npm install -g --unsafe-perm --verbose --build-from-source cnpmjs.org cnpm sqlite3
如果安装不流畅通过下面形式安装:
npm install -g --build-from-source \
--registry=https://registry.npm.taobao.org \
--disturl=https://npm.taobao.org/mirrors/node \
cnpmjs.org cnpm sqlite3
如果报警告或者安装错误,请添加参数--unsafe-perm --verbose
管理员:myname,othername
范围:my-company-name,other-name
默认端口:7001-registry, 7002-web
启动服务
$ nohup cnpmjs.org start --admins='myname,othername' \
--scopes='@my-company-name,@other-name' &
将cnpm默认注册地址更改为私有注册地址
cnpm set registry http://localhost:7001
$ cnpm login
Username: myname
Password: ***
Email: (this IS public) test@test.com
新建项目
$ cd /tmp
$ mkdir helloworld && cd helloworld
$ cnpm init
name: (helloworld) @my-company-name/helloworld
version: (1.0.0)
{
"name": "@my-company-name/helloworld",
"version": "1.0.0",
"description": "my first scoped package",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
上传到私有仓库
$ cnpm publish
+ @my-company-name/helloworld@1.0.0
浏览器中预览
open http://localhost:7002/@my-company-name/helloworld
使用cnpm预览
cnpm info
所有公共包都可直接使用cnpm安装
cnpm install hotkeys-js
verdaccio 是一个轻量级的私有npm代理注册。(sinopia fork)
# 使用 npm 安装
npm install -g npm
# 使用 yarn 安装
yarn global add verdaccio
verdaccio >> verdaccio.log 2>&1 & # 后台启动并写入日志
# Verdaccio doesn't need superuser privileges. Don't run it under root.
# warn --- config file - /root/.config/verdaccio/config.yaml
# warn --- http address - http://localhost:4873/ - verdaccio/2.3.6
verdaccio --listen 4000 --config ./config.yaml # 指定配置启动
npm adduser --registry http://localhost:4873
npm publish --registry http://localhost:4873
npm config list -l # 查看默认配置
# 将默认地址 https://registry.npmjs.org/ 改成私有地址
npm set registry http://localhost:4873
# 如果您使用HTTPS,请添加适当的CA信息
#(“null”表示从操作系统获取CA列表)
$ npm set ca null
这个方法得益于,npm提供的的丰富安装方法。通过下面方法安装:
npm i -S git+ssh://git@git.showgold.cn:npm/hello.git
npm install -S git+ssh://git@github.com:npm/npm.git#v1.0.27
npm install -S git+ssh://git@github.com:npm/npm#semver:^5.0
npm install -S git+https://isaacs@github.com/npm/npm.git
npm install -S git://github.com/npm/npm.git#v1.0.27
上面安装需要注意:你的工程一定是在某一个组下面建立,方便管理,在生成你的包的时候package.json中的name一定要带上范围
# 假设你建立了一个Git仓库,先克隆下来
git clone http://git.your-inc.com/companyfe/hello-private.git
# 生成 `package.json` 配置, 注意限定 `@scope` 范围
npm init --scope=companyfe
# 提交到仓库
git push origin master
将得到如下依赖,注意:
name字段必须限定范围,一般为 GitLab group 的名字, 例如 @companyfe, 那么 name 为: @companyfe/hello-private。
private 设为 true 防止将私有模块上传到公网上去,需要手动设置一下。
{
"name": "@companyfe/hello-private",
"version": "1.0.1",
"description": "",
"main": "index.js",
"private":true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "kenny wang <wowohoo@qq.com> (http://wangchujiang.com)",
"license": "ISC"
}
跟安装开源的模块一样, 使用 npm install 安装依赖即可. 私有模块会安装在 @scope 的子文件夹中, 例如: node_modules/@companyfe/hello-private.
# 基础安装
npm i -S git+ssh://git@git.your-inc.com/companyfe/hello-private.git
# 带版本信息的,必须通过 git 打 tag
npm i -S git+ssh://git@git.your-inc.com/companyfe/hello-private.git#v1.2.0
将得到如下依赖
{
"name": "helloworld",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@companyfe/hello-private": "git+ssh://git@git.your-inc.com/companyfe/hello-private.git#v1.2.0"
},
"author": "kenny wang <wowohoo@qq.com> (http://wangchujiang.com)",
"license": "ISC"
}
使用私有模块
var hello = require('@companyfe/hello-private');
不好的地方是,使用 npm update 是无法更新私有模块,想更新只能重新安装一次。好处是不用搭建服务。
参考资料:Can I run my own private registry?
来源:在5分钟内搭建企业内部私有npm仓库
这篇文章主要介绍了vue项目中Npm run build 根据环境传递参数方法来打包不同域名,使用npm run build --xxx,根据传递参数xxx来判定不同的环境,给出不同的域名配置,具体内容详情大家参考下:config文件夹下dev.env.js中修改代码、prod.env.js中修改代码 HOST为截取到的参数
从架构的角度来看,将大型单体代码库拆分为较小的、独立封装的一系列模块通常是个好方法。从微服务到可复用组件库,很多技术都很适合模块化。但从版本发布和源代码管理的角度来看,它也可能是一场噩梦。
什么是 npm 脚本?npm 允许在package.json文件里面,使用scripts字段定义脚本命令。上面代码是package.json文件的一个片段,里面的scripts字段是一个对象。
如果您曾在 Node 或 JavaScript 前端开发中投入过时间和精力,那么您就知道 npm 中有数以十万计的模块可供您选择。挑选模块可能很难,但您只需要一些方法点来解决它。当您正在为如何抉择浪费时间,或者甚至不知道从哪里开始时,请使用本指南来帮助您。
npm 为你和你的团队打开了连接整个 JavaScript 天才世界的一扇大门。它是世界上最大的软件注册表,每星期大约有 30 亿次的下载量,包含超过 600000 个 包(package) (即,代码模块)。来自各大洲的开源软件开发者使用 npm 互相分享和借鉴
安装angular cli工具时,发现进度条一直卡住不动,相信很多朋友也遇到过。原因应该是国内的网络连接npm速度较慢,甚至很多东西都无法下载安装。那么如何解决这个问题呢?
将安装包放在 ./node_modules 下(运行 npm 命令时所在的目录),如果没有 node_modules 目录,会在当前执行 npm 命令的目录下生成 node_modules 目录。可以通过 require() 来引入本地安装的包。
Yarn 是 Facebook, Google, Exponent 和 Tilde 开发的一款新的 JavaScript 包管理工具。它的目的是解决这些团队使用 npm 面临的少数问题,即:安装的时候无法保证速度/一致性,安全问题,因为 npm 安装时允许运行代码
在mylib文件夹下,src文件夹是让你写源码的,example是让你开发调试用的文件夹,里面也是源码(使用你npm包的源码),dist是你编译后的输出目录,在npm pub时就会把dist上传到npm上
一般的node工程,官方提供npm link的方式本地调试。具体步骤如下: 我有私库rn-lib 和工程rn-demo,原理是:将../rn-lib下面的代码copy一份到rn-demo/node_modules/rn-lib
内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!