From 9ae6e7c89dcc01327e4786ff747e896ffa6ecafb Mon Sep 17 00:00:00 2001 From: xzeldon Date: Tue, 11 Jan 2022 02:24:39 +0300 Subject: [PATCH] docs: added clarification regarding CommonJS --- README.md | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e7d7039..b5c95af 100644 --- a/README.md +++ b/README.md @@ -14,12 +14,29 @@ Collect information about Minecraft (both Java and Bedrock) using **[Node.js](ht npm i @minescope/mineping ``` -## Example +## Loading and configuration the module + +### ES Modules (ESM) + +```js +import { pingJava, pingBedrock } from 'mineping'; +``` + +### CommonJS +`mineping` is an ESM-only module - you are not able to import it with `require()`. +If you cannot switch to ESM, you can use the async `import()` function from CommonJS to load `mineping` asynchronously: + +```js +const pingJava = (...args) => import('mineping').then(module => module.pingJava(...args)); +const pingBedrock = (...args) => import('mineping').then(module => module.pingBedrock(...args)); +``` + +## Usage Ping a Java server with default options: ```js -import { pingJava } from 'mineping' +import { pingJava } from 'mineping'; const data = await pingJava('mc.hypixel.net'); console.log(data); @@ -28,7 +45,7 @@ console.log(data); Ping a Bedrock server with custom options: ```js -import { pingBedrock } from 'mineping' +import { pingBedrock } from 'mineping'; const data = await pingBedrock('mco.mineplex.com', { port: 19132,