OneDrive doesn’t allow to exclude directories by name, and that’s something that won’t change soon:
[OneDrive’s representative response on a UserVoice request:] Not right now
Some people recommend to uncheck directories you don’t want to sync in OneDrive settings, but that became impossible with OneDrive Files on-demand.
However, you can solve this by tweaking things on the Node.js’ side. Make the
node_modules directory a file symlink to a different place:
# Open any place outside OneDrive
# Make a symlink target. After linking, node_modules for your project
# will be installed here
# Open the project directory
cd <the project directory>
# Make a *file* link from node_modules to the newly created directory.
# You will need to delete existing node_modules directory if it exists
cmd /C "mklink node_modules D:\node_dependencies\node_modules_for_my_project"
The important piece here is that you create a file symlink, not directory one. OneDrive won’t recognize and sync this symlink, whereas Node.js will work with it as intended:
Tested with OneDrive v17.3.7101.1018 and OneDrive Files on-demand enabled.
This is not a universal solution. A drawback of this is that Explorer, Powershell and other tools won’t recognize
node_modules as a directory:
However, Node.js-based code editors will read it just fine: