Port Node.js loader plugin fixes from d2c

Fixes #18235, #18236.

(cherry picked from commit c58335c35155178b316953ec07d06d1fb8b16e54)
(cherry picked from commit af45feb8fdb71e014256ad4191d4ea49357c2a5b)