Skip to content

Instantly share code, notes, and snippets.

@BSFishy
Created November 7, 2023 18:57
Show Gist options
  • Save BSFishy/f917770acac228565e98d9d819d582e5 to your computer and use it in GitHub Desktop.
Save BSFishy/f917770acac228565e98d9d819d582e5 to your computer and use it in GitHub Desktop.
Get a list of dependencies required at runtime
import { nodeFileTrace } from '@vercel/nft';
import path from 'node:path';
import { lstat } from 'node:fs/promises';
async function run() {
const cwd = process.cwd();
const dir = process.argv[2];
if (!dir) {
throw 'You must specify the directory';
}
const stat = await lstat(path.resolve(cwd, dir));
if (!stat.isDirectory()) {
throw 'Not a directory';
}
const { fileList } = await nodeFileTrace([path.join(dir, 'src', 'index.js')], {
base: dir,
ts: true,
});
const list = [...fileList]
.filter(file => file.startsWith('node_modules'))
.map(file => file.split('/')[1]);
console.log(new Set(list));
}
run().catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment