基于 Verdaccio 搭建鸿蒙(HarmonyOS Next)开发的轻量级 Node.js 私有仓库

一、背景

最近在进行 HarmonyOS Next 应用开发,官方的 DevEco Studio 4.1 需要时候 Node.js,但是公司开发环境不支持外网访问,需要搭建内网的镜像服务器。下面,我们研究在内网服务器只使用 Apache(HTTPD)/Nginx 提供文件下载服务,不安装 NodeJs 搭建代理服务的方法来建立 NPM 文件下载代理。

执行缓存任务的设备是 MacBook Pro 2023 / macOS Sonoma 14.3

二、简介

1. 什么是 Verdaccio

“一个基于 Node.js 的轻量级私有仓库”。
平时使用 npm publish 进行发布时,上传的仓库默认地址是 npm,通过 Verdaccio 工具在本地新建一个仓库地址,再把本地的默认上传仓库地址切换到本地仓库地址即可。当 npm install 时没有找到本地的仓库,则 Verdaccio 默认配置中会从 npm 中央仓库下载。

注:Verdaccio 表示意大利中世纪晚期 fresco 绘画中流行的一种绿色的意思。

2. 优点
  • 私密性高,仅团队共享。
  • 安全性高,能够有效的防治恶意代码攻击。
  • 使用局域网,传输速度快。
3. 官网

三、准备环境

# 我们通过 nvm 管理 node 进行多版本切换 
$ brew install nvm

# 加载并且列出远程的 node 分支,否则执行 `nvm list` `nvm install` 等命令的
# 时候没办法列出或者安装对应的版本

$ nvm ls-remote

# 但是我们使用最新版本的 node 执行安装操作,这样才能干净的进行缓存
# 否则在我们后续安装 verdaccio 的时候,数据是已经缓存过的了
$ nvm install 20.11.0

# 没有特殊情况下,建议把刚刚安装的版本设置为默认版本
$ nvm alias default 20.11.0

# 安装 verdaccio
$ npm install --location=global verdaccio

# 全局安裝 npm 源管理工具(可以快速切换仓库源)

$ npm install --location=global nrm

# 添加一个私有 npm 源,'verdaccio' 为自定义的源地址名称 
$ nrm add verdaccio http://localhost:4873/

# 如果需要还原到默认设置,只需要删除当前用户下的 .nrmrc 文件即可

$ rm -rf .nrmrc

配置 verdaccio 从华为镜像服务器地址下载,默认配置服务器地址国内访问可能存在问题。另外注意禁用 npm-audit ,安全审计会非常非常慢,而且经常失败。

修改后的完整配置如下:

#
# This is the default configuration file. It allows all users to do anything,
# please read carefully the documentation and best practices to
# improve security.
#
# Look here for more config file examples:
# https://github.com/verdaccio/verdaccio/tree/5.x/conf
#
# Read about the best practices
# https://verdaccio.org/docs/best

# path to a directory with all packages
storage: /Users/xxxx/.local/share/verdaccio/storage
# path to a directory with plugins to include
plugins: ./plugins

# https://verdaccio.org/docs/webui
web:
  title: Verdaccio
  # comment out to disable gravatar support
  # gravatar: false
  # by default packages are ordercer ascendant (asc|desc)
  # sort_packages: asc
  # convert your UI to the dark side
  # darkMode: true
  # html_cache: true
  # by default all features are displayed
  # login: true
  # showInfo: true
  # showSettings: true
  # In combination with darkMode you can force specific theme
  # showThemeSwitch: true
  # showFooter: true
  # showSearch: true
  # showRaw: true
  # showDownloadTarball: true
  #  HTML tags injected after manifest <scripts/>
  # scriptsBodyAfter:
  #    - '<script type="text/javascript" src="https://my.company.com/customJS.min.js"></script>'
  #  HTML tags injected before ends </head>
  #  metaScripts:
  #    - '<script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>'
  #    - '<script type="text/javascript" src="https://browser.sentry-cdn.com/5.15.5/bundle.min.js"></script>'
  #    - '<meta name="robots" content="noindex" />'
  #  HTML tags injected first child at <body/>
  #  bodyBefore:
  #    - '<div id="myId">html before webpack scripts</div>'
  #  Public path for template manifest scripts (only manifest)
  #  publicPath: http://somedomain.org/

