Replace an npm package with an alternative - npm

Following up on Substitute an npm package with own implementation, which is about six years ago,
is there any simpler alternative now, with npm (not yarn, and not "transitive dependency")?
Basically, the same as NPM replace package with other, I found that I need package XXX, however, that package XXX has been out of maintenance for a year now, and I've found an updated git repo (but with the same name of package XXX).
Is there any easy way for npm to grab from the alternative git repo instead, or any simpler workarounds? (Not to start a language war but Go now has)
That updated git repo owner must have a simple way to make use of his own package without publishing to npm, so what's the trick?

npm install has a built in support to install package from github, gitlab, bitbucket, gist, and other special formats.
but you can install it from any git repository using the following
npm install <git repo url>
for more information, see npm install documentation

Related

Is there a way to install an npm package locally but not affect package.json or package-lock.json?

I have a project that I'm working on for a client where I have two private packages (which I can't get access to npm install) are inside the package.json.
I do however have access to clone the repos for those said packages. If I simply run an npm install I'll get a permission denied error. Same if I run npm link to the packages.
I've been working around this by removing the packages from the package.json then running npm install ../some-package. This works but isn't a great solution because if I wanted to add a new package I'd have to deal with a bit of a mess with the package.json.
Is there a better way than this?
I have tried running npm link ../some-package but I still get access denied. The only way I've managed to complete an install is by removing the packages then installing them from a local dir.
I don't know the details of your situation, but I see at least two potential solutions to explore.
Option 1: Install the package from the repo
I do however have access to clone the repos for those said packages.
You can install from a git repo and package.json will record that git repo as the source of the package rather than the npm registry.
From the docs at https://docs.npmjs.com/cli/v8/commands/npm-install:
npm install :
Installs the package from the hosted git provider, cloning it with git. For a full git remote url, only that URL will be attempted.
Option 2: Install from the local file system with --no-save
If that approach doesn't work for you, you can try npm install --no-save ../some-package as a build step. The --no-save makes it so it doesn't modify package.json.

NPM dependency from Gitlab directly

I have a situation where there was an update in one of npm packages I use in my project, but author didn't publish it on npmjs registry, so up to date code sits in gitlab only.
What would be the best solution to get updated version of code? I believe there is a way to add dependency to project which will be downloaded from gitlab or github public repository directly? Is it possible to compile it like in npmjs as well?
Yes, you could install a dependency from a git repository directly. As can be seen in the npm docs. You can straight install a Git Remote repository like this:
npm install <git remote url>
e.g.
npm install git://github.com/npm/cli.git
But beware that installing directly from the source git might have unintended side effects (missing build files, additional documentation files in general changes to the npmjs Version).
Also installing from the repository I would recommend you install from a specific commit/Tag.

NPM install part from github monorepo

I'm trying to install typescript plugin from rollup plugins monorepo (actually a branch with a PR from this repo).
I guess the following command tries to install the whole repo:
npm install git+https://github.com/rollup/plugins.git.
It also fails at their disallow-npm.js, I guess a guard for this kind of thing.
How can I pull and install only one plugin from this monorepo?
relating to your problem I found an article that talks about installation of NPM modules from Github repos:
How to install NPM modules from Github Repos
Being more accurate to your question, that repo has a Readme on each package that you can use to install the modules from that Github repo.
npm install #rollup/plugin-alias
The code about should be valid to install the module "plugin-alias" from that "rollup" repo.
I hope this helps to solve your problem, regards.

Does npm or yarn clone from VCS and run build script when install a package?

I am studying about npm and I have some questions.
Where the npm get the package from? i.e. when run npm install <package-name> or yarn add <package-name>.
When get the package, do npm get the package as raw or get then build it(like run the build script written in package.json)?
When publish the package, the repository field of package.json is required?
Can be different between the repository for publishing and the repository in pacakge.json?
To answer your questions:
npm gets them from the NPM package registry, and so does yarn, but Yarn probably has a proxy registry in front of it. In general, you can say, both tools fetch their packages from https://npmjs.com by default.
It gets the package as it was published (so, in short, the answer is "raw"). Building is up to the publisher and depends on the type of package. Often, some prepublish task builds something into dist/ (or any other location in the package), and these files are also shipped with the package others then download. Building rarely happens after installing a package (exception here are library-wrapping packages built with node-gyp).
The repository field is not required, to my knowledge, but it is good practise to include it (it will be displayed on the NPM website, for example).
Technically, yes. You can just specify any repository in repository, but it wouldn't make much sense to specify one that isn't the source of the package.
If you in general want to read up more on how npm works, check out it's documentation over at https://docs.npmjs.com/

Using NPM package which requires prepublish/build from Git

Currently NPM does not run prepublish after installing a package from a Git repository.
When working with a team (i.e: I cannot ask team members to run npm install specifically on a dependent package), how can I use a package that requires a build step when installing from a Git repo?
You can try this: https://www.npmjs.com/package/npm-git-install
It requires nodegit, so you'll have to be able to install that as well on your system.