Use js to download and unzip the npm package. only supports use in the browser
Use js to download and unzip the npm package. only supports use in the browser.
Usage
Options are optional.
import { fetchFiles, fetchPackage, findTarget } from 'fetch-npm-package';
// All file are flattened
const files = await fetchFiles('react', {
version: 'latest', // default is `latest`
registry: 'https://registry.npmjs.org' // default is `https://registry.npmjs.org`
});
console.log(files);
// Aggregate as a folder
const folder = await fetchPackage('react');
console.log(folder);
By tarball request.
const files = await fetchFiles.tarball('https://registry.npmjs.org/react/-/react-18.1.0.tgz');
console.log(files);
const folder = await fetchPackage.tarball('https://registry.npmjs.org/react/-/react-18.1.0.tgz');
console.log(folder);
Find package file.
const folder = await fetchPackage('react');
console.log(folder);
const target = findTargetFile(folder, 'cjs/react-jsx-dev-runtime.development.js');
console.log(target.code);
CDN
<!DOCTYPE html>
<html lang='en'>
<body>
<script src='https://unpkg.com/fetch-npm-package/dist/fetch.umd.js'></script>
<script>
const { fetchFiles, fetchPackage, findTarget } = FetchNpmPackage;
// ...
</script>
</body>
</html>
Extended use
If you want to find a file more precisely based on package.json, you can use node-package-exports.