# https://verdaccio.org/docs/configuration#authentication
auth:
  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
    # Hash algorithm, possible options are: "bcrypt", "md5", "sha1", "crypt".
    # algorithm: bcrypt # by default is crypt, but is recommended use bcrypt for new installations
    # Rounds number for "bcrypt", will be ignored for other algorithms.
    # rounds: 10

# https://verdaccio.org/docs/configuration#uplinks
# a list of other known repositories we can talk to
uplinks:
  # npmjs:
  #   url: https://registry.npmjs.org/
  # 注意依赖顺序,一定要把 ohpm 放在最前面,先去 ohpm 更新,有些依赖,两个镜像上都存在,后面会出现冲突的情况
  ohpm:
    url: https://ohpm.openharmony.cn/ohpm/  
  ohpm2:
    url: https://repo.harmonyos.com/ohpm/
  oh_npm:
    url: https://repo.harmonyos.com/npm/
  npm_mirror:
    url: https://mirrors.huaweicloud.com/repository/npm/

# Learn how to protect your packages
# https://verdaccio.org/docs/protect-your-dependencies/
# https://verdaccio.org/docs/configuration#packages
packages:
  '@*/*':
    # scoped packages
    access: $all
    publish: $authenticated
    unpublish: $authenticated
    proxy: ohpm oh_npm npm_mirror

  '**':
    # 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/publish packages
    # (anyone can register by default, remember?)
    publish: $authenticated
    unpublish: $authenticated

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

# To improve your security configuration and  avoid dependency confusion
# consider removing the proxy property for private packages
# https://verdaccio.org/docs/best#remove-proxy-to-increase-security-at-private-packages

# https://verdaccio.org/docs/configuration#server
# You can specify HTTP/1.1 server keep alive timeout in seconds for incoming connections.
# A value of 0 makes the http server behave similarly to Node.js versions prior to 8.0.0, which did not have a keep-alive timeout.
# WORKAROUND: Through given configuration you can workaround following issue https://github.com/verdaccio/verdaccio/issues/301. Set to 0 in case 60 is not enough.
server:
  keepAliveTimeout: 60
  # Allow `req.ip` to resolve properly when Verdaccio is behind a proxy or load-balancer
  # See: https://expressjs.com/en/guide/behind-proxies.html
  # trustProxy: '127.0.0.1'

# https://verdaccio.org/docs/configuration#offline-publish
# publish:
#   allow_offline: false

# https://verdaccio.org/docs/configuration#url-prefix
# url_prefix: /verdaccio/
# VERDACCIO_PUBLIC_URL='https://somedomain.org';
# url_prefix: '/my_prefix'
# // url -> https://somedomain.org/my_prefix/
# VERDACCIO_PUBLIC_URL='https://somedomain.org';
# url_prefix: '/'
# // url -> https://somedomain.org/
# VERDACCIO_PUBLIC_URL='https://somedomain.org/first_prefix';
# url_prefix: '/second_prefix'
# // url -> https://somedomain.org/second_prefix/'

# https://verdaccio.org/docs/configuration#security
# security:
#   api:
#     legacy: true
#     jwt:
#       sign:
#         expiresIn: 29d
#       verify:
#         someProp: [value]
#    web:
#      sign:
#        expiresIn: 1h # 1 hour by default
#      verify:
#         someProp: [value]

# https://verdaccio.org/docs/configuration#user-rate-limit
# userRateLimit:
#   windowMs: 50000
#   max: 1000

# https://verdaccio.org/docs/configuration#max-body-size
# max_body_size: 10mb

