diff --git a/bin/sbang b/bin/sbang
index 0b883a5feb06bbe4348cbfd73d36ca3e4821db34..ce4df4327cc0718b71deb6f1edf0926dd1e7f0a3 100755
--- a/bin/sbang
+++ b/bin/sbang
@@ -104,6 +104,8 @@ lines=0
 while read line && ((lines < 2)) ; do
     if [[ "$line" = '#!'* ]]; then
         interpreter="${line#\#!}"
+    elif [[ "$line" = '//!'*node* ]]; then
+        interpreter="${line#//!}"
     elif [[ "$line" = '--!'*lua* ]]; then
         interpreter="${line#--!}"
     fi
diff --git a/lib/spack/spack/hooks/sbang.py b/lib/spack/spack/hooks/sbang.py
index e86f8260b8ba85057e203f9478bb924bba7b8416..cd3529dbb8183299ccba40eacc044bed4dcd3f9c 100644
--- a/lib/spack/spack/hooks/sbang.py
+++ b/lib/spack/spack/hooks/sbang.py
@@ -62,10 +62,17 @@ def filter_shebang(path):
     if original.startswith(new_sbang_line):
         return
 
+    # In the following, newlines have to be excluded in the regular expression
+    # else any mention of "lua" in the document will lead to spurious matches.
+
     # Use --! instead of #! on second line for lua.
-    if re.search(r'^#!(/[^/]*)*lua\b', original):
+    if re.search(r'^#!(/[^/\n]*)*lua\b', original):
         original = re.sub(r'^#', '--', original)
 
+    # Use //! instead of #! on second line for node.js.
+    if re.search(r'^#!(/[^/\n]*)*node\b', original):
+        original = re.sub(r'^#', '//', original)
+
     # Change non-writable files to be writable if needed.
     saved_mode = None
     if not os.access(path, os.W_OK):