# Trying to get 'require' to work but no result

**URL:** <https://discourse.nodered.org/t/trying-to-get-require-to-work-but-no-result/71819>\
**Category:** General\
**Created:** [7 December 2022 13:07 UTC](https://discourse.nodered.org/t/trying-to-get-require-to-work-but-no-result/71819 "2022-12-07T13:07:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)\
**Post date:** [7 December 2022 13:07 UTC](https://discourse.nodered.org/t/trying-to-get-require-to-work-but-no-result/71819/1 "2022-12-07T13:07:40Z")

</div>

I am trying to get a third-party package to work. I first tried the example 'os', and I got it functional.

Now I am trying to get the package `'node-dsmr'` to function in a similar way.  
I installed the package in ~/.node-red as it says in the manual, with command `npm install node-dsmr`.

Then I changed te settings.js

 ![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/b/4b504b82f1c7eee5bc8e8bb9342ee337849509a5.png)

As I only need the parser, I should point to the parser.js file according to this site:

> **[node-dsmr](https://www.npmjs.com/package/node-dsmr/v/0.1.1)**
>
> Parser for the DSMR protocol used on the P1 port of smart meters. Latest version: 1.0.2, last published: a year ago. Start using node-dsmr in your project by running \`npm i node-dsmr\`. There are no other projects in the npm registry using node-dsmr.

After restart node-red, I made a function node that has the added package defined,

 ![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/7/c7a65958bddb32920ecfb6104e76f8d82d0c4910.png)

But when I started to run the function node I get errors,

![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/8/48a9098f5aa7da42edeb32b06658572806f491bf.png)

I checked the folder and the file is really there.

 ![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/c/2c3a394fad50f39fd05f3a7fa938530e220d2833.png)

Can anyone tell me what I did wrong?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [7 December 2022 13:19 UTC](https://discourse.nodered.org/t/trying-to-get-require-to-work-but-no-result/71819/2 "2022-12-07T13:19:07Z")

</div>

You don't need it in both places, only one or the other.

If you require it in settings.js, you have to use `const xxxx = global.get('xxxx')` in your function code.

If you apply it to the function node, you don't need to `get` it. It will just be available.

---

<div class="post-metadata">

**Author:** ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)\
**Post date:** [7 December 2022 13:20 UTC](https://discourse.nodered.org/t/trying-to-get-require-to-work-but-no-result/71819/3 "2022-12-07T13:20:31Z")

</div>

@TotallyInformation , sorry I don't know what you mean by both places.....

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [7 December 2022 13:21 UTC](https://discourse.nodered.org/t/trying-to-get-require-to-work-but-no-result/71819/4 "2022-12-07T13:21:02Z")

</div>

You've put it in settings.js and in the startup of your function node.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [7 December 2022 13:22 UTC](https://discourse.nodered.org/t/trying-to-get-require-to-work-but-no-result/71819/5 "2022-12-07T13:22:03Z")

</div>

Hi @henkkas

You are mixing up the two different ways it is possible to add additional modules to the Function node.

**Option 1:**  
edit your settings file as you have done and use `const dsmrParser = global.get("dsmrParser")`

**Option 2:**  
Do not edit settings.js. Add the module to the 'Setup' tab of the Function node. If you do that, you have to provide the npm module name - `node-dsmr` - the runtime will then ensure it is installed and loaded. I'm not 100% sure if it'll work doing `node-dsmr/lib/parser`.. you may have to require `node-dsmr` and then 'explore' the object that returns from find the parser bit of it.

---

<div class="post-metadata">

**Author:** ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)\
**Post date:** [7 December 2022 14:46 UTC](https://discourse.nodered.org/t/trying-to-get-require-to-work-but-no-result/71819/6 "2022-12-07T14:46:33Z")

</div>

@knolleary , Thank you. Option 1 is working for me, but option 2 I can't get to work.

As I like option 1 better, I settle for that.

The package seems to be a 'function' object.  
The 'os' package was a 'data' object. Maybe that's the reason ???

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [5 February 2023 14:47 UTC](https://discourse.nodered.org/t/trying-to-get-require-to-work-but-no-result/71819/7 "2023-02-05T14:47:17Z")

</div>

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