awslabs / llrt

LLRT (Low Latency Runtime) is an experimental, lightweight JavaScript runtime designed to address the growing demand for fast and efficient Serverless applications.
Apache License 2.0
8.02k stars 354 forks source link

Loading node_modules with import/require #589

Open nabetti1720 opened 4 days ago

nabetti1720 commented 4 days ago

We know that bundling is better for performance, but for experimental or less performance-intensive use cases, it would be great if node_modules loading could be supported.

// index4.js
import { Event } from "event-target-shim";

globalThis.Event = Event

import { Bench } from 'tinybench';

const bench = new Bench({ time: 100 });

import mod from '../package.json';

bench
  .add('json', async () => {
    JSON.stringify(mod)
  })
await bench.warmup();
await bench.run();
console.log(bench.table());
% llrt ./src/index4.js
ReferenceError: Error resolving module 'event-target-shim' from './src/index4.js'
ahaoboy commented 3 days ago

Resolve is very complex, for reference:

https://crates.io/crates/oxc_resolver

https://www.npmjs.com/search?q=enhanced-resolve

richarddavison commented 2 days ago

Yes, agree that this is super useful but also very complex: ESM: https://nodejs.org/api/esm.html#resolution-and-loading-algorithm CJS: https://nodejs.org/api/modules.html#all-together

Maybe we should do a basic support that doesn't handle all the edge cases?