Skip to content

Instantly share code, notes, and snippets.

@triptych
Forked from josdejong/npm.js
Created March 14, 2024 22:28
Show Gist options
  • Save triptych/4df86b2a36edb1d3c374423aec7e9ad8 to your computer and use it in GitHub Desktop.
Save triptych/4df86b2a36edb1d3c374423aec7e9ad8 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('[email protected]')
*
* 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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment