Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8894481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:30:28+00:00 2026-06-14T23:30:28+00:00

PRE: I’ve read NodeJS modules vs classes but this is more specific. As part

  • 0

PRE: I’ve read NodeJS modules vs classes but this is more specific.

As part of some refactoring in Node I have a couple of Application Services (in DDD-terminology) which are technically implemented as Node modules.

Since (in a DDD-world, an probably any other for that matter) Application Services should be singletons and since Node modules are guaranteed to be 1 ‘instance’ only, it seems to me that this is an okay fit (modules trivially implement the ‘singletonness’)

Is there any reason why I should consider refactoring these application services as proper singleton classes (as far as ‘singletonness’ can be guarenteed in javascript anyway), apart from the purist standpoint?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:30:30+00:00Added an answer on June 14, 2026 at 11:30 pm

    Check out Node’s module caching caveats for the cases where the ‘singletoness’ of modules will break down.

    If you always reference your singleton module with file paths (starting with ./, ../, or /) within a single package you’re safe.

    If your service is wrapped up in a package to be used by other modules, you may end up with multiple instances of your singleton.

    Say we publish this sweet service library:

    service-lib/
    ⌞ package.json
    ⌞ service.js
    
    service.js:
      var singleton = {};
      module.exports = singleton;
    

    In this app, server.js and other.js will get different instances of our service:

    app/
    ⌞ server.js
    ⌞ node_modules/
      ⌞ service-lib/
        ⌞ service.js
      ⌞ other-package/
        ⌞ other.js
        ⌞ node_modules/
          ⌞ service-lib/
            ⌞ service.js
    

    While this app will share an instance:

    app/
     ⌞ server.js
     ⌞ node_modules/
        ⌞ service-lib/
           ⌞ service.js
        ⌞ other-package/
           ⌞ other.js
    

    The same npm installing the same app could result in either directory structure depending on the versions of the dependencies. Node’s folders doc has the details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Pre-warning : There are some other questions similar to this but don't quite answer
I have a pre-order traversal function that looks like this: void listInPreOrder(node* hd){ if(hd
I am using some pre-written JavaScript from polldaddy. They have a JavaScript option which,
I have a pre-existing database, and this code: class User private() extends MongoRecord[User] with
I have done some pre processing for dft , and i am trying to
Trying to merge 8 pre-sorted arrays. I'm fairly new to C, but this is
I have used pre-build static libs of OpenSSL 1.0, but it makes my binary
I want to pre-append some text a a CSV file that is created by
I'm doing some pre-processing of an XML document in a ASMX Webservice (Legacy .NET
I have a pre tag within my page that contains quite a bit of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.