0

I am a seasoned C# developer. I have been teaching myself nodejs etc. I am trying to find some information on code reuse. However, I have been searching and not finding information about reusing code that I am looking for... Can someone point me to the right place to look?

In c# I can write a lib that can then be shared and reused to build other apps. All the docs I found talks about publishing code to a git repository, then use npm to pull it down. Is the only other way copy/paste code files? I am sure there are companies out there that have proprietary code, how are they solving this? Are they just not using nodejs?

Thank you for your time

UPDATE/EDIT: I tried adding file path inside of package.json like so:

{
  "name": "starter",
  "version": "1.0.0",
  "dependencies": {
    "@angular/common": "^2.4.6",
    "@angular/core": "^2.4.6",
    "bower": "^1.8.0",
    "rxjs": "^5.1.0",
    "typescript": "^2.1.5",
    "zone.js": "^0.7.6",
    "SimpleTest" : "file:///C:\\me\\Code\\nodecommon",

got this to work. the trick is I have to make the common code a node package. This will copy or update my folders.

user691856
  • 167
  • 1
  • 1
  • 11
  • Use node modules. Put shared code in a module and then you can use it in lots of projects. A module can be shared in many different ways from just sharing via a common directory hierarchy to sharing via a local NPM repository to sharing via a public NPM repository and many other ways. The key is to design your shared code into node modules. – jfriend00 Feb 07 '17 at 20:47
  • 2
    See NPM private modules: https://docs.npmjs.com/private-modules/intro – jfriend00 Feb 07 '17 at 20:47
  • 1
    More info on private modules here: [can you host a private repository for your organization to use with npm?](http://stackoverflow.com/questions/7575627/can-you-host-a-private-repository-for-your-organization-to-use-with-npm), – jfriend00 Feb 07 '17 at 20:49
  • "All the docs I found talks about publishing code to a git repository, then use npm to pull it down." Yes, but this is in no way incompatible with keeping proprietary code private. Git repositories hosted privately (e.g. on a company's internal network, or on the public internet but secured by SSH credentials) are private. – Jordan Running Feb 07 '17 at 20:59
  • @jfriend00 how would deployment work when my module exists in ../../Common? Would I need to create the same structure at deployment location? Is there some kind of mechanism to consolidate and organize all the local dependencies? – user691856 Feb 07 '17 at 22:06
  • @Jordan It seems to be weird that I have to mess with repositories just to get this part working... What if I am working offline and made a change to my lib? I would then have to find a way to get connection and publish before I can update my node_module in order to continue working? – user691856 Feb 07 '17 at 22:09
  • 1
    Study NPM. If you use a private repository, then deploying just means pulling the main app module from NPM and then if that module is set up appropriately, it will get all the dependencies and put them in an appropriate place. This is the whole idea behind NPM. It manages hierarchies of dependencies so you can just pull the master module you want and it will pull all the specified dependent modules to your local location for you. You can, of course, invent your own scheme and just copy directory hierarchies around or package things in zip/jar and decompress to a local location – jfriend00 Feb 07 '17 at 22:10
  • 1
    The npm docs are really excellent. Type `npm help json` (or visit [this page](https://docs.npmjs.com/files/package.json#dependencies)) to read about all of the different ways you can access packages installed locally or remotely. You'll find that it's very easy to tell npm to use your local copy of a package instead of the remote one (spoiler: [`npm link`](https://docs.npmjs.com/cli/link)). – Jordan Running Feb 07 '17 at 22:12
  • There are other package managers one can use with node.js besides NPM if it doesn't meet your needs too. bower and yarn are a couple others. – jfriend00 Feb 07 '17 at 22:14

2 Answers2

1

This answer is collectively compiled by all the comments to this post. I should not get credit for this. Thanks to @jfriend00 and @Jordan for the comments. Hope this helps someone else in the future.

  1. hosted NPM private module: I like everything about this except that I can't really work off-line if I needed to change one of my common modules. Also, it would cost me monthly subscription unless I make it open source.
  2. NPM seems to supports file path configuration in package.json. You can specify ../../some_package as your configuration. I will need to create a module myself to try this out and see how it works. But if this will auto-update and copies latest into my current project then this is what I am looking for. Ultimately, I can probably point this config to my private module location in bullet 1 and work offline.
  3. NPM link. I read little bit about it. But will need to study it more to get a better understanding of how this works. But from what I read so far, it has some potential.

EDIT: Got this to work for bullet 2. Thanks for all the help from all comments.

user691856
  • 167
  • 1
  • 1
  • 11
  • 1
    4. Any git repository, public or private (assuming it has a `package.json`). 5. Your own private npm registry: https://docs.npmjs.com/misc/registry#can-i-run-my-own-private-registry – Jordan Running Feb 08 '17 at 02:47
0

Modules is the mechanism of code reuse in node js, red more here.

There are npm and yarn package managers to help you manage your code dependencies. Although nothing stops you from manually copying your code into the project and then requiring it in your code.

The only advantage of keeping your modules in 'node_modules' folder is that you can require them by just package name without a full path. Node will look into local node_modules then into global ones and then fail if package is not found.

Dmitry Matveev
  • 5,320
  • 1
  • 32
  • 43