# https://verdaccio.org/docs/configuration#listen-port
# listen:
# - localhost:4873            # default value
# - http://localhost:4873     # same thing
# - 0.0.0.0:4873              # listen on all addresses (INADDR_ANY)
# - https://example.org:4873  # if you want to use https
# - "[::1]:4873"                # ipv6
# - unix:/tmp/verdaccio.sock    # unix socket

# The HTTPS configuration is useful if you do not consider use a HTTP Proxy
# https://verdaccio.org/docs/configuration#https
# https:
#   key: ./path/verdaccio-key.pem
#   cert: ./path/verdaccio-cert.pem
#   ca: ./path/verdaccio-csr.pem

# https://verdaccio.org/docs/configuration#proxy
# http_proxy: http://something.local/
# https_proxy: https://something.local/

# https://verdaccio.org/docs/configuration#notifications
# notify:
#   method: POST
#   headers: [{ "Content-Type": "application/json" }]
#   endpoint: https://usagge.hipchat.com/v2/room/3729485/notification?auth_token=mySecretToken
#   content: '{"color":"green","message":"New package published: * {{ name }}*","notify":true,"message_format":"text"}'

middlewares:
  audit:
    enabled: false

# https://verdaccio.org/docs/logger
# log settings
log: { type: stdout, format: pretty, level: http }
#experiments:
#  # support for npm token command
#  token: false
#  # disable writing body size to logs, read more on ticket 1912
#  bytesin_off: false
#  # enable tarball URL redirect for hosting tarball with a different server, the tarball_url_redirect can be a template string
#  tarball_url_redirect: 'https://mycdn.com/verdaccio/${packageName}/${filename}'
#  # the tarball_url_redirect can be a function, takes packageName and filename and returns the url, when working with a js configuration file
#  tarball_url_redirect(packageName, filename) {
#    const signedUrl = // generate a signed url
#    return signedUrl;
#  }

# translate your registry, api i18n not available yet
# i18n:
# list of the available translations https://github.com/verdaccio/verdaccio/blob/master/packages/plugins/ui-theme/src/i18n/ABOUT_TRANSLATIONS.md
#   web: en-US

启动一个独立的Shell 运行 verdaccio 

$ nvm use 20.11.0

$ verdaccio                                
 info --- config file  - /Users/xxxx/.config/verdaccio/config.yaml
 info --- the "crypt" algorithm is deprecated consider switch to "bcrypt" in the configuration file. Read the documentation for additional details
 info --- using htpasswd file: /Users/xxxx/.config/verdaccio/htpasswd
 info --- plugin successfully loaded: verdaccio-htpasswd
 info --- plugin successfully loaded: verdaccio-audit
 warn --- http address - http://localhost:4873/ - verdaccio/5.29.0

清理缓存,并且要求通过 verdaccio 代理下载:

$ nvm use 20.11.0

# 'verdaccio' 为添加源时定义的源地址名称
$ nrm use verdaccio

# 列出缓存目录路径
$ npm config ls -l | grep cache

# 清理缓存
$ npm cache clean --force

当前(2024/02/01)申请并通过了华为开发计划的才可以下载到 HarmonyOS NEXT 开发需要的 HUAWEI DevEco Studio 4.x 版本(API 11)HUAWEI DevEco Studio 5.x (API 11、API 12) HarmonyOS Developer管理中心套件货架 目前只有这个版本的包含离线鸿蒙开发依赖 ohpm-repo,官方文档以及报错信息还是稀烂,基本找不到有用信息,需要自己研究。

注意: 目前测试发现 DevEco Studio 4.1.3.500 版本无法真机调试 C++ 代码。 DevEco Studio 4.1.3.501 版本可以正常调试。

截止 2024/04/01 最新Release版本是 DevEco Studio 4.1.3.700,最新测试版本 DevEco Studio 5.0.3.100(SP1),可惜编译不通过,报错如下:

ERR_PNPM_NO_MATCHING_VERSION  No matching version found for @ohos/hvigor-ohos-plugin@4.2.0

