I build source on localhost. I push to testing branch on Github. I pull on testing server and run my build script where Hugo builds the final source into the virtual hosts’ document root. This way, I can control the building, etc.
I would just script something in bash to remove the node_modules directory after the site has been built so that it remains in your souce but when built, does not show up in static (as you’ll be deleting it)
I’m not entirely sure I understand the issue, perhaps there could be a more logical or different/better approach.
You could try something like this at https://regex101.com

You can learn regex here and also how to match directory and file patterns.
ignoreFiles := cast.ToStringSlice(s.Cfg.Get("ignoreFiles"))
if len(ignoreFiles) > 0 {
for _, ignorePattern := range ignoreFiles {
match, err := regexp.MatchString(ignorePattern, filePath)
if err != nil {
helpers.DistinctErrorLog.Printf("Invalid regexp '%s' in ignoreFiles: %s", ignorePattern, err)
return false
} else if match {
return true
}
}
}
The above is the hugo code. It’s basically processing whate regex list you give it, and you can probably give it a regex which matches a directory path instead of files.
In the documentation examples, they are ignoring files ending with .foo and .bar, using regular expression.
^ == start of line
. == match anything
$ end of line
I won’t do into the details of regex here, but you can even specify multiple directories:

So possibly try something like
ignoreFiles = [ "(.*/home/user/dev/node_modules/|/usr/share/node_modules)"]
or
ignoreFiles = [ "^(.*/home/user/dev/node_modules/)$", "^(.*/directory_2/)$"]
etc.
I’m unsure which, I’ve never used this directive, and I haven’t the means to test it out locally right now.