npm install not installing latest version on GitHub - node.js

I have a module called 'sails-mongo' and I want to update it to the newest version using the following command:
npm update sails-mongo --save
I also tried uninstall then install again. I tried sails-mongo#latest and sails-mongo#beta.
Problem:
The current version (master) on GitHub the package.json (https://github.com/balderdashy/sails-mongo/blob/master/package.json) file has:
"dependencies": {
"async": "~0.2.9",
"lodash": "~2.4.1",
"mongodb": "1.4.2",
"waterline-errors": "~0.10.0"
},
And in the one being updated
"dependencies": {
"async": "0.2.10",
"underscore": "1.5.2",
"underscore.string": "2.3.3",
"mongodb": "~1.3.23"
},
The only way I get the master branch is using the command npm install git+https://github.com/balderdashy/sails-mongo
Why doesn't sails-mongo#latest install the master branch?

By default, NPM dependencies are pulled from the NPM repository. Authors must manually upload new versions of their software to the NPM repository, so the "#latest" version of the code hosted on NPM is different from the latest version of the code that exists anywhere (e.g., on GitHub).
According to the NPM repository's info page on Sails, the latest NPM-hosted version is 0.9.16 while the current GitHub version is 0.10.0-rc3.
If you want to have your project depend upon a particular branch or commit of a particular Git repo (instead of the version(s) hosted on the NPM repository), the NPM developers have included an explicit mechanism to allow this, detailed in "Git URLs as Dependencies" in the package.json docs:
Git URLs as Dependencies
Git urls can be of the form:
git://github.com/user/project.git#commit-ish
git+ssh://user#hostname:project.git#commit-ish
git+ssh://user#hostname/project.git#commit-ish
git+http://user#hostname/project/blah.git#commit-ish
git+https://user#hostname/project/blah.git#commit-ish
The commit-ish can be any tag, sha, or branch which can be supplied as an argument to git checkout. The default is master.
In fact, it's easier still to use a Github.com repo as a dependency:
As of version 1.1.65, you can refer to GitHub urls as just "foo": "user/foo-project". For example:
{
"name": "foo",
"version": "0.0.0",
"dependencies": {
"express": "visionmedia/express"
}
}
So, to use the Sails GitHub repo, simply use:
"dependencies": {
"sails": "balderdashy/sails-mongo",
...
}
And to use the exact state of Sails as it exists on GitHub as of April 28, 2014, use:
"dependencies": {
"sails": "git://github.com/balderdashy/sails-mongo#b9cdce9a48",
...
}

I had a similar issue. Via the NPM Registry I was trying to get the latest from a project I saw in in GitHub, like this:
//package.json
"devDependencies": {
"foo-package": "^3.3.0",
}
But the code I got back from npm install (as observed in the node_modules/ folder) was not what I saw in GitHub repository's master branch. I was confused; as the two didn't match.
I eventually found: https://docs.npmjs.com/cli/view, which reveals some information (versions and dates) of what the NPM Registry is aware of for a particular repository.
// Console example
npm view foo-package
After confirming that what I wanted from GitHub repository's master branch wasn't in the NPM Registry, I eventually changed my approach Git URLs as Dependencies, just as #apsillers answers.

Related

How to deploy npm package dependencies to gitlab registry?

I am working in a environment where build env doesn't have access to internet and my application uses npm packages during the build. Till now we have copied the node_modules to source and used them to build. We recently moved to gitlab premium and want to use gitlab registry to store our npm packages. So our issue is gitlab allows to publish only the registry and not its dependencies. But that will not help use because when we do npm install it will to get the some package dependencies from internet(registry.npm.org.js) which will fail as it will not have internet access.
My requirement is i should be able to publish all the dependencies also along with package as tar files in the npm registry itself so that during the build when npm install happens it should download packages from gitlab registry itself.
My package.json looks like this:
{
"name": "#npm-project/aws-sdk-v3-iam-examples",
"version": "1.2.0",
"main": "index.js",
"repository": "git#github.com/awsdocs/aws-doc-sdk-examples/tree/master/javascriptv3/example_code/iam.git",
"author": "Brian Murray <brmur#amazon.com>, Alex Forsyth <alex-git#amazon.com>",
"license": "Apache 2.0",
"dependencies": {
"#aws-sdk/client-sqs": "^3.32.0",
"#aws-sdk/node-http-handler": "^3.32.0",
"#aws-sdk/types": "^3.32.0",
"ts-node": "^9.0.0"
},
"devDependencies": {
"#types/node": "^14.0.23",
"typescript": "^4.0.2"
},
"publishConfig": {
"#npm-project:registry": "https://gitlab.com/api/v4/projects/xxxxx/packages/npm/"
}
}
i want #aws-sdk/client-sqs,#aws-sdk/node-http-handler,#aws-sdk/types packages to be available in my npm registry itself.
it might look something like this,
What you are searching for are mirror/proxy Registries like Nexus, jFrog or Verdaccio. Maintaining all your packages manually will be incredibly tedious.
In the GitLab Docs I can't find such a feature, they only support publishing scoped private packages so packages like ts-node could not be published at all. So it might be an option to setup such a mirror/proxy Registry yourself which could update automatically. Especially Verdaccio is OpenSource, free and easy to setup. Also jFrog+GitLab is a common combination I think but I think it's a paid product.

How To Update Git Hash in npm package.json git project?

How do I specify exact git hash in package.json dependencies for a Github project, and have an easy way to upgrade it at the same time?
My package.json is as follow:
{
"name": "my faboulous app",
"version": "1.0.0",
"dependencies": {
// ...
"request": "request/request#5ee89063cd"
}
}
It relies on a Github project: https://github.com/request/request and uses specific revision which is 5ee89063cd.
I want to stick to specific version, so when someones clones my project and calls npm install she has the same request dependency version as me.
But at certain point in time, there comes an important bugfix for me, and I want to upgrade the revision in package.json to the newest version that is available at Github.
Is it possible to achieve this with npm update command? How can I upgrade the revision from command line, instead of manually editing the file?
My understanding is that, when I call npm install it always takes the hash that is specified in package.json. But when I call npm update I would like to have package.json request dependency updated to the latest repository version of it with the newest revision hash.
How can I achieve that? If not with npm update maybe there is the other simple way?
Update
Checkout the git-npm-updater package which can get the job done for you.
git-npm-updater automatically updates npm dependencies presented in your package.json and create pull requests to your git repo.
Hope this helps!

npm package from private github repo, install vs update (package.json)

I have already seen this question npm-install-vs-update-whats-the-difference
My question is around using install vs update for private github repo using git+ urls.
I have a private github repo which is used in the grunt. This repo receives frequent updates. I installed this repo using git+ssh url as mentioned here npm install git remote url
Everything works fine when installing. Problem comes when updating the package. As per my understanding and question mentioned above, npm updates the package to latest version when doing npm install but this doesnt seem to be case with package installed from github. I had to use npm update to get the latest version. I dont mind using npm update but I have observed that its slow compared to npm install. Can anyone put their thoughts why this might be happening.
My package.json looks like following
{
"name": "My Project",
"version": "1.0.0",
"dependencies": {
"grunt": "^0.4.5",
//Relevent package
"my-tasks": "git+ssh://git#github.com:Flutterbee/my-tasks.git"
}
}
PS : Using npm 3.3.3 (if that makes difference)
Can you try with specifying the branch name with in your package.json like this
"my-tasks": "git+ssh://git#github.com:Flutterbee/my-tasks.git#master"

nodejs project dependencies synchronization

I am creating a nodejs project and wish to have same library versions used across dev, qa, staging and production environment.
Tried package.json but was unable to find good help. Need some suggestion here.
EDITED
{
"dependencies": {
"ioredis": "1.7.5",
"redis": "0.12.1",
"redis-hash": "0.0.4",
"debug": "*"
}
}
Specify your dependencies using the exact versions in the package.json file, and when you run npm install it will get the proper versions of all your dependencies.
If you don't specifiy an exact version for the libraries, there's a chance that there's a new version since the last time you ran the command, you will end up with different versions of your libraries on your servers, which might not be what you want.
NPM has an outdated command which will list all the dependencies that have fallen behind and need to be updated. You can then run npm install.

Make app installable from both GitHub & NPM

require('./module') VS require('module')
I want my app be installable from both sources GitHub and NPM. I am struggling with require directives and folder structures. NPM installs modules in node_modules but git would clone into newly created directory inside the working directory and I have to require with ./mymodule syntax.
I have about 10 modules used by my app, each of them has it's own repository.
How to solve this problem? How to organize folders on the development machine? How to organize repositories?
You just have to clone your repositories in the node_modules folder!
If you want to automatically add your repos via npm install, you can add your them to your depencies in your package.json as specified on the docs:
"dependencies" : {
"project": "git://github.com/user/project.git#commit-ish"
}
And if you want to include GitHub projects, you'll just have to refer to GitHub urls as just "foo": "user/foo-project":
"dependencies": {
"express": "visionmedia/express",
"mocha": "visionmedia/mocha#4727d357ea"
}

Resources