Include files on npm package but not on the git repo - node.js

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.

Related

git status is not showing my changed files

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.

dynamically add folder to ignore when using npm publish

I have a static .npmignore file with
foo
bar
baz
in it. When I publish to NPM, the contents in those 3 dirs will be ignored.
My question is, is there a way to dynamically add a folder to ignore when using npm publish at the command line?
Something like:
npm publish --ignore=.r2g
here we can ignore a folder called .r2g
Here are the npm-publish docs
First, a static solution:
You can include a .npmignore file per directory that you want to exclude.
As the doc states:
Like git, npm looks for .npmignore and .gitignore files in all subdirectories of your package, not only the root directory.
I created an example of this in https://github.com/dral/npmignore-example (and the corresponding package npmignore-example).
file structure
index.js
.npmignore
bar/index.js
baz/index.js
foo/index.js
/.npmignore
the root .npmignore removes only the baz directory. the nested foo/.npmignore removes all content (from this point on).
The installed package includes only
index.js
bar/index.js
In order to do so dynamically you can use a script that adds and then removes a simple .npmignore file in the selected folder(s).
echo '*' > .r2g/.npmignore && npm publish && rm .r2g/.npmignore
Then if you need to automate this, consider using prepublish and postpublish scripts that take into account an env variable so you can use something like NPM_IGNORE=.r2g npm publish.
Hope this helps.

How to commit NPM module Names to git without the actual files so that they can get rebuilt at the other end?

I'm developing a node project which depends on several npm modules. What I'm currently doing is committing all those modules to my git repository, pulling on my server, and then rebuilding the modules on the other end because the system architectures are different. What I would like to do instead, is just commit enough stuff so that npm knows what it needs to rebuild, and nothing more.
I would hoping I could do this without actually committing all the module files. I know little about npm's internals, but see that every module has a package.json file, is this all that npm will need? And if that's the case, how would I go about ignoring all files in my node_modules folder except the package.json files?
Thanks.
Why can't you just have a package.json file in your main application root listing all your dependencies? This file should then be the only thing checked in to source control.
At build time or when other developers pull the code, npm install should be run from the same directory that has your package.json file. It will then pull down all your dependencies locally.

Does NPM ignore files listed in .gitignore?

I have a file that is generated by npm install command (using preinstall task). I don't want to add it in the git repository, nor in the NPM project.
Supposing the file name is foo.json, I added it in .gitignore file as foo.json.
Is this enough to avoid uploading it on NPM registry?
I know I can add .npmignore file that will surely ignore the file, but I won't add it if .gitignore already does this.
If a project has both an .npmignore and .gitignore file, npm will only use the .npmignore file.
From the documentation:
Use a .npmignore file to keep stuff out of your package. If there's no .npmignore file, but there is a .gitignore file, then npm will ignore the stuff matched by the .gitignore file. If you want to include something that is excluded by your .gitignore file, you can create an empty .npmignore file to override it.
In simpler terms, npm prefers the .npmignore file if it is there, but will fall back to the .gitignore file.
In many cases, both Git and npm can ignore the same files, so it makes sense to just use a .gitignore file on its own. If there's ever a discrepancy (i.e. npm and Git need to ignore different files), then you need to maintain separate .gitignore and .npmignore files.
More information on what to put in .npmignore files: Should I .npmignore my tests?
For anyone reading this trying to ignore a file/dir from git but wish to include it in npm publish and have tried using an empty .npmignore file with no luck. This works.
In your .gitignore file, add the file/dir you wish to exclude **/build for example and in your .npmignore file make sure you specify the same file/dir but with the ! prefix so for the build example you would include !**/build

.gitignore and node_modules

I am trying to figure out the best way to handle node_modules in git. From what I read, there are two options:
A. Keep all the node_modules in the git repository, together with my project. This way, a person cloning my project does not have to install any modules.
B. Don't keep any node_modules in the git repository, i.e., have a ".gitignore" file that contains "node_modules".
However, in some projects, I don't see any of these two options. For example, in this node.js project, there are no node_modules, but also no .gitignore file...
When I fork this repo, and do npm install, the folder is filled with node_modules, and since there is no .gitignore, git tries to commit them...
What am I doing wrong?
You are not doing anything wrong, npm install will download and install all the dependencies of the project, which are defined in package.json:
"dependencies": {
"underscore" : ">=1.3.3"
},
"devDependencies" : {
"mocha" : ">=1.0.0",
"canvas" : ">=0.10.0",
"cradle" : ">=0.2.0",
"should" : ">=0.6.0",
"async" : ">=0.1.18"
}
There are many possible explanations as to how these do not appear in the source tree:
One possibility is that they are installed globally.
One other possibility is that they are actually added in .gitignore, but that .gitignore itself is never committed (this is done by adding .gitignore in the .git/info/exclude file of the project.
In any case, the only way to know why no .gitignore exists is by asking the project's owner :).
Am not an expert for this node modules stuff but one things for sure. If there is no .gitignore then no files are being ignored. This clearly means that the committer is taking care of it manually not to commit these modules.

Resources