Port Node.js loader plugin fixes from d2c

Fixes #18235, #18236.

(cherry picked from commit c58335c35155178b316953ec07d06d1fb8b16e54)