1. 首页
  2. >
  3. 前端开发
  4. >
  5. Vue

Windows下安装npm本地化仓库Sinopia及问题处理

采用Node.js开发本地项目,有时不同项目之间存在依赖,如果不想把项目发布到npm社区的仓库,则需要有自己本地的仓库。本地仓库候选方案有:sinopia,cnpm和kappa。

sinopia特点

  1. 零配置安装
  2. 使用文件系统作为存储,仅保存用户需要的包,如果本地仓库没有对应的包,则从指定的registry下载,默认为npmjs.org。

安装

以下是安装过程以及遇到的问题

安装环境:

  • windows: 10
  • node: 10.7.0
  • npm:6.1.0
  • python: 2.7

Sinopia的安装比较简单,只需使用npm一条安装命令即可:


npm install -g sinopia

安装问题

在windows下直接执行这个命令会遇到一些问题:

1、 python没有安装或版本不对

gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable.

node-gyp依赖python 2.7。安装python2.7,并把它添加到环境变量PATH,npm config set python python2.7

2、MSBuild版本不对

MSBUILD : error MSB4132: The tools version "2.0" is unrecognized. Available too ls versions are "4.0"

node-gyp需要用到Visual C++ Build Tools ,安装Visual Studio Express 2013 for Windows Desktop可以解决。

3、windows下不支持fs-ext和crypt3

node-gyp报编译fs-ext和crypt3失败的错误,安装Sinopia时可以忽略。错误信息如下:

fs-ext.cc(195): error C3861: 'fcntl': identifier not found [C:\Users\clcaza\AppData\Roaming\npm\node_modules\sinopia\node_modules\.0.6.0@fs-ext\build\fs-ext.vcxproj]
以及



crypt3.cc(5): fatal error C1083: Cannot open include file: 'unistd.h': No such file or directory [C:\Users\clcaza\AppData\Roaming\npm\node_modules\sinopia\node\_modules\.0.2.0@crypt3\build\crypt3.vcxproj]
sinopia模块sinopia-htpasswd依赖于fs-ext和crypt3,但这两个包是可选的,查看sinopia源码里的package.yaml可以看到



optionalDependencies:
  # those are native modules that could fail to compile
  # and unavailable on windows
  fs-ext: '>=0.4.1 <1.0.0-0'
  crypt3: '>=0.1.6 <1.0.0-0' # for sinopia-htpasswd

在windows,删除sinopia安装目录node_modules里的fs-ext和crypt3相关的包,否则执行npm添加用户和登陆验证时会报错。包括:.0.2.0@crypt3,.0.6.0@fs-ext,crypt3和fs-ext以及sinopia-htpasswd\node_modules下的crypt3和fs-ext

sinopia使用

启动

$ sinopia
warn  --- config file  - C:\Users\clcaza\AppData\Roaming\sinopia\config.yaml
warn  --- http address - http://localhost:4873/

# npm configuration
$ npm set registry http://localhost:4873/
sinopia启动时默认会创建config.yaml文件,文件路径可以看输出的提示。可以使用-c选项指定配置文件。



$ sinopia -c D:\sinopia\config.yaml
添加用户



$npm adduser --registry http://localhost:4873/
Username: dwu
Password: dwu
Email: (this IS public) cc@dwu.cc
如果没有删除crypt3,添加用户时会报错:



error --- unexpected error: Cannot find module './build/Release/crypt3'
添加完用户后,可以使用npm 登陆



$npm login
Username: clc
Password: clc
Email: (this IS public) clc@cc.com
Logged in as clc on http://localhost:4873/.

这样你就可以使用sinopia来发布了。

sinopia配置

sinopia默认配置如下

# 存储的路径
storage: ./storage

auth:
  htpasswd:
    #使用htpasswd保存用户验证信息
    file: ./htpasswd
    # Maximum amount of users allowed to register, defaults to "+inf".
    # You can set this to -1 to disable registration.
    #max_users: 1000

# a list of other known repositories we can talk to
uplinks:
  npmjs:
    #如果sinopia本地仓库不存在包时,默认下载的npm仓库,国内可以配置淘宝的 https://registry.npm.taobao.org
    url: https://registry.npmjs.org/

packages:
  '@*/*':
    # scoped packages
    access: $all
    publish: $authenticated

  '*':
    # allow all users (including non-authenticated users) to read and
    # publish all packages
    #
    # you can specify usernames/groupnames (depending on your auth plugin)
    # and three keywords: "$all", "$anonymous", "$authenticated"
    access: $all

    # allow all known users to publish packages
    # (anyone can register by default, remember?)
    publish: $authenticated

    # if package is not available locally, proxy requests to 'npmjs' registry
    proxy: npmjs

# log settings
logs:
  - {type: stdout, format: pretty, level: http}
  #- {type: file, path: sinopia.log, level: info}

sinopia对npm的支持

sinopia目前支持下面npm的命令:

  • publish
  • unpblish
  • install
  • adduser

参考: