Git dependency with npm keeping .git files - npm

I am using a private GIT repository as dependency in npm:
"name": "git+ssh://git#git.domain.com:user/repo.git"
This is working and clones the repository inside node_modules when I do npm install.
The matter is it deletes .git folder and .gitignore file. I want to keep those file (to do commits later) ¿How to keep those files?

It's better to use npm link ../path-to-local-git after you git clone your dependency repo.
git clone <repo>
cd PROJECT
npm link ../<repo>
and you see the build process run.

It sounds like you would be better served keeping a local checkout of the project and specifying the dependency with a local path.
cd ..
git clone ssh://git#git.domain.com:user/repo.git
cd repo; npm install
cd ../PROJECT
npm i --save ../repo
That way you can make changes and commit them back.
npm treats the contents of node_modules as private, so you should not expect to be able to get into the node_modules directory and do anything useful. If you want to maintain a git checkout of a project which is a dependency, then do that, but don't combine it with the dependency-management that npm does.
Also check out npm link if the dependency is itself a npm-compatible package. https://docs.npmjs.com/cli/install

Related

How can I install all files from GitHub repo with npm and ignore file field on package.json?

I was using yarn in my project and use two git repositories as dependencies. Yarn get all the files from the GitHub repo, and ignore the field "files" in package.json or the .npmignore file.
Particullary I need the whole repo because. The reason is that I really need the generated stuff when I ran the commands there. I want to migrate to npm.

Can I re-create node_modules from package-lock.json?

I cloned a repository from github which has a package-lock.json (but no package.json). Then in a git bash terminal I go to the directory and run npm install but I just get a message saying there is no package.json and then everything in package-lock.json gets deleted so it's basically empty except for the project name and version.
I thought running npm install with a package-lock.json in the directory was enough to re-create node_modules, but am I seriously misunderstanding how this works? By the way I have node 8.12.0 and npm 6.4.1 and am running on Windows 10. Also, I think the package-lock.json was created on a unix system so could there be problems when using package-lock.json on a different OS?
I already tried running npm init just to get a package.json file and then running npm install but that still didn't get me a node_modules folder.
Starting from Mar 5, 2018, you can run npm ci to install packages from package-lock.json.
npm ci bypasses a package’s package.json to install modules from a
package’s lockfile.
https://blog.npmjs.org/post/171556855892/introducing-npm-ci-for-faster-more-reliable
package-lock.json records the exact version and url of packages need to install, thus you can use npm to install them accordingly:
npm can install from urls that point to tarballs
--no-package-lock option to tell npm to not touch package-lock.json file
For example, to install all packages in package-lock.json:
cat package-lock.json | jq '.dependencies[].resolved' | xargs npm i --no-package-lock
jq is a command line tool to pares jq, you can write a simple JavaScript script to parse it instead (if you do not want to install jq or learn jq's query syntax).
AFAIK, the package-lock.json file relies on the presence of a package.json file, so you'll not be able to recreate your node_modules folder from the package-lock.json file alone (happy to be proved wrong here).
Therefore, your best bet is to (mis)use a module like auto-install that is capable of generating the package.json file based on a project's dependencies, as they appear in the files.
Install it globally (npm install -g auto-install), then you'll need to generate an empty package.json file for it to run (use npm init -y in your project root). Kick things off with the command auto-install and it should add the dependencies to the package.json file.
HTH

Forking and changing an NPM package

I have been using an NPM for angular-4 which support drag and drop objects (ng2-drag-drop). I found a missing functionality and decide to add it to the package.
What I did is forking the original project and adding my changes. after commit/push to my git I then used the following command to install my NPM :
npm install https://github.com/..... --save
the NPM installed successfully however when looking in my node_modules I see that the source files are missing and I have only the root directory including the package.json and some other files . any source files are missing.
I then tried to instal the NPM directly from the author git so instead of running :
npm install ng2-drag-drop --save
I used
npm install https://github.com/ObaidUrRehman/ng2-drag-drop.git --save
and I had the same issue with my fork.
Why the installation is different between the author git and the named package ? isn't it taking the files from the same location ? if no, what should I do to make it work ?
The reason you are not able to see the src folder is
If you see the git repo you will find two files
gitignore & npmignore.
In that npm ignore file you will find the src has been ignored to be prevent it from being added to the package when running npm commands .
Keeping files out of your package
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. Like git, npm looks for
.npmignore and .gitignore files in all subdirectories of your package,
not only the root directory.
You need to overwrite these settings to be able to get src contents in node modules when you do npm install

How to install bower component with git submodules as a dependency?

I have a library which requires 2 dependencies to exist in the "vendor" directory. This library has these 2 dependencies installed as git submodules on Github, so to install my library from a Git clone you would do:
git clone --recursive https://github.com/MYLIBRARY.git
And to install it as another Git submodule, you would do:
git submodule add https://github.com/MYLIBRARY.git && git submodule update --init --recursive
The issue comes when trying to install it with Bower, as the Git submodules do not get downloaded with the bower package (the dependency folders exist, but are empty). The 2 dependencies exist separately on bower, but as they are required to be in the "vendor" directory of the main library, I cannot include them as bower dependencies. I also can't assume that any project installing my library will be a Git repo, so I can't do something like:
bower install MYLIBRARY && git submodule update --init --recursive
Which would work if the parent project you installed my library in was a Git repo, but would error if not.
I'm worried I may have to have an instruction similar to:
bower install MYLIBRARY && cd bower_componets/MYLIBRARY && git clone https://github.com/DEPENDENCY1.git vendor/DEPENDENCY1 && git clone https://github.com/DEPENDENCY2.git vendor/DEPENDENCY2
This can be tidied slightly by adding an npm run script in package.json:
"scripts": {
"bower_vendor": "git clone https://github.com/DEPENDENCY1.git vendor/DEPENDENCY1 && git clone https://github.com/DEPENDENCY2.git vendor/DEPENDENCY2"
}
Then the installation instruction would become:
bower install MYLIBRARY && cd bower_componets/MYLIBRARY && npm run bower_vendor
Which is a little neater, but again wouldn't work if the user has customised their bower_components directory (which is simple enough to change in the above command, but is still another concern).
I'm hoping I've just missed something somewhere which will solve my issue. I read that bower performs a git clone of the repo, so if there was a way to get it to perform git clone --recursive that would be amazing.
Thanks

NPM dependency is not creating git submodule

I'm running grunt.js. One of its dependencies is node-jshint which has the actual jshint files added as a submodule. (I'm using my own fork of node-jshint so that I can do some modifications to the jshint source).
If you npm install in grunt, it'll install node-jshint and it will also build the jshint submodule.
However, my package.json file is pointing to the url of my node-jshint fork rather than the npm version, and the jshint submodule never gets created.
It seems like when npm grabs dependencies normally, it's internally running git submodule update, but when it grabs dependencies via a URL, it never runs that command.
Is there anyway to force npm to run git submodule update when it install a dependency, like via package.json?