How can I automatically link local npm package? - node.js

I'm buidling two private npm packages that depends on each other.
Say that I have :
project
/my-commons
package.json :
{
name : "my-commons",
version : "0.0.1"
...
}
/my-server
package.json :
{
dependencies : {
"my-commons" : "0.0.1"
}
}
I can use 'npm link' to install the 'commons' package. So anyone willing to
start working on server has to do :
checkout project
cd my-server
npm link ../my-commons
npm install
And a symlink to ../my-commons is added in /my-server/node_modules, and everything's fine.
Is there however a way to tell npm that 'my-commons' package will always be in that folder out there, so that you could just do :
checkout project
cd my-server
npm install
Or am I missing something obvious here ?
Thanks

Maybe.
But first: If my-commons is needed by my-server then it is most likely a good idea to keep it in my-server/node_modules even if that is redundant. In most cases it's best keep a module's dependencies isolated from the rest of your application.
In that scenario, npm link can be used during development, when you're working on my-commons and want to use the changes in my-server without having to npm publish my-commons.
In production you will not want to use npm link, because dependent modules will lose control over which version of the linked module they end up with. If my-server depends on my-commons 0.1.0, but you npm linked your 1.0.1-pre-release version of the my-commons module all hell might break loose.
However, since version 1.2.10 (shipping with node 0.8.19) NPM supports peer dependencies.
Peer dependencies allow you to specify that my-server requires that my-commons be installed "besides" my-server. This does not enable you to require("my-commons") inside my-server but could be useful if my-server is a plugin for my-commons.
For more information: http://blog.nodejs.org/2013/02/07/peer-dependencies/
And finally, since you said that you are developing private packages: If installing the packages is what is causing trouble for you, because you can't publish your packages to the public NPM registry, have a look at alternative ways to specify dependencies (git-, http-URLs, TGZ files): https://docs.npmjs.com/files/package.json#dependencies

Related

How to get npm to favor local linked dependency over its published install

I've searched through other questions such as this one, but they all seem to be about a local npm link stopping working for another reason than mine. I assume this is a common use-case issue, so if I'm doing something methodically wrong, I'm more than happy to take suggestions on how I should be doing it.
Principally, I have a private npm module that I'm working on called #organisation/module. When working locally, I'll run npm link on it, and use it within my 'host' project as npm link #organisation/module — this all works great with hot-reloading, etc. I'll also import it as import module from '#organisation/module.
However, since I also want to publish my local changes to npm (as #organisation/module) from time to time, for build testing and production code, I need to run npm install #organisation/module on the host project.
This then seems to break the implicit npm link I set up earlier... I assume mainly because they are the same name, and npm favors an install over a link?
When I want to make live, local changes again, the only way I can currently get it to work is via npm uninstall #organisation/module and then to re-link it.
Is there a way to keep the published module installed (in order to avoid careless mistakes, like forgetting to reinstall it for build testing), but always favour the local, linked instance?
Diagram for ref:
Have you tried locally installing with the other method npm provides.
npm install /absolute/path/packageName
I believe this will change your entry in package.json to look like this:
"dependencies" {
...
"packageName": "file:../../path/to/packageName",
...
}
Since npm link creates a symlink in the global folder, while npm install is local to the project npm install takes precedence. You can read about npm link here: https://docs.npmjs.com/cli/link
To avoid this, my suggestion would be to use npm install <path to local> and when you need to use the production code use npm install #organization/module. This would update your node_modules per code basis. Read about npm install here: https://docs.npmjs.com/cli/install
Hope this helps :)
Go to the directory where your local package is located open package.json change the name from original_name to "original_name_local".
write npm link on terminal at the same location.
After this go to your working directory and write npm install <path to local>
Now whereever you're requiring or importing update the name to "original_name_local"
for example if it's require('space-cleaner') then change it to require('space-cleaner_local')
Like this you can have both local as well as production package just change the name wherever required.
Otherwise you can remove package by removing it from package.json and deleting from node_modules.
if local is needed go to local package directory and on terminal write npm link and then on your working directory write npm install ./path/to/package
if production then again delete the package as told above and write npm install package_name

How to npm prune and avoid the removal of a file: dependency?