DevEco Studio 4.1.3.501 使用的官方SDK下载地址:

下载完成后,解压缩到 SDK 目录下的 HarmonyOS-NEXT-DP1 目录即可,如下图:

安装并缓存  ohpm-repo 依赖

$ unzip devecostudio-mac-arm-4.1.3.500.zip -d devecostudio-mac-arm-4.1.3.500

$ cd devecostudio-mac-arm-4.1.3.500

$ cd ohpm-repo

$ unzip ohpm-repo-2.1.0.0.zip -d ohpm-repo-2.1.0.0

$ cd ohpm-repo-2.1.0.0

# 当前华为的 HarmonyOS 要求 node 18 

# 先卸载已经安装过的 node 版本,保证最小安装集合,最大限度的进行缓存
# 之所以我们选择不同于HarmonyOS 需要的 node 版本来启动 verdaccio 进行缓存 
# 原因是我们在安装 verdaccio 的时候已经安装过很多缓存到全局安装目录了,这些缓存不会再次进行下载操作

# 当前正在使用的版本无法卸载,我们需要先切换到其他node版本下执行卸载命令
$ nvm use 20.11.0

$ nvm uninstall 18.19.0

$ nvm install 18.19.0

$ nvm use 18.19.0

# 删除可能已经下载过的模块

$ rm -rf node_modules

# 清理缓存
$ npm cache clean --force

$ npm install

# 额外手工下载几个会缺少的依赖
# ohos 依赖 参考下载链接样例 'https://repo.harmonyos.com/ohpm/@ohos/hypium/'

$ npm install @ohos/hamock@1.0.0-rc

# 下面的这些依赖需要使用 pnpm 安装
$ npm install -g pnpm

$ npm install -g pnpm@7.30.0

$ pnpm install @ohos/hvigor

# 20240206 :默认下载的是 4.0.4 版本
# 但是 DevEco Studio 4.1.3.500 新建工程还是使用的 4.0.2 版本。 
# 所以,要么下载低版本,要么在项目配置文件 "hvigor-config.json5" 
# 里面配置升级构建工具版本

# 20240319: DevEco Studio 4.1.3.501 已经要求必须使用 4.0.4 版本了

$ pnpm install @ohos/hvigor-ohos-plugin

# DevStudio 4.1.3.500
# https://repo.harmonyos.com/npm/@ohos/hvigor
# https://repo.harmonyos.com/npm/@ohos/hvigor/-/@ohos/hvigor-4.0.2.tgz

$ pnpm install @ohos/hvigor@4.0.2

$ pnpm install @ohos/hvigor-ohos-plugin@4.0.2

# DevStudio 4.1.3.501
# https://repo.harmonyos.com/npm/@ohos/hvigor
# https://repo.harmonyos.com/npm/@ohos/hvigor/-/@ohos/hvigor-4.0.4.tgz

$ pnpm install @ohos/hvigor@4.0.4 

$ pnpm install @ohos/hvigor-ohos-plugin@4.0.4

# DevStudio 4.1.3.700
# https://repo.harmonyos.com/npm/@ohos/hvigor
# https://repo.harmonyos.com/npm/@ohos/hvigor/-/@ohos/hvigor-4.1.2.tgz

$ pnpm install @ohos/hvigor@4.1.2 

$ pnpm install @ohos/hvigor-ohos-plugin@4.1.2 

# https://repo.harmonyos.com/npm/@ohos/hvigor-ohos-arkui-x-plugin
# arkui-x
$ pnpm install @ohos/hvigor-ohos-arkui-x-plugin

npm 安装包的缓存在 ~/.local/share/verdaccio/storage 目录下。

接下来需要逐个调整缓存包的 package.json 文件,这个文件里面其他包的依赖地址都被改成了 verdaccio/config.yaml 里配置的服务器地址,内网是访问不到这个地址的。

