# npm

Node Package Manager

南大源采用了腾讯云和官方源的混合代理形式。

## npm 配置指南

### 临时使用

```shell
npm install [packageName] --registry=https://repo.nju.edu.cn/repository/npm/ 
```

使用此命令可以临时使用南大源下载指定包。

### 设置为默认源

使用一下命令修改默认源：

```shell
npm config set registry https://repo.nju.edu.cn/repository/npm/ # 修改源
```

## npm 私服

南京大学 npm 仓库同时提供 **npm 包托管服务**，本校师生可以将自己编写的 js 脚本打包后发布到仓库中让其他人使用。此仓库文件**不会同步到其他镜像源**。

### 从仓库获取文件

将上面提到的仓库地址改为 `https://repo.nju.edu.cn/repository/npm-nju/` 即可。

使用此仓库请注意:

- 此仓库会优先从官方库和其他源中获取包，如果你发布了同名包，会被其他来源的同名包覆盖，**因此请不要也禁止发布同名包或者名称相近包**；
- 当仓库网络出现问题或者上游源出现问题时，**可能会导致你下载到仓库其他人发布的同名包**，这可能不是你想要的，甚至有风险（pypi 仓库则没有此问题）；
- 如果发现和上游仓库**同名包**，请[填写统计表](https://table.nju.edu.cn/dtable/forms/818ab7f9-6525-47d5-be85-2f527e7ae8d3/)，管理员将会定期删除；
- 如果发现**恶意同名包或者类似包**，请[填写统计表](https://table.nju.edu.cn/dtable/forms/818ab7f9-6525-47d5-be85-2f527e7ae8d3/)，管理员将核实后采取措施。


### 向仓库提交文件

1. 使用 npm 登陆：
    ```shell
    npm adduser --registry https://repo.nju.edu.cn/repository/npm-releases/
    ```
    其中，邮箱推荐使用南大邮箱，账号密码为统一身份认证账号密码。

2. 在项目的 `package.json` 中加入以下内容：

    ```
      "publishConfig": {
        "registry" : "https://repo.nju.edu.cn/repository/npm-releases/"
      },
    ```

3. 配置 `package.json` 中的其他配置项。
4. 配置 `.npmignore` 指定你不想发布的文件（夹）。
5. 构建并推送：

    ```bash
    npm run build
    npm publish
    ```

## yarn 配置指南

```shell
yarn config set registry https://repo.nju.edu.cn/repository/npm/ # 修改源
```

yarn 发布的过程和 npm 类似，不过 `yarn login` 的时候不需要输入密码，在每次发布的时候输入，具体内容请查看 yarn publish 的文档。