# 使用第三方模快

## 使用第三方模組 is.js

使用第三方模組的方式與使用原生模組類似，只需要透過 `require()` 函式引入模組即可。

以 `is.js` 為例，`is.js` 是一個用於 JavaScript 的資料型別判斷庫。它提供了一系列方法，用於判斷一個變數的資料型別是否符合預期。

首先，你需要在 Node.js 中安裝 `is.js` 模組，可以透過以下命令來安裝：

```bash
npm install is_js --save
```

{% hint style="warning" %}
注意：is.js 的 npm 包名是 is\_js，名字中有底線
{% endhint %}

安裝完成後，你就可以在 Node.js 應用程式中使用 `is.js` 模組。例如，你可以透過以下方式來引入 `is.js` 模組並使用它的函式：

```javascript
const is = require('is_js');

if (is.number(x)) {
  console.log('x 是一個數字');
}
else {
  console.log('x 不是一個數字');
}
```

透過 `is.js` 庫，您可以輕鬆地對 JavaScript 中的變數進行型別判斷，從而避免型別不匹配導致的錯誤。

如果您想要集成到 Eagle 插件中，以下是範例程式碼與執行結果：

```javascript
const is = require('is_js');

eagle.onPluginCreate(() => {
    var x = 1;

    if (is.number(x)) {
        document.write('x 是一個數字');
    } else {
        document.write('x 不是一個數字');
    }
});
```

<figure><img src="https://3610077812-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fs4bjQFr3EFnc58JiXhsg%2Fuploads%2FbNbKIJxu4txo4oaY8464%2Fimage.png?alt=media&#x26;token=3fa12f4b-61f9-4b1f-bcf4-0567582cb4d9" alt=""><figcaption><p>執行結果</p></figcaption></figure>

{% hint style="info" %}
上述範例專案可以在 [這裡](https://github.com/eagle-app/eagle-plugin-examples/tree/main/3rd-party) 取得
{% endhint %}

***

## 第三方包管理工具：NPM <a href="#zql65" id="zql65"></a>

npm 是 Node.js 的官方包管理工具，提供了一種方便的方式來管理第三方模組和發布您自己的模組。透過 npm，您可以使用 `npm install` 命令快速安裝模組。npm 提供了強大的模組管理功能，可以幫助您更好地管理專案依賴和模組版本，提高專案的開發效率。

此外，npm 還提供了一個線上模組倉庫，您可以在這裡搜尋和下載第三方模組。總的來說，npm 是 Node.js 開發者不可或缺的工具，提供了一系列實用的功能，幫助您更好地開發和管理您的專案。

{% hint style="info" %}
**npm 官網 -** <https://www.npmjs.com/>
{% endhint %}
