# Can I require elastic-apm-node twice?

**URL:** <https://discuss.elastic.co/t/can-i-require-elastic-apm-node-twice/224684>\
**Category:** APM\
**Tags:** nodejs\
**Created:** [March 23, 2020, 2:13pm UTC](https://discuss.elastic.co/t/can-i-require-elastic-apm-node-twice/224684 "2020-03-23T14:13:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ambrozy](https://avatars.discourse-cdn.com/v4/letter/a/8dc957/32.png) [@ambrozy](https://discuss.elastic.co/u/ambrozy)\
**Post date:** [March 23, 2020, 2:13pm UTC](https://discuss.elastic.co/t/can-i-require-elastic-apm-node-twice/224684/1 "2020-03-23T14:13:41Z")

</div>

Hi guys,  
In my git repo I have an internal logger module that is stored as a git submodule, let's call it **logger-module**. It looks like this

MyApp

- submodules
  - **logger-module**

The logger-module/index.js requires and starts elastic-apm-node at the very top.  
The main app index.js requires logger-module at the very top also.  
It all works fine, APM gets all the data captured by the main app.

I also have another app, that also uses logger-module along with other submodules that use it too. It looks like this:

AnotherApp

- submodules
  - **logger-module**
  - another-submodule
    - submodules
      - **logger-module**

As you can see logger-module (with elastic-apm-node inside) will be required twice. Will it break my another-application? Will I get two instances of elastic-apm-node?

---

<div class="post-metadata">

**Author:** ![wa7son](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wa7son/32/21259_2.png) [@wa7son](https://discuss.elastic.co/u/wa7son)\
**Post date:** [March 24, 2020, 2:24pm UTC](https://discuss.elastic.co/t/can-i-require-elastic-apm-node-twice/224684/2 "2020-03-24T14:24:14Z")

</div>

The `elastic-apm-node` package acts as a "singleton" and will use a shared state inside the running process. So you can require it as many times as you like, **as long as you only start it once**.

If you try to start it more than once in the same process, it will throw an error.

However, since all code in the outer scope of a module only runs the first time it's required, you can require your `logger-module` module multiple times as long as you make sure that you require and start the agent in that outer scope. This way the agent will start only the first time the `logger-module` is required.

If you find yourself in a situation where you want to conditionally start the agent based on if it's already started, you can use the [`agent.isStarted()`](https://www.elastic.co/guide/en/apm/agent/nodejs/current/agent-api.html#apm-is-started) function.

---

<div class="post-metadata">

**Author:** ![ambrozy](https://avatars.discourse-cdn.com/v4/letter/a/8dc957/32.png) [@ambrozy](https://discuss.elastic.co/u/ambrozy)\
**Post date:** [March 24, 2020, 5:28pm UTC](https://discuss.elastic.co/t/can-i-require-elastic-apm-node-twice/224684/3 "2020-03-24T17:28:22Z")

</div>

Hi Thomas,  
thanks for the answer. Unfortunately in my case the agent is started twice and two agents are running. No error is thrown when the 2nd instance is being started.  
It's because each submodule has its own node\_modules directory, and therefore I have agents from:

- another-app / submodules / logger-module / node\_modules / elastic-apm-node
- another-app / submodules / another-submodule / submodules / logger-module / node\_modules / elastic-apm-node

I guess from the node point of view these are two different packages.

Anyway I'm setting process.env.ELASTIC\_APM\_ACTIVE to false after staring 1st instance, to prevent the 2nd one from being started also.

---

<div class="post-metadata">

**Author:** ![wa7son](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wa7son/32/21259_2.png) [@wa7son](https://discuss.elastic.co/u/wa7son)\
**Post date:** [March 25, 2020, 1:36pm UTC](https://discuss.elastic.co/t/can-i-require-elastic-apm-node-twice/224684/4 "2020-03-25T13:36:05Z")

</div>

Ah ok, having the agent installed in two different locations on the filesystem for the same app is unfortunately a way to side-step the "singleton" behaviour and isn't officially supported. It might work without any errors in your case, but it's not something I would recommend as it - amongst other things - adds extra overhead to the application. Your approach of using `ELASTIC_APM_ACTIVE` to control this behaviour sounds like a good hack and should work as expected. But it should probably be something we looked into handling better.

---

<div class="post-metadata">

**Author:** ![ambrozy](https://avatars.discourse-cdn.com/v4/letter/a/8dc957/32.png) [@ambrozy](https://discuss.elastic.co/u/ambrozy)\
**Post date:** [March 26, 2020, 9:47am UTC](https://discuss.elastic.co/t/can-i-require-elastic-apm-node-twice/224684/5 "2020-03-26T09:47:39Z")

</div>

Ok, got it.

BTW I love the tool! 👏

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 16, 2020, 5:47am UTC](https://discuss.elastic.co/t/can-i-require-elastic-apm-node-twice/224684/6 "2020-04-16T05:47:46Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
