npm install creates long file names that can't be pushed to - node.js

I have some code created in the latest version of node.js. The file names in the node_modules folder can be very longer, and are sometimes longer than GitHub Desktop will accept. When trying to push some code to GitHub, I got the error:
error:
lstat("node_modules/npm/node_modules/libcipm/node_modules/pacote/node_modules/make-fetch-happen/node_modules/http-proxy-agent/node_modules/agent-base/node_modules/es6-promisify/node_modules/es6-promise/dist/es6-promise.auto.js"): Filename too long
fatal: Unable to process path node_modules/npm/node_modules/libcipm/node_modules/pacote/node_modules/make-fetch-happen/node_modules/http-proxy-agent/node_modules/agent-base/node_modules/es6-promisify/node_modules/es6-promise/dist/es6-promise.auto.js
What causes this, and what I can I do to fix it?

Usually the node_modules folder is excluded from the repository by adding it to the .gitignore file.
When cloning or pulling the repository you simply run the npm install command and all the packages are automatically downloaded to the node_modules folder.

Related

node_modules folder missing after push to git remote repo

Has anyone faced the problem of missing node_modules directory or any other dir after pushing to remote git repo?
I just did a git push origin master yesterday and can't find node_modules directory on my local repo since then.
I have the package.json file in my project dir, so I can just do npm install to get the modules back but wondering why it got deleted at all.
For more info, I have node_modules ignored in .gitignore file along with few others but I can see other ignored files present still in my project dir except node_modules. Anything I'm missing which can cause this? I did backup of my project dir just before pushing it to remote and can see node_modules dir present there.

git add . command no response when adding node_modules

I have run
git init
npm install --save-dev webpack
Then, there is a package.json and a node_modules directory in the root directory.
I want to add the node_modules directory into git repo.
After I run
git add .
There is no response any more. And I found the git.exe process occupy more and more memory usage , but there is not any response after several hours.
What is wrong with it?
Is it caused by pretty much files to be added into repo by git?
How can I debug what happens in detail when that command is executed?
Everything works fine if I just git add some other files/folder which is not node_modules.
Have you git init the repo folder? If yes, then you can run git status. There you should see any new or changed folder.
If node_modules is not mentioned, I guess you have a .gitignore file in your project folder. If you want to git add the node modules folder you will need to remove node_modules/ from .gitignore.
npm install --save-dev webpack will just add one more dependency in package.json and install webpack in your node local repo. This is not a git related command.
Check also this question: Git - Ignore node_modules folder everywhere
Hopfully this will help,
I figured out myself.
This is caused by pretty much symlinks in the node_modules directory. If i evaluated the git-bash as administrator, the new installed node_module symlinks can be created as linux. And then when git add works well. Maybe, if without evaluated git-bash, git-bash can not create symlinks which cause pretty much files need to be git added, even worse maybe a circular reference between symblink and node module staff (not sure), that cause memory consumption of git increase always and no response.
Hope it can help others if met the same problem on windows.

Why does npm install keep saving under node modules folder even though i changed directory to another folder?

In terminal, I entered the command cd /Users/MyUserName/Google/Google Drive/Coding then entered the command npm install underscore. So I figured the underscore module would be saved under my Coding folder; however, it keeps saving under the directory: /Users/MyUserName/node_modules/
How do I change the setting so that when ever I change my directory in terminal and enter a npm install command, the module gets installed in the respective changed directory?
From the documentation:
Starting at the $PWD, npm will walk up the folder tree checking for a folder that contains either a package.json file, or a node_modules folder. If such a thing is found, then that is treated as the effective "current directory" for the purpose of running npm commands. (This behavior is inspired by and similar to git's .git-folder seeking logic when running git commands in a working dir.)
If no package root is found, then the current folder is used.
So it seems the package is installed under your home folder because it already contains a node_modules folder. If you remove that the install should go into the current folder.
Alternatively: Add a node_modules folder to the current folder.

How can I commit changes that I've made in node_modules back to git?

Sometimes it's easier to maintain a fork of a node package for your module, I'd like to be able to edit a module that's in node_modues that I've installed via npm install githubaccount/myrepo.git.
Currently any changed that I made to a file I have to copy back to the repo. This is tedious.
How can I edit modules in npm and have them tracked by Git?
Python's pip has an option where you can define an 'egg' which symlinks a clone of the repo to the site_packages folder. I know it's a different system, but I've found it really quite useful in the past.
You can use the npm link command to do this. Here's how to use that. Let's assume that your npm module is located in ~/npm-module and your project using the npm module is in ~/my-project.
cd ~
# First clone the npm module
git clone https://..../npm-module.git
# Go into the module's directory:
cd npm-module
# Link the module
npm link
# CD into the project using the npm module
cd ../my-project
# Link the module
npm link npm-module
The first call to npm link will create a link in npm's cache directory pointing to your local clone of the npm-module. The second call will link it into the project that's using the module (into the node_modules folder).
If you take a look at your project's node_modules folder, you will see that it now contains a symbolic link to the npm-module's sources. Any changes you make in that folder will also be in the cloned project. You can commit changes there and push them back to your remote.
This is similar to what you do with Python's egg feature.
Here's the npm link documentation.

Gulp installs outside project root

I've encountered a weird issue when installing Gulp in a new project.
Let's say I have the following path:
c:/development/myproject
When I run npm install gulp in that directory, the node_modules folder actually gets created in
c:/development/node_modules
instead of
c:/development/myproject/node_modules
And all of gulp plugins also get installed in that directory outside my project root.
I also have an earlier project where gulp was already installed before, and when I tried to rerun gulp installation in that project directory it was installed correctly in the project root (for example: c:/development/myolderproject/node_modules), not outside.
I don't think it has anything to do with the case, but the new project is using Laravel 4, while the other one is on Laravel 5.
I don't recall having to set any specific configuration before, so I'm totally confused why it behaves differently.
When you did npm install it found package.json from parent directory and thought it was the package root.
Related docs: https://www.npmjs.org/doc/files/npm-folders.html#more-information
Starting at the $PWD, npm will walk up the folder tree checking for a folder that contains either a package.json file, or a node_modules folder. If such a thing is found, then that is treated as the effective "current directory" for the purpose of running npm commands. (This behavior is inspired by and similar to git's .git-folder seeking logic when running git commands in a working dir.)
If no package root is found, then the current folder is used.
I run Ubuntu 15 and I had a similar issue where gulp was installing the node_module folder somewhere I couldn't find. gulp would say ../../node_modules was the location but it was NOT in my project folder.
I figured out from the link above and some more research I just needed to run npm init to create a project.json in my project folder. gulp was installing the node_modules in another folder because it searches for a project.json file to install the folder node_modules into.
Hope this helps anyone else solve this silly problem.

Resources