nodejs / import-in-the-middle

Like `require-in-the-middle`, but for ESM import
https://www.npmjs.com/package/import-in-the-middle
Apache License 2.0
60 stars 22 forks source link

usage of `import` causes odd side effects/timing #8

Open bmeck opened 2 years ago

bmeck commented 2 years ago

Due to import() causing evaluation getting the namespaces of modules can cause double init since the loader module cache != the app module cache and can cause out of order evaluation.

A nasty intentional runtime error with a nested cycle could let you get a hold of a module namespace without executing it potentially / sadly.

// inspector
import 'will-explode';
import * as ns from 'to-inspect';
export function keys() {
  return Object.keys(ns);
}
// will-explode
import {keys} from 'inspector';
throw keys();
bengl commented 1 year ago

This is fixed on Node.js 20, where a parser is used instead to get exports.

That said, I'll leave this open, since we're not yet testing for it, and it will still explode on earlier versions of Node.js.