24

I’m a front-end developer, and I keep my front-end projects in OneDrive. Due to how Node.js organizes dependencies, each project has a node_modules directory with tens of megabytes of tiny files which take forever to sync.

What can I do to avoid synchronizing node_modules directories while keeping the projects in OneDrive?

2
10

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.

3
  • 2
    As of March 2019 this is still the best answer that I've found. It isn't ideal but it is functional. – Taul Mar 12 '19 at 22:04
  • 1
    As of July 2019: If you are getting sync errors about 'The file or folder already exists', go to OneDrive web UI, and remove the node_modules folder there. If it's there, OneDrive complains that they are different. If it's not in cloud, there is no difference and OD doesn't complain. – Tomáš Kafka Jul 24 '19 at 12:47
  • 1
    @TomášKafka Then the folder will start syncing again after a restart of OneDrive. – renklus Apr 10 '20 at 0:32
8

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

3
  • This does not seem to work. I'm getting Cannot find module errors when trying to launch the app (using Typescript). – Pierrick Bignet Dec 5 '17 at 13:45
  • @PierrickBignet A guess: you indirectly run some native code which isn't designed to process symlinks, that's why creating symlink requires privileges. – InQβ Sep 14 '19 at 1:41
  • Sadly, this didn't work for me. npm install regularly failed with maximum call stack size exceeded, and attempting to install to the default node_modules folder, copy all files to the symlink location, then create the symlink afterwards caused failures when attempting things like npm run start etc. – Mike Willis Oct 14 '20 at 20:31
1

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

1

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

0

After reading these proposed options, I ended up deciding to put everything outside of OneDrive and create a script with Robocopy to backup to OneDrive whenever there was a change in the projects folder.

Something like:

robocopy SOURCE DEST /mir /xd node_modules
0

Just delete the files or folders in OneDrive that you don't want to sync , they're still there in your local disk.

0
-1

I have found that using dev containers can fix this issue if you can put the time into creating your environment.

Specifically this part.

1
  • Welcome to Super User! Please quote the essential parts of the answer from the reference link(s), as the answer can become invalid if the linked page(s) change. – DavidPostill Sep 25 '20 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.