How to configure package.json to place node_modules in another folder outside of the web app?

156 views Asked by At

I have a web app located under C:\Google-drive\vue-app. Running yarn build command will install node_modules folder in C:\Google-drive\vue-app. I am using Google Drive to sync my web app source code to Google cloud. The problem I am facing is the thousands of files in node_modules will crash Google Drive.

Is there some way to configure package.json to install node_modules folder outside of C:\Google-drive\vue-app and have it installed in a location called C:\temp\node_storage?

I am open to any solution that can stop Google Drive from syncing node_modules folder. It need not necessarily involve configuring package.json

3

There are 3 answers

0
user3848207 On BEST ANSWER

I found a solution to my question. Not satisfactory but it works.

Use FreeFileSync application. This free app allows the user to select folders to sync to Google Drive. The user can select the source code folders to sync to Google Drive, excluding node_modules folder.

For more convenience, there is a real-time sync feature in this app. It requires some configuration. I have not tested it. If this real-time sync feature is not enabled, then manual syncing is required.

4
Jerren Saunders On

It looks like there are a few options suggested over at Excluding node_modules folders from syncing with Google Drive.

Replacing node_modules with a symbolic link looks to be the simplest.

I know Dropbox has an undocumented method using an alternate file stream that can be done in Windows to avoid syncing certain folders. I use this batch script to set the alternate file stream:

@echo off

REM This script should be executed from the root of the repo
REM Folders which potentially can become large (node_modules, logs, etc) will be created
REM then a stream file created that instructs the Dropbox service to keep the folder locally, but not sync to the cloud
set repoRootPath=%cd%

REM Back Folders:
mkdir %repoRootPath%\back\node_modules
echo 1 > %repoRootPath%\back\node_modules:com.dropbox.ignored

mkdir %repoRootPath%\back\logs
echo 1 > %repoRootPath%\back\logs:com.dropbox.ignored

1
Leftium On

I don't think it's possible to configure package managers like that. The only reason I purchased Insync was so my node_modules folders would not be synced to cloud services like Google drive or Dropbox.

Insync supports ignore rules: https://www.insynchq.com/i/features/ignore-rules

Ignore rules works like .gitignore

Set rules for files or folders you don't want to sync. ... node_modules, anyone?

Insync works very well. Overall the experience is much better than having node_modules constantly and needlessly syncing to the cloud.

Sometimes the sync gets a little janky after removing or adding a whole bunch of files suddenly (like npm install). Other times git gets confused because files seem like they were modified, but they were just deleted and recreated by sync. (A simple git add . resolves this.)

Also at first the Insync UI can be a little confusing to get set up correctly. There are just a lot of features I don't need like two-way vs. one-way cloud sync. But once it's set up, it works automatically. So you can pretty much forget about it.


A bonus feature of Insync is I can move my git projects outside the folder (and even drive) limitations dictated by Google Drive/Dropbox. For example:

  • My git projects are synced to c:\dropbox\p\

  • Insync lets me work with my projects from s:\p (I think a bare drive like p: is also possible.)

  • (I configure Dropbox not to sync c:\dropbox\p\ so I only have one copy synced locally)


Before that, I was using this script to tell Dropbox not to sync node_modules folders. But it only applies to existing folders, so must be repeated if new node_modules folders are created:

ignore_node_modules.sh

#!/bin/sh

find "$1" -type d | grep 'node_modules$' | grep -v '/node_modules/' | xargs -I {} -t powershell -command "Set-Content -Path '{}' -Stream com.dropbox.ignored -Value 1"

See this question for more details about the script above: https://stackoverflow.com/a/69655523/117030 (I used git-bash for windows, thus the mix of bash and powershell.)