Skip to content
Myr

Add instructions for dynamic import in README.md (#516)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Add instructions for dynamic import in README.md * Split into ul and add example * Make example code block more clear * Cache dynamic import in example

authored by YitzchakMeltz · committed by GitHub · parent 078b605 · Browse files · 1 file+20 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+20 added−8 removed

  • other:+20 added−8 removed

1 file+20 added−8 removed

modified:README.md+20 added-8 removed
130 unmodified lines
131```bash132npm install nanoid133```134135### ESM136Nano ID 5 works with ESM projects (with `import`) in tests or Node.js scripts.136For CommonJS `require()` you need to use latest Node.js 22.12137(works out-of-the-box) or Node.js 20 (with `--experimental-require-module`):137139For Node.js 18 you can use Nano ID 3.x (we still support it):140141```bash142npm install nanoid@3143```144138For quick hacks, you can load Nano ID from CDN. Though, it is not recommended139to be used in production because of the lower loading performance.140141```js142import { nanoid } from 'https://cdn.jsdelivr.net/npm/nanoid/nanoid.js'143```144145### CommonJS146Nano ID 5 can be used with CommonJS in one of the following ways:147148- You can use `require()` to import Nano ID. You need to use latest Node.js 22.12 (works out-of-the-box) or Node.js 20 (with `--experimental-require-module`)149150- For Node.js 16 or newer you can dynamically import Nano ID as follows:151 ```js152 let nanoid;153 154 module.exports.createID = async () => {155 if (!nanoid) ({ nanoid } = await import('nanoid'));156 const id = nanoid() // => "V1StGXR8_Z5jdHi6B-myT"157 }158 ```159160- You can use Nano ID 3.x (we still support it):161 ```bash162 npm install nanoid@3163 ```164165## API166167Nano ID has 2 APIs: normal and non-secure.168
295 unmodified lines