I have a local npm package (a node.js/electron add-on) that we install via the file.
npm i --save my_package-1.0.0-beta.1.tgz
The package.json file lands up looking like this;
{
...
"dependencies": {
"my_package": "file:my_package-1.0.0-beta.1.tgz",
...
},
}
The module is used in the code, extensively.
When I run npm prune, the package is removed from node_modules. How can I configure or use npm prune to not remove this package?
Some notes from the comments;
Ultimately this is part of a workflow from dev to "test" builds for local device deployments and testing. On the build server, this issue doesn't really pop up since we only install for production and package from there (i.e. with no prune).
This dependency is part of the production dependencies, the solution must maintain that fact.
During a recent update to the environment, this was retested. It was found that npm now (as of npm#4.2.0 installed with node v 7.10.0 and npm#4.6.1, "latest") does seem to preserve the file: dependency. In addition the --production flag works as expected as well.
It is not known exactly when this begin to work or was supported, but it may be circa npm#4.1.0; in particular the rewrite to use the updated prune plumbing.
To update npm to the latest version;
npm install npm#latest -g

How to specify local modules as npm package dependencies

I have an application which has the usual set of dependencies on third party modules (e.g. 'express') specified in the package.json file under dependencies. E.g.
"express" : "3.1.1"
I would like to structure my own code modularly and have a set of local (meaning on the file system I am currently in) modules be installed by the package.json. I know that I can install a local module by running:
npm install path/to/mymodule
However, I don't know how to make this happen via the package.json dependencies structure. Using the --save option in this command is simply putting "mymodule": "0.0.0" into my package.json (doesn't reference the filepath location). If i then remove the installed version from node_modules, and try to re-install from the package.json, it fails (because it looks for "mymodule" in the central registry, and doesn't look locally).
I'm sure the is a way of telling the "dependencies": {} structure that I want it to be installed from a file system path, but don't know how.
Anyone else had this problem?
Thanks.
npm install now supports this
npm install --save ../path/to/mymodule
For this to work mymodule must be configured as a module with its own package.json. See Creating NodeJS modules.
As of npm 2.0, local dependencies are supported natively. See danilopopeye's answer to a similar question. I've copied his response here as this question ranks very high in web search results.
This feature was implemented in the version 2.0.0 of npm. For example:
{
"name": "baz",
"dependencies": {
"bar": "file:../foo/bar"
}
}
Any of the following paths are also valid:
../foo/bar
~/foo/bar
./foo/bar
/foo/bar
syncing updates
Since npm install <folder> adds the package in the directory as a symlink in the current project any changes to the local package are automatically synced.
See: Local dependency in package.json
It looks like the answer is npm link: https://docs.npmjs.com/cli/link
I couldn't find a neat way in the end so I went for create a directory called local_modules and then added this bashscript to the package.json in scripts->preinstall
#!/bin/sh
for i in $(find ./local_modules -type d -maxdepth 1) ; do
packageJson="${i}/package.json"
if [ -f "${packageJson}" ]; then
echo "installing ${i}..."
npm install "${i}"
fi
done
After struggling much with the npm link command (suggested solution for developing local modules without publishing them to a registry or maintaining a separate copy in the node_modules folder), I built a small npm module to help with this issue.
The fix requires two easy steps.
First:
npm install lib-manager --save-dev
Second, add this to your package.json:
{
"name": "yourModuleName",
// ...
"scripts": {
"postinstall": "./node_modules/.bin/local-link"
}
}
More details at https://www.npmjs.com/package/lib-manager. Hope it helps someone.
You can just add to your package.json file in your project
"package-name" : "path/to/package"
and then run npm i in your project
At work we have a common library that is used by a few different projects all in a single repository. Originally we used the published (private) version (npm install --save rp-utils) but that lead to a lot of needless version updates as we developed. The library lives in a sister directory to the applications and we are able to use a relative path instead of a version. Instead of "rp-utils": "^1.3.34" in package.json it now is:
{
"dependencies": { ...
"rp-utils": "../rp-utils",
...
the rp-utils directory contains a publishable npm package
use install-local
I had issues with conflicting react installations from the local dependency.
I solved the error by using install-local npm package. This package does not create symlinks, which solved my issue.
Steps:
run npm i -g install-local
run npx install-local --save <local-path> inside the target repository to install the local dependency
Further reading: https://www.npmjs.com/package/install-local
The error I received, when trying to install the local package with npm install --save <local-directory>:
Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
You might have mismatching versions of React and the renderer (such as React DOM)
You might be breaking the Rules of Hooks
You might have more than one copy of React in the same app
If it's acceptible to simply publish your modules preinstalled in node_modules alongside your other files, you can do it like this:
// ./node_modules/foo/package.json
{
"name":"foo",
"version":"0.0.1",
"main":"index.js"
}
// ./package.json
...
"dependencies": {
"foo":"0.0.1",
"bar":"*"
}
// ./app.js
var foo = require('foo');
You may also want to store your module on git and tell your parent package.json to install the dependency from git: https://npmjs.org/doc/json.html#Git-URLs-as-Dependencies

configure custom module for Node.js project

I create my Node project (npm init). In this project, i want to use Node REST Client from git hub https://github.com/dchester/node-rest-client.
update package.json with dependency:
...
"dependencies": {
"rest-client": "*"
},
...
create dir node_modules in my project
Copy the rest-client into the directory 'node_modules'
Run 'npm install' to install this dependency, however it doesn't.
What am i doing wrong? how to properly install dependency that is not from npm central repo?
Thank You
It looks like you'll have to do it manually for every module. However as the accepted answer in How to install a private NPM module without my own registry? by Mihai points out, npm knows about git:
npm install git://github.com/visionmedia/express.git
furthermore, besides going into the directory and typing npm install there it is possible to specify the path as argument:
npm install path/to/somedir
There's another useful answer in How to install a private NPM module without my own registry? where you can see you can also specify a url pointing to a tarball. I suggest you take a look at that thread.

How to install a private NPM module without my own registry?

I've taken some shared code and put it in an NPM module, one I don't want to upload to the central registry. The question is, how do I install it from other projects?
The obvious way is probably to set up my own NPM registry, but according to the documentation, that involves a lot of hassle.
Can I just install an NPM module that sits on the local filesystem, or perhaps even from git?
npm install --from-git git#server:project
In your private npm modules add
"private": true
to your package.json
Then to reference the private module in another module, use this in your package.json
{
"name": "myapp",
"dependencies": {
"private-repo": "git+ssh://git#github.com:myaccount/myprivate.git#v1.0.0",
}
}
cd somedir
npm install .
or
npm install path/to/somedir
somedir must contain the package.json inside it.
It knows about git too:
npm install git://github.com/visionmedia/express.git
Can I just install an NPM package that sits on the local filesystem, or perhaps even from git?
Yes you can! From the docs https://docs.npmjs.com/cli/install
A package is:
a) a folder containing a program described by a package.json file
b) a gzipped tarball containing (a)
c) a url that resolves to (b)
d) a <name>#<version> that is published on the registry with (c)
e) a <name>#<tag> that points to (d)
f) a <name> that has a "latest" tag satisfying (e)
g) a <git remote url> that resolves to (b)
Isn't npm brilliant?
Update January 2016
In addition to other answers, there is sometimes the scenario where you wish to have private modules available in a team context.
Both Github and Bitbucket support the concept of generating a team API Key. This API key can be used as the password to perform API requests as this team.
In your private npm modules add
"private": true
to your package.json
Then to reference the private module in another module, use this in your package.json
{
"name": "myapp",
"dependencies": {
"private-repo":
"git+https://myteamname:aQqtcplwFzlumj0mIDdRGCbsAq5d6Xg4#bitbucket.org/myprivate.git",
}
}
where team name = myteamname, and API Key = aQqtcplwFzlumj0mIDdRGCbsAq5d6Xg4
Here I reference a bitbucket repo, but it is almost identical using github too.
Finally, as an alternative, if you really don't mind paying $7 per month (as of writing) then you can now have private NPM modules out of the box.
FWIW: I had problems with all of these answers when dealing with a private organization repository.
The following worked for me:
npm install -S "git+https://username#github.com/orgname/repositoryname.git"
For example:
npm install -S "git+https://blesh#github.com/netflix/private-repository.git"
I'm not entirely sure why the other answers didn't work for me in this one case, because they're what I tried first before I hit Google and found this answer. And the other answers are what I've done in the past.
Hopefully this helps someone else.
Structure your code in an accessible fashion like below. If this is possible for you.
NodeProjs\Apps\MainApp\package.json
NodeProjs\Modules\DataModule\package.json
Within MainApp # NodProjs\Apps\MainApp\
npm install --S ../../Modules/DataModule
You may need to update package.json as:
"dependencies": {
"datamodule": "../../Modules/DataModule"
}
This worked for my situation.
I had this same problem, and after some searching around, I found Reggie (https://github.com/mbrevoort/node-reggie). It looks pretty solid. It allows for lightweight publishing of NPM modules to private servers. Not perfect (no authentication upon installation), and it's still really young, but I tested it locally, and it seems to do what it says it should do.
That is... (and this just from their docs)
npm install -g reggie
reggie-server -d ~/.reggie
then cd into your module directory and...
reggie -u http://<host:port> publish
reggie -u http://127.0.0.1:8080 publish
finally, you can install packages from reggie just by using that url either in a direct npm install command, or from within a package.json... like so
npm install http://<host:port>/package/<name>/<version>
npm install http://<host:port>/package/foo/1.0.0
or..
dependencies: {
"foo": "http://<host:port>/package/foo/1.0.0"
}
Npm now provides unlimited private hosted modules for $7/user/month used like so
cd private-project
npm login
in your package json set "name": " #username/private-project"
npm publish
then to require your project:
cd ../new-project
npm install --save #username/private-project
This was what I was looking for - get the latest from "private repo" :
GitHub :
$ npm install git+https://token:x-oauth-basic#github.com/username/my-new-project.git
$ npm install git+ssh://git#github.com/username/my-new-project.git
Bitbucket :
$ npm install git+https://username:password#bitbucket.org/username/my-new-project.git
$ npm install git+ssh://git#bitbucket.org/username/my-new-project.git
Starting with arcseldon's answer, I found that the team name was needed in the URL like so:
npm install --save "git+https://myteamname#aQqtcplwFzlumj0mIDdRGCbsAq5d6Xg4#bitbucket.org/myteamname/myprivate.git"
And note that the API key is only available for the team, not individual users.
I use the following with a private github repository:
npm install github:mygithubuser/myproject
Very simple -
npm config set registry https://path-to-your-registry/
It actually sets registry = "https://path-to-your-registry" this line to /Users/<ur-machine-user-name>/.npmrc
All the value you have set explicitly or have been set by default can be seen by - npm config list
You can use Verdaccio for this purpose which is a lightweight private npm proxy registry built in Node.js. Also it is free and open-source. By using Verdaccio it does not involve that much hassle as a plain private npm registry would.
You can find detailed information about how to install and run it on their website but here are the steps:
It requires node >=8.x.
// Install it from npm globally
npm install -g verdaccio
// Simply run with the default configuration that will host the registry which you can reach at http://localhost:4873/
verdaccio
// Set the registry for your project and every package will be downloaded from your private registry
npm set registry http://localhost:4873/
// OR use the registry upon individual package install
npm install --registry http://localhost:4873
It also has a docker so you can easily publish it to your publicly available docker and voila you have a private npm repository that can be distributed to others in a way as you configure it!
Config to install from public Github repository, even if machine is under firewall:
dependencies: {
"foo": "https://github.com/package/foo/tarball/master"
}
Obviously, setting up the private npm registry is the most scalable and long-term solution, although it's a bit of hassle in the beginning.
Also, you can install using the git+https/ssh as mentioned in the other answers. But if you have the private repo, and you're building the image in the cloud, let's say using google cloud build, you have to set up the GitHub ssh connection.
The simplest solution for one-off case like this can be solved using the following approach.
Clone and modify or create your own library from scratch.
Generate the archive file(package code along with its dependencies), using
yarn install && yarn pack
this will produce file like
rich-markdown-editor-v11.13.117.tgz
move this file to libs folder and add this entry in dependencies object of package.json.
"rich-markdown-editor": "file:libs/rich-markdown-editor-v11.13.117.tgz",
Now, install the package.
yarn install
Make sure to add that file in your vcs and the installation process in docker image creation should work in cloud as well.
Note: if you frequently update the package and commit in your vcs, it will increase your repo size(while cloning with full history).
Publish your module under an organization name using the standard "#my-org/my-module" (by default all organization modules are private).
From your npm profile create a read-only access token under "Access Tokens"
Next in your project directory root create a .npmrc file, and inside the file write the following:
//registry.npmjs.org/:_authToken=${Your_Access_Token}
Note: this also should work for others packaging services that follow the same standard.

Resources