git status is not showing my changed files - node.js

I have a Node.js project and have imported a few Node modules. The git_status command shows the files that I changed in the project. I have also changed some files under the node_modules directory but those are not shown. The node_modules directory is shown as untracked.
Untracked files:
(use "git add <file>..." to include in what will be committed)
node_modules/
src/js/main-release-paths.json
I have changed only one file in node_modules:
node_modules/#oracle/grunt-oraclejet/node_modules/#oracle/oraclejet-tooling/lib/serve/connect.js
How can I track this file?

You shouldn't add the node_modules folder to git then it's managed bei the npm command. So remove that folder and use the npm install command.
When you have added something in that folder it's shown as changed, if you had first added some files. So i hop that folder is in your .gitignore file and ignored. You should check that.

Assuming you have git repo, Please check the .gitignore files inside your project. Entries in .gitignore will be ignored by Git and will not show up when you do git add.

Adding the file to track
This is most likely not a suitable approach (read below). However , if you really want to do it:
In order to track a file nested under node_modules, you should add the directory. All of its content will be staged in git
git add node_modules
Recommended approach
You probably don't want to track the contents of node_modules directory, because:
running npm install will wipe your changes
hundreds or thousand files are likely to end up present in that directory
tracking package.json and package-lock.json in git is sufficient, then populate node_modules using npm install command.
You are not supposed to modify files located in node_modules directory directly. Instead, fork the module in question, modify it, and:
either publish your own version in npm
or reference it as a git repository inside package.json (how-to)
Then, also add node_modules directory to the .gitignore file.

If the whole directory is untracked, git status will show only the directory. Because it's untracked, there is no meaningful difference to git between changed and unchanged files in there.
Other answers have already addressed that tracking node_modules is nota good idea. Of you want to track your changes to modules, consider cloning the module repo and including it as a git submodule.

It should be inside .gitignore file. A .gitignore file should be committed into your repository, in order to share the ignore rules with any other users that clone the repository.

Related

Why is multiple Gitignore ignored?

The folder structure is as follows.
Created for personal project.
"SERVER" directory works as a server with nodejs, and "CLIENT" directory works with React. So, if you run npm run start inside the "SERVER" folder, the server starts, and the react html generated by "CLIENT" is imported and displayed on the screen.
In git, there is a folder called "GATHER" that contains all of these CLIENT and SERVER folders.
Currently the .gitignore file is only inside the "CLIENT" folder. The contents of the "CLIENT" folder are as follows.
/node_modules
/.pnp
.pnp.js
/build
So, in "SERVER", all parts that need to be added to .gitignore such as node_modules and build are detected as changes.
To solve this problem, I added .gitignore to the parent folder of "CLIENT" and "SERVER", but it doesn't work.
So, as a result of searching, I found that it works even if there are multiple gitignores.
I created a gitignore file in the "SERVER" folder and entered the same code as "CLIENT". But it doesn't work. node_modules are still being tampered with.
I tried git rm --cached node_modules
fatal: pathspec 'node_modules' did not match any files
I only get this error. How can I solve this?
In conclusion, I want to apply .gitignore to each of the "SERVER" and "CLIENT" folders in the GATHER folder.
If your shell current working path is where the .gitignore (and SERVER folder) are, the command to use is:
cd /path/to/repo/GATHER
git rm -r --cached -- SERVER/
To remove the full SERVER content (that you want to ignore).
The OP adds in the comments:
build/ and node_modules/ (directories that should be ignored) are all being ignored.
In addition, since it is recommended to manage only one gitignore file, there were originally two gitignore files: CLIENT internal gitignore and SERVER internal gitignore.
I ended up putting one gitignore in the parent directory of these two directories.
To check if this work, you can use git check-ignore -v
git check-ignore -v -- CLIENT/build/a/file/inside
If you see an ignore rule, the file is ignored.

.gitignore file is not generated automatically

I initialized an empty git repository with git init and it's making only a .git folder. It's not making .gitignore itself. When I tried making a .gitignore file and then wrote \node_modules, it is not ignoring the node_modules folder. I am working with Linux OS on the NodeJs application and was pushing the code on GitHub.
It doesn't get generated automatically. You need to first commit it in the repo and use "node_module/" in your .gitignore file.
$ cat .gitignore
node_modules/
Try this as follows.
Do git init
Add .gitignore file
Add file path or folder path you
want to avoid
Commit as Git ignore file added or something.
Push to repository
After then if it's not working,
Clone it to another folder. It will work, sure.

Include files on npm package but not on the git repo

I have a fairly straightforward question it possible to include in the npm release the artifacts (files) but avoid to include them in the git repo (I guess with a combination of .npmignore and .gitignore)?
Yes - this is absolutely possible.
The way that npm handles this is during publish as follows:
Check if there is an .npmignore file present. If yes, exclude those files from the release. If not, fallback to step 2.
(optional) Check if there is a .gitignore file present. If yes, exclude those files from the release.
So, if you want to keep everything in your npm release, but avoid pushing them to your git repository, all you need to do is put those files in .gitignore and create an empty .npmignore file.
This way, npm won't try to fallback to .gitignore.

npm install is partially cloning the source from git repo

When I run npm i command it partially clones the code from bitbucket repo. Here are some dependencies in package.json
"dependencies": {
"bluerain-app-hello-world": "git+ssh://git#bitbucket.org/projects/bluerain-app-hello-world.git#build_issue",
"graphql-tag": "0.1.16" }
And this is the source of my module
But when i list down cloned rope from node_module by
ls node_modules/bluerain-app-hello-world/
It gives this list
index.js node_modules package.json README.md src
There is a dist folder in my source but it didnt clone properly. I tried to remove my node modules and reinstall it but it didnt work. Any suggestions please?
I started working on a boilerplate who had added the files array field in package.json and specified only the src folder. This is why when my node module installed, it didn't contain the dist folder.
According to official documentation
The "files" field is an array of files to include in your project. If you name a folder in the array, then it will also include the files inside that folder. (Unless they would be ignored by another rule.)
You can also provide a ".npmignore" file in the root of your package or in subdirectories, which will keep files from being included, even if they would be picked up by the files array. The .npmignore file works just like a .gitignore.

git exclude everything in folder except a subdirectory

I am working on a node js project and I am creating a node module that for now want have it added to the project I am working on. So in my gitignore I put the following but it did not work
node_modules/*
!node_modules/zephyr-rest
The zephyr-rest is a directory, so I want just that directory and its files to be saved. I do not want any other sub directory under node_modules to be versioned. Thanks
Try this, worked for me.
node_modules/*
!node_modules/zephyr-rest/
After git status it will show like this
Untracked files:
(use "git add <file>..." to include in what will be committed)
node_modules/
Don't worry because git status just tells the top-most untracked folder as untracked not every subfolder, so this tells there is an untracked folder node_modules. Doing git add node_modules should add just node_modules/zephyr-rest/

Resources