Last active
March 14, 2024 22:28
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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`) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Note: It's not available in blank tab