You need to remove space before EOF in closing:
#!/bin/sh
cd $HOME/filedirectory
for k in *; do
cat > $HOME/mongodb/bin/script1.js << EOF
##commented out stuff
EOF
done
As per man bash
:
If the redirection operator is <<-, then all leading tab characters are stripped from input lines and the line containing delimiter. This allows here-docu- ments within shell scripts to be indented in a natural fashion.
So you can change the heredoc operator to <<-
. then you can indent both the heredoc and the delimiter with tabs. (no spaces)