How to exclude node_modules directory from OneDrive synchronization?

I know this is an older question, but I ran into the same issue. The solution I came across was to first create an empty node_modules folder and then sync that with the cloud.

Once the empty folder was synced, I then went into

OneDrive settings → Choose folders

and deselected the node_modules folder.

Then when you run npm install in the project root folder, OneDrive will detect a conflict with the contents of the folder in the cloud. It will not sync.

Image of Sync Error

The downside of this approach is that OneDrive produces sync errors, but it seemed to work well for my needs.


Background

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.

Solution

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
cd D:\node_dependencies
# Make a symlink target. After linking, node_modules for your project 
# will be installed here
mkdir node_modules_for_my_project

# 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:

A screenshot showing that npm install and Node.js’s require work as intended

Tested with OneDrive v17.3.7101.1018 and OneDrive Files on-demand enabled.

Drawbacks

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:

A screenshot showing that Explorer shows node_modules as an unrecognized file with a shortcut icon

However, Node.js-based code editors will read it just fine:

Screenshot of Visual Studio code with node_modules directory expanded


I've found that the best solution is creating a 'local' folder outside the OneDrive folder where the project will be saved and any modules will be installed. This is the actual project.

To sync it to OneDrive I manually create a 'parallel' folder and copy only the content I want using 'RoboCopy'. You can then schedule this task.

The files & folders names/wildcards to ignore were copied from the .gitignore file

The solution is based on a few articles:

Schedule RoboCopy task and simple examples

RoboCopy documentation by Microsoft

you can also use GUI to create the .bat task file to be scheduled. GUI by microsoft & ChoEazyCopy GUI which is much better


For Typescript users out there, add node_modules to your path to avoid module resolution issues.

In tsconfig.json add :

"paths": {
  [... some paths ...],
  "*": [
    "node_modules/*"
  ]
}

Edit: Still have some issues. I can build the project but ts-node and nodemon fire Cannot find module errors. Well, this is still an issue then :(