Wednesday, November 26, 2014

npm link with interdependent modules

Sometimes you need to develop on multiple interdependent node modules at the same time. When developing on a dependent module, in order to test your changes, you can do one of several things:
  1. Publish your module and run npm update
  2. Copy and paste code into your node_modules folder
  3. Use npm link to use the in progress version of your module in your other applications
Out of any of the above options only 3 really scales and doesn't leave much potential for errors. Before going any further let's review what npm link does.

Let's say you have 2 modules:
  • dependent-module
  • master-module
master-module also has a dependency on dependent-module. Running npm link inside the dependent-module directory will create a symlink from that directory to the global node_modules/dependent-module directory. Then when running npm link dependent-module in the master-module directory will add dependent-module as a symlink inside the local node_modules directory. This allows you to make changes to dependent-module and instantly have them available inside master-module.

This works great until you have a module that is used in both of your modules that also has global state. A good example is mongoose, the excellent mongodb odm. Schemas and models are defined on the global mongoose object. If each of your modules has mongoose installed, they will have 2 different mongoose objects. So if your models are defined in one module, those models won't be available in your other module. Normally this behavior doesn't happen because npm is smart enough not to install module dependencies if they have already been installed, but when using npm link this won't be the case.

In order to avoid this problem, you will need to run npm link on any susceptible modules. So in our above example if both modules need mongoose, you will need to run npm link mongoose in each affected module directory.

Something to also keep in mind is that running npm update will destroy the symlinks, meaning you need to run npm link dependent-module and npm link mongoose after your run npm update.

50 comments:

  1. Really a great post. Appreciate the effort in educating us. We are also same service provides in Bangalore.
    Web Design Company in Bangalore
    Website Designers in Bangalore
    Website Development Company in Bangalore

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. 더 재미 있고 흥미로운 기사가 ​​있기를 바랍니다. 대단히 감사합니다.

    chó Bull Pháp

    bán chó bull pháp

    chó bull pháp giá bao nhiêu

    mua chó bull pháp

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Bu ən yaxşı məqalələrdən biridir. Paylaşımınız üçün təşəkkür edirik. Sizə uğurlar və uğurlar arzulayıram!


    giảo cổ lam 5 lá

    giảo cổ lam 7 lá

    giảo cổ lam khô

    giảo cổ lam 9 lá

    ReplyDelete
  6. Eskerrik asko partekatutako administratzaileari. Nahi duzun artikulu interesgarri eta interesgarriak izatea.


    lều xông hơi

    lều xông hơi giá rẻ

    lều xông hơi sau sinh

    lều xông hơi loại nào tốt

    ReplyDelete
  7. Vanskeligheter( van bi ) vil passere. På samme måte som( van điện từ ) regnet utenfor( van giảm áp ) vinduet, hvor nostalgisk( van xả khí ) er det som til slutt( van cửa ) vil fjerne( van công nghiệp ) himmelen.

    ReplyDelete
  8. Your blog is filled with unique good articles! I was impressed how well you express your thoughts and sharing your experiences.I never regretted any moment that came to your blog! :)
    paperhelp review

    ReplyDelete
  9. Nội Thất Trẻ Em Bảo An Kids là doanh nghiệp chuyên thiết kế và thi công các sản phẩm nội thất trẻ em bao gồm: Phòng ngủ trẻ em, Giường tầng, bàn học đẹp, kệ sách, bàn học hiện đại, mục đích mua giường tầng hiện đại dành cho bé

    ReplyDelete
  10. The GT44 boys bunk bed is designed for homes with small space. This bed model is made from imported MDF imported from Thailand, high-class Korean paint. With this material, parents can be assured of long-term use because it does not affect the health of the baby. Refer to many other bunk beds at Bao An Kids: Mẫu giường tầng bé trai GT44

    ReplyDelete
  11. Nguồn máy tính là một trong những thiết bị không thể thiếu của một bộ máy tính để bàn trọn bộ. Nguồn máy tính được coi là thiết bị quan trọng của một chiếc máy tính để bàn. Tuy nhiên, đây là linh kiện dễ bị lỗi nhất trong quá trình sử dụng. Thông tin của bài viết dưới đây chúng tôi sẽ tổng hợp những lỗi thường gặp của một bộ nguồn máy tính cũ. Mời bạn cùng tham khảo để có thông tin cho mình nhé
    Những Lỗi Thường Gặp Của Nguồn Máy Tính Cũ

    ReplyDelete
  12. Dale Hollow News Welcome to our Pitbull breeder home were we have been proudly breeding and showing quality AKC registered Pitbull puppy since 2004. Our main focus is to produce show quality pitbull puppies with amazing personalities suitable for their forever family. We treat every Puppy with individualized care based on their needs to give them the best opportunities in life. Click "Available Pitbull puppies" to check out our puppies and dogs looking for new homes.

    ReplyDelete
  13. Cemboard cement sheet is no longer a strange name for users anymore. Because Cemboard is the first choice of contractors and architects in the construction of large and small projects in Vietnam. Today's article let us find out detailed information What is Cemboard? What are the advantages, application, price and quality of the product? But many customers believe so. More: Tấm XPS cách nhiệt , tấm chống nóng, cemboard

    ReplyDelete