我们需要修改成我们内网服务器的地址(假定为: http://158.220.155.188/repository/npm )。

可以简单的执行如下命令即可:

$ cd ~/.local/share/verdaccio

$ cp -r storage storage.1

$ cd storage.1

$ find . -type f -name "package.json" | xargs sed -i '' 's/https:\/\/repo.harmonyos.com\/ohpm/http:\/\/158.220.155.188\/repository\/npm/g'

$ find . -type f -name "package.json" | xargs sed -i '' 's/https:\/\/repo.harmonyos.com\/npm/http:\/\/158.220.155.188\/repository\/npm/g'

$ find . -type f -name "package.json" | xargs sed -i '' 's/https:\/\/mirrors.huaweicloud.com\/repository\/npm/http:\/\/158.220.155.188\/repository\/npm/g'

# 注意,如果报错 sed: ./package.json: in-place editing only works for regular files 原因是在缓存目录下有个叫 package.json 的目录,比较奇葩,所以在搜索的时候需要指明查找文件,忽略文件夹 

# 默认情况下,ohos源的下载路径会存在一个 "/-/@ohos/" 的子路径,我们需要移除这个子路径
# 比如 "https://repo.harmonyos.com/npm/@ohos/hvigor/-/@ohos/hvigor-2.0.0.tgz"

$ find . -type f -name "package.json" | xargs sed -i '' 's/\/-\/@ohos\//\//g'

# 默认情况下,npm源的下载路径会存在一个 "/-/" 的子路径,我们需要移除这个子路径
# 比如 "https://mirrors.huaweicloud.com/repository/npm/accepts/-/accepts-1.0.0.tgz"

$ find . -type f -name "package.json" | xargs sed -i '' 's/\/-\//\//g'

针对 Apache(HTTPD) 2.4 服务器需要修改 httpd.conf 配置文件,增加对于 package.json 的默认访问。

# vim /usr/local/apache2/conf/httpd.conf

$ vim /etc/httpd/conf/httpd.conf

增加如下内容:

# 解决链接地址中存在 %2F 导致无法访问的问题
# 比如: http://158.220.155.188/repository/npm/@sqltools%2fformatter

AllowEncodedSlashes On

# 目录位置为服务器上的存储目录的实际位置
<Directory "/var/www/html/repository/npm">
    
    # 默认返回目录下的package.json文件
    DirectoryIndex package.json
    
    # 目前测试发现,扩展名为 .har 类型的文件,在CentOS上会被识别为 application/x-tar 
    # 导致下载过程中,Apache2 会去解压缩文件,然后执行网络传输,从而诱发文件校验失败
    # 我们需要配置一个独立的文件类型,取消 Apache2 的解压缩行为
    # 如果不进行如下配置,ohpm 会在下载完成之后出现校验错误
    AddType application/har .har

    
    # 目前鸿蒙系统的 ohpm 工具使用 node-fetch 进行网络下载的时候,配置的重定向参数为 “manual” 
    # /Applications/DevEco-Studio.app/Contents/tools/ohpm/lib/core/registry/registry.js 搜索 “withRedirect” 
    # 导致 node-fetch 无法正确处理 301 重定向,我们需要在 Httpd 2.4 上进行配置,当访问的URL是目录的时候不要直接 301 重定向
    # ohpm 访问的URL类似如下:
    # http://158.220.155.188/repository/npm/@ohos/hamock
    # 也就是在访问文件夹的时候,链接尾部没有以“/” 结尾
    # Httpd 遇到这这种链接默认会发一个 301 要求重定向到
    # “http://158.220.155.188/repository/npm/@ohos/hamock/” 
    # 但是 ohpm 处理不了这种情况。
    # 如果不进行如下配置,则会直接报错,无法下载成功

    # 在当前目录下,关闭 "目录不以 '/' 结尾,自动 301 重定向" 的默认逻辑
    DirectorySlash off
    
    # 启用URL重写引擎
    RewriteEngine on
    
    # 允许重写引擎处理不是以 '/' 结尾的请求,默认情况下,如果不以 '/' 结尾,可能会被重写引擎忽略
    RewriteOptions AllowNoSlash
    
    # 重写条件约束只适合于目录,不适用于普通文件
    RewriteCond %{REQUEST_FILENAME} -d

    # 对目录的访问一律重定向到当前目录下的 package.json 文件
    RewriteRule ^(.*[^/])$ $1/package.json [L]

    # ohpm 的特定行为,访问 
    # http://158.220.155.188/repository/npm/@ohos/hamock
    # 之后,会去
    # http://158.220.155.188/repository/npm/hamock
    # 执行下载,我们需要对于这种访问进行目录的二次重定向
    # 

    # 如果目录或文件不存在,并且路径中不包含 @ohos,则去 @ohos 目录下搜索子目录
    # 如果路径中 已经存在 @ohos 那么这个路径是我们自己添加进去的,但是依旧找不到文件,
    # 不要再次重定向了,应该返回 404 否则会引起循环重定向
    RewriteCond         %{REQUEST_FILENAME}  !-d
    RewriteCond         %{REQUEST_FILENAME}  !-f
    RewriteCond         %{REQUEST_FILENAME} !@ohos

    RewriteRule ^(.*[^/])$ @ohos/$1 [L]

    # hvigor 始终会在路径中拼接 "/@ohos/" 的子路径,我们需要移除这个子路径 
    # 比如 "https://repo.harmonyos.com/npm/@ohos/hvigor/@ohos/hvigor-2.0.0.tgz"

    RewriteCond         %{REQUEST_FILENAME}  !-d
    RewriteCond         %{REQUEST_FILENAME}  !-f
    RewriteCond         "%{REQUEST_URI}" "(.*)@ohos/(.*)/@ohos(.*)"

    RewriteRule ^(.*)$ "%1@ohos/%2%3" [L]

</Directory>

重启服务:

$ sudo service httpd restart

之后,整个 ~/.local/share/verdaccio/storage 目录可以拷贝到 Apache(HTTPD) 2.4 的对应文件下载目录下即可。

Figure 1: The back-reference flow through a rule.
In this example, a request for /test/1234 would be transformed into /admin.foo?page=test&id=1234&host=admin.example.com.
Figure 1: The back-reference flow through a rule. In this example, a request for /test/1234 would be transformed into /admin.foo?page=test&id=1234&host=admin.example.com.

四、使用配置

上面介绍的是如何通过缓存建立内网的 NPM 代理服务器,下面我们讲一下如何使用我们刚刚配置好的缓存服务器。

编辑当前用户目录下的 .npmrc 文件(不存在则手工创建),增加如下内容:

# 镜像服务器地址,此地址被 npm install 命令使用
registry=http://158.220.155.188/repository/npm/

# ohos 鸿蒙系统npm安装包镜像地址,此参数给 hvigor 使用的 pnpm 使用
# 如果不配置 pnpm install 会去默认的官方地址下载
@ohos:registry=http://158.220.155.188/repository/npm/

# 禁用 npm-audit 安全审计,内网连接不上外网审计服务器
audit=false

1. 在内网下载从 HarmonyOS NEXT 开发需要的 HUAWEI DevEco Studio 4.x 版本(API 11) 下载到内网服务器上的HUAWEI DevEco Studio 4.x 安装包,并且解压缩。

2. 找到安装包目录下的 sdk 并解压缩到任意目录。

3. 安装 Node 18.19.0到内网开发机。

4. 编辑当前用户目录下的 .ohpm/.ohpmrc 文件(不存在则手工创建),增加如下内容:

### 仓库配置,支持配置多个地址,使用英文逗号分割
# registry=https://repo.harmonyos.com/ohpm/

# 镜像服务器地址,此地址被 ohpm install 命令使用
registry=http://158.220.155.188/repository/npm/

# 禁用 npm-audit 安全审计,内网连接不上外网审计服务器 
audit=false

## 支持配置含有group的仓库,优先匹配
#@group1:registry=
#@group2:registry=

### 代理配置
#no_proxy=
#http_proxy=
#https_proxy=

### 证书校验配置
## 是否校验https仓库的证书,取值:true|false, default: true, 为true时需要配置ca_files证书路径
strict_ssl=true
#ca_files=

### 发布配置
## 因为registry支持配置多仓,所以此处需明确指定发布仓
publish_registry=https://repo.harmonyos.com/ohpm/
## 用户发布三方库的发布id,从云端个人中心获取
#publish_id=
## 用户私钥文件路径,公钥需在云端进行配置
#key_path=

### 缓存路径, 默认在用户目录下的.ohpm/cache
#cache=

### 日志级别, 取值:debug|info|warn|error, default: info
log_level=info

### 网络请求超时时间, 单位ms, 默认为60s
fetch_timeout=60000

5. 新建项目,编译调试。

五、可能遇到的问题

1. 现实使用中,我们部署上去的文件存在问题,然后替换成正确的版本,但是中间有些用户已经更新到有问题的版本了,导致编译报错。可能会遇到不管用户如何清理缓存,删除当前用户下的 .hvigor 目录,甚至重装编译器,都不能解决报错。 这时候,Windows 用户需要注意, 在 C:\Users\XXXX\AppData\Local 目录下(XXXX为当前用户名)存在一个名为 pnpm-cache 的目录,这个目录下存在一系列的缓存文件,尝试删除一下这个目录试试。 

2. 部署下载完成后的软件包之后,如果在执行 ohpm update 的时候报错:

ohpm ERROR: ohpm tool does not support this registry http://xxx.xxx.xxx.xxx/
ohpm ERROR: missing: xxxx required by xxxx
ohpm ERROR: ERUNNING execute tasks failed, Error:Dependency node build failed

检查之后,会发现各个软件包都是正常的,但是就是不断报错。

这个问题产生的原因是部分被 Verdaccio 处理之后的 package.json ,无法被 ohpm 正常解析。

解决方法是手动去 ohpm 网站下载,类似如下:

# 以 reflect-metadata@^0.1.13为例

$ wget -O package.json https://ohpm.openharmony.cn/ohpm/reflect-metadata

$ wget -O reflect-metadata-0.1.13.tgz https://repo.harmonyos.com/ohpm/reflect-metadata/-/reflect-metadata-0.1.13.tgz

$ sed -i '' 's/https:\/\/repo.harmonyos.com\/ohpm/http:\/\/158.220.155.188\/repository\/npm/g' package.json

$ sed -i '' 's/https:\/\/repo.harmonyos.com\/npm/http:\/\/158.220.155.188\/repository\/npm/g' package.json

$ sed -i '' 's/https:\/\/mirrors.huaweicloud.com\/repository\/npm/http:\/\/158.220.155.188\/repository\/npm/g' package.json

# 注意,如果报错 sed: ./package.json: in-place editing only works for regular files 原因是在缓存目录下有个叫 package.json 的目录,比较奇葩,所以在搜索的时候需要指明查找文件,忽略文件夹 

# 默认情况下,ohos源的下载路径会存在一个 "/-/@ohos/" 的子路径,我们需要移除这个子路径
# 比如 "https://repo.harmonyos.com/npm/@ohos/hvigor/-/@ohos/hvigor-2.0.0.tgz"

$ sed -i '' 's/\/-\/@ohos\//\//g' package.json

# 默认情况下,npm源的下载路径会存在一个 "/-/" 的子路径,我们需要移除这个子路径
# 比如 "https://mirrors.huaweicloud.com/repository/npm/accepts/-/accepts-1.0.0.tgz"

$ sed -i '' 's/\/-\//\//g' package.json

3. 针对 crypto-js 这个包,我们会发现 

返回的数据是完全不同的,但是自动依赖解析,尤其是多个 NPM 源的情况下,可能会出现依赖错误,导致各种报错。我们需要手工分析真实依赖,再完成导入。

参考链接


发布者

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注