| name | livecodes/module-resolution |
| description | Import npm, deno.land/x, jsr, and GitHub modules without build steps using automatic CDN resolution, custom import maps, and CDN provider prefixes. Load this skill when using bare module imports, configuring CDN providers, or resolving import conflicts.
|
| type | core |
| library | livecodes |
| library_version | 0.13.0 |
| sources | ["live-codes/livecodes:docs/docs/features/module-resolution.mdx","live-codes/livecodes:src/livecodes/compiler/import-map.ts"] |
LiveCodes — Resolve Modules
LiveCodes automatically resolves bare module imports to CDN URLs using import maps. Use npm packages and other modules without npm install or build steps.
Setup
import { createPlayground } from 'livecodes';
createPlayground('#container', {
config: {
script: {
language: 'javascript',
content: `
import { v4 } from 'uuid'; // npm module
import React from 'react'; // npm module
import { useState } from 'react'; // Named import
console.log(v4());
`,
},
},
});
Core Patterns
Import npm packages
import { v4 } from 'uuid';
import React from 'react';
import { useState, useEffect } from 'react';
import { format } from 'date-fns';
import React from 'react@18';
import React from 'react@18.2.0';
import _ from 'lodash@4.17.21';
Use different CDN providers
import React from 'react';
import React from 'esm.sh:react';
import React from 'skypack:react';
import React from 'jsdelivr:react';
import React from 'unpkg:react';
import { uuid } from 'deno:uuid';
import React from 'https://esm.sh/react@18';
Import from Deno, GitHub, JSR
import { uuid } from 'https://deno.land/x/uuid/mod.ts';
import { yassify } from 'jsr:@kwhinnery/yassify';
import { flatten } from 'https://github.com/remeda/remeda/blob/master/src/flatten.ts';
import { flatten } from 'https://github.com/remeda/remeda/blob/master/src/flatten.ts#nobundle';
import { Bench } from 'pr:tinybench@a832a55';
Custom import maps
createPlayground('#container', {
config: {
imports: {
'my-lib': 'https://my-cdn.com/lib.js',
'my-lib/utils': 'https://my-cdn.com/utils.js',
'my-private-package': 'https://my-server.com/package/index.js',
},
script: {
language: 'javascript',
content: `
import { something } from 'my-lib';
import { helper } from 'my-lib/utils';
`,
},
},
});
{
"imports": {
"my-lib": "https://my-cdn.com/lib.js"
}
}
Change default CDN
const config = {
customSettings: {
defaultCDN: 'skypack',
},
script: {
language: 'javascript',
content: `import React from 'react'; // Now uses skypack`,
},
};
Import CSS from script
import 'https://unpkg.com/github-markdown-css/github-markdown.css';
import 'github-markdown-css/github-markdown.css';
import styles from './style.css';
console.log(styles);
Common Mistakes
HIGH Same module from different CDNs conflicts
Wrong:
import React from 'esm.sh:react';
import { createRoot } from 'skypack:react-dom/client';
Correct:
import React from 'react';
import { createRoot } from 'react-dom/client';
import React from 'esm.sh:react';
import { createRoot } from 'esm.sh:react-dom/client';
Module instances from different CDNs are separate. React context, hooks, and component state require the same instance.
Source: docs/docs/features/module-resolution.mdx — CDN Providers caution
MEDIUM CommonJS require works but ESM preferred
Works but not recommended:
const { v4 } = require('uuid');
const React = require('react');
Preferred:
import { v4 } from 'uuid';
import React from 'react';
LiveCodes converts require to ESM imports, but explicit ESM is clearer and matches local development.
Source: docs/docs/features/module-resolution.mdx — CommonJS Modules section
LOW Not using #nobundle for pre-bundled URLs
When you want raw module URLs instead of bundling:
import { flatten } from 'https://github.com/remeda/remeda/blob/master/src/flatten.ts';
import { flatten } from 'https://github.com/remeda/remeda/blob/master/src/flatten.ts#nobundle';
Use #nobundle when the URL already serves bundled code or when you want to avoid bundling overhead.
Source: docs/docs/features/module-resolution.mdx — GitHub/GitLab/Bitbucket section
CDN Provider Reference
| Prefix | CDN | URL Format |
|---|
| (none) | esm.sh | https://esm.sh/{package} |
esm.sh: | esm.sh | https://esm.sh/{package} |
skypack: | Skypack | https://cdn.skypack.dev/{package} |
jsdelivr: | jsDelivr | https://cdn.jsdelivr.net/npm/{package} |
unpkg: | unpkg | https://unpkg.com/{package}?module |
bundle: | bundlejs | https://deno.bundlejs.com/?file&q={package} |
deno: | bundlejs | https://deno.bundlejs.com/?file&q={url} |
jsr: | esm.sh (JSR) | https://esm.sh/jsr/{package} |
pr: | esm.sh (pkg.pr.new) | https://esm.sh/pr/{package} |
How It Works
- Bare imports (
import x from 'package') are resolved via import maps
- Import map maps bare specifiers to full CDN URLs
- CDN (default: esm.sh) serves ESM versions of npm packages
- Deno/GitHub URLs ending in
.ts/.tsx/.jsx are bundled via bundlejs.com
- CommonJS
require() calls are transpiled to ESM imports