Skip to content

Instantly share code, notes, and snippets.

@josdejong
Last active March 14, 2024 22:28
Show Gist options
  • Save josdejong/3d176fa90efa36ed51ab6a4e8810074e to your computer and use it in GitHub Desktop.
Save josdejong/3d176fa90efa36ed51ab6a4e8810074e to your computer and use it in GitHub Desktop.
Helper function to load ESM packages from NPM in your browser dev tools console
/**
* Load an ESM package from npm in your JavaScript environment.
*
* Loads packages from https://www.npmjs.com/ via the CDN https://www.jsdelivr.com/
*
* Usage:
*
* const lodash = await npm('lodash-es')
* const lodash = await npm('lodash-es@4')
* const lodash = await npm('lodash-es@4.17.21')
*
* lodash.uniq([1, 3, 2, 1, 2, 2]) // [1, 3, 2]
*/
async function npm(name) {
return await import(`https://cdn.jsdelivr.net/npm/${name}/+esm`)
}
const lodash = await npm('lodash-es')
lodash.uniq([1, 3, 2, 1, 2, 2])
// [1, 3, 2]
@Jancat
Copy link

Jancat commented Mar 13, 2024

Note: It's not available in blank tab

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment