From 90ee0ce9980df28e2de28e93d646f31f101a83fc Mon Sep 17 00:00:00 2001 From: Tuyen Nguyen Date: Fri, 11 Nov 2022 16:24:24 +0700 Subject: [PATCH] feat: migrate to esm --- README.md | 2 +- index.js | 3 ++- package.json | 6 +++++- src/index.ts | 3 ++- test.js | 4 ++-- tsconfig.json | 5 +++-- 6 files changed, 15 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index e1c7872..620bdbc 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ or ### Usage ```js -const is_ip_private = require('private-ip') +import is_ip_private from 'private-ip' is_ip_private('10.0.0.0') // => true diff --git a/index.js b/index.js index 9f6069c..8b584af 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ 'use strict' -module.exports = require('./lib').default +import is_ip_private from './lib/index.js' +export default is_ip_private \ No newline at end of file diff --git a/package.json b/package.json index 0291c7b..b701858 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,8 @@ "name": "private-ip", "version": "2.3.4", "description": "Check if IP address is private.", - "main": "index.js", + "exports": "./index.js", + "type": "module", "types": "lib/index.d.ts", "repository": { "type": "git", @@ -54,5 +55,8 @@ "ipaddr.js": "^2.0.1", "@chainsafe/is-ip": "^2.0.1", "netmask": "^2.0.2" + }, + "engines": { + "node": ">=14.16" } } diff --git a/src/index.ts b/src/index.ts index 3354a3b..64547fe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,8 @@ import { Netmask } from 'netmask' import ip_regex from 'ip-regex' import { isIP } from '@chainsafe/is-ip' -import { isValid as is_valid, parse, IPv4 } from 'ipaddr.js' +import ipaddr, { IPv4 } from 'ipaddr.js' +const { isValid: is_valid, parse } = ipaddr const PRIVATE_IP_RANGES = [ '0.0.0.0/8', diff --git a/test.js b/test.js index 27fb08a..9a12a92 100644 --- a/test.js +++ b/test.js @@ -1,5 +1,5 @@ -const test = require('ava') -const is_ip_private = require('./') +import test from 'ava' +import is_ip_private from './index.js' const pub_ips = [ '44.37.112.180', diff --git a/tsconfig.json b/tsconfig.json index 56291b4..0d75b0f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,8 @@ { "compilerOptions": { - "target": "es6", - "module": "commonjs", + "target": "ES2020", + "module": "ES2020", + "moduleResolution": "node", "esModuleInterop": true, "declaration": true }