Reusing code via custom modules in node.js - node.js

I have 3 projects in a bitbucket repo: projectA, projectB and projectCommon.
Last one, projectCommon should be used in ProjectA and ProjectB and it has been structured as node module which it is not public (not published to npm directory).
How can I use ProjectCommon module in ProjectA and ProjectB?
I've tried using doing npm link but I'm not very convinced in using this in a production environment.
Is there a better way for doing it? Maybe should I remove projectCommon from the repo and add it in a new repo?
¿How the package.json must be configured?

Add it as a dependency, e.g.
"dependencies" : {
"Your_Module": "https://bitbucket.org/:username/:projectname/get/master.tar.gz"
}
After which you hit npm install

Related

NPM Dependency - Folder within a Git Repo?

Does NPM support installing dependencies via a Git URL to a sub-folder within the repo? I checked the documentation and it didn't look like there was a way, but I couldn't explicitly tell.
I know it does and I know how to setup a Git repo itself to serve as a dependency, something like this:
(other files)
package.json
But what I would like to do is basically use a monorepo pattern (like Babel), so I'd have a folder structure similar to this:
packages/
packageA/
package.json
packageB/
package.json
packageC/
package.json
And then be able to install them all from the same repo just referring to packageA, packageB and packageC.
I know an alternative would be to have them all be part of one module, and then import them like package/packageA, but some of them aren't super related conceptually so that is less than ideal. The only reason they are in the same repo because I don't want NPM module repos clustering up our repository.
I know this is normally done with a private NPM repository, but we currently don't have access to one, so hoping there is an alternative.
Any ideas?
It looks like https://gitpkg.now.sh/ is potentially what you want
Looking at the link that it produces it seems that it is acting as a proxy for your package in some way, so I wouldn't use this in mission critical production code but it should be fine if you're trying to test that something works between repos

How to modify an npm package built with TypeScript

I want to try and make some changes to a package published in npm? (I've suggest some changes as an issue but I think they are simple enough for me to attempt them).
https://www.npmjs.com/package/bt-presence#contributing--modifying
The author supplies some information on how to modify the package, but not really enough for someone doing it for the first time.
Where should I clone the GitHub repo to? The folder where the package is installed? I tried it in my home folder and that would not build (unmodified).
The command npm run build - where is this run from? The root folder of the package where the package.json is?
Will I need to modify the package.json?
In general what is the best way to develop something like this for npm? I've worked on packages before but they were simply Javascript.
If you want to work on the bt-presence package in isolation, you can put the cloned repository anywhere. If you want to use your modified version of bt-presence in combination with an application, my recommended approach is to register bt-presence as a dependency in the application's package.json file with the version set to a relative path to your bt-presence repository; then running npm install in the application will make a symlink from node_modules/bt-presence in the application to your bt-presence repository.
npm run build should indeed be run from the root folder that contains the package.json of bt-presence.
If you just want to change the code of bt-presence, you won't need to modify its package.json. You would only modify the package.json if you need to change any of the settings in there, e.g, if you need to add additional dependencies to your version of bt-presence.
None of the above is really specific to TypeScript. (Some JavaScript packages have build processes too if they need to transform or package the JavaScript files in some way.)

How to use a package in Angular2 without publishing it to npm

I have a package which is published to npm and we are successfully using the package in our angular2 applications by defining the dependency in package.json as
"dependencies": {
"somesharedmodule": "^1.0.21",
}
and in our components as import {SharedService} from 'somesharedmodule'.
Now we dont want to make the library public(not interested to make private library too).My question is - i have a module in some folder of my local system.Can i use this module in my application without making the package published in npm. can somebody please guide me the alternatives to use a module in my application without publishing it?
Not clear what exactly you want to do... Bu there are lots of options:
npm link https://docs.npmjs.com/cli/link
use github private repo:
"dependencies": {
"ng2-resource": "https://github.com/{nickName}/{repoName}.git"
}
link your IDE to use local folder as node_modules
manually copy/paste library into project's node_modules

Shipping node.js project with custom dependencies

I fixed a couple of issues in nested dependencies of my node.js project (dependencies are managed by npm). These fixes are pending pull requests and thus aren't published. What's the best way to use them in my project?
I know that I can do npm link inside the fixed version of the library and then npm link library-name inside my project to force npm to use my fixed version. This approach works but installs my library-name globally on my machine which I don't like.
Is it possible to have it locally in main project's repo, force the project to use it and don't do npm link.
You can use a url as the dependency and point it to your own repo (fork).
https://docs.npmjs.com/files/package.json#urls-as-dependencies
for example:
"dependencies": {
"foo": "git+ssh://user#hostname:project.git#commit-ish"
}
If your pull requests are on GitHub its even easier...
As of version 1.1.65, you can refer to GitHub urls as just "foo":
"user/foo-project". Just as with git URLs, a commit-ish suffix can be
included.
https://docs.npmjs.com/files/package.json#github-urls

How can I switch between a linked npm dependency (in development) and an installed dependency (in staging/prod)?

I have a custom npm module that I am working on, and it has a GitHub repo. I'm also working on a project that uses the custom module. When working on the larger project, it is nice to use npm link so I can make changes to the module and see them right away in the main project.
To deploy to staging or production, I use shrinkwrap and shrinkpack so I can do an npm install after every deploy (some of the dependencies need binaries, and dev systems aren't the same as production systems, so they do need to be installed and not just kept in source control). Edit: I'm crossing this out as the answer below technically solves my issue, even though it doesn't solve for this particular point, but that wasn't as important as the rest of it.
Of course, since the module is linked to my main project and not listed in package.json, a deploy and install misses it entirely. I can go ahead and list it in package.json and have it point to the appropriate GitHub repo, but then every time I need to test a change in the main project I would have to commit and push those changes, then update the main project, kill and restart the app...that would get tiresome pretty quickly.
I guess I need something like the opposite of "devDependencies"; something where I can have it not install the module on dev, but do install it from GitHub when doing npm install on staging or production. Other than remembering to manually change package.json every time I need to go back and forth, is there a better way to do this?
you can specify a github repository as your package to install, in your package.json file:
{
dependencies: {
"my-library": "githubusername/my-library"
}
}
this will work in your production environment.
in your development environment, use "npm link".
from within the "my-library" folder, run npm link directly. that will tell npm on your local box that "my-library" is avaialable as a link.
now, in your project that uses "my-library", run npm link my-library. this will create a symlink to your local development version of "my-library", allowing you to change code in that repository and have it work in your other project that needs it.
once you are ready to push to production, push "my-library" to your github repository, and then you can npm install on your servers, like normal.

Resources