# Embedding NR into an existing app

**URL:** <https://discourse.nodered.org/t/embedding-nr-into-an-existing-app/93561>\
**Category:** General\
**Created:** [29 November 2024 07:24 UTC](https://discourse.nodered.org/t/embedding-nr-into-an-existing-app/93561 "2024-11-29T07:24:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pratham](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@Pratham](https://discourse.nodered.org/u/Pratham)\
**Post date:** [29 November 2024 07:24 UTC](https://discourse.nodered.org/t/embedding-nr-into-an-existing-app/93561/1 "2024-11-29T07:24:32Z")

</div>

Hi, I'm trying to embed node red in my nodejs project referring to [Embedding into an existing app : Node-RED](https://nodered.org/docs/user-guide/runtime/embedding#:~:text=It%20is%20possible%20to%20embed,dependencies%20in%20your%20application%27s%20package).

but it try to build executable for my project I am not able to  
my package. json file

```json
{
  "name": "nodered",
  "version": "1.0.0",
  "main": "index.cjs",
  "bin": {
    "nodered-app": "index.cjs"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "pkg --target=latest-win-x64 -o \"./dist/service.exe\" index.cjs"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "axios": "^1.7.8",
    "express": "^4.21.1",
    "node-red": "^4.0.5"
  },
  "devDependencies": {
    "esbuild": "^0.24.0"
  }
}

```

index.cjs

```javascript
const http = require('http');
const express = require('express');
const RED = require('node-red');

const app = express();
const server = http.createServer(app);

// Middleware for parsing JSON request bodies
app.use(express.json());

// Define Node-RED settings
const settings = {
    httpAdminRoot: "/red", // Node-RED editor available at /red
    httpNodeRoot: "/api", // Node-RED HTTP nodes available at /api
    userDir: "./node-red-data", // Directory for Node-RED flow configurations
    functionGlobalContext: {}, // Global context for Function nodes
};

// Initialize Node-RED
RED.init(server, settings);

// Serve the Node-RED editor
app.use(settings.httpAdminRoot, RED.httpAdmin);

// Serve the Node-RED HTTP nodes
app.use(settings.httpNodeRoot, RED.httpNode);

// Define custom APIs
app.get('/custom/status', (req, res) => {
    res.json({ status: "Node-RED is running", uptime: process.uptime() });
});

app.post('/custom/add-flow', async (req, res) => {
    const flow = req.body; // Expect flow data in the request body

    try {
        // Example of adding a flow via Node-RED Admin API
        const adminApi = `http://localhost:1880/red/flows`;
        const response = await fetch(adminApi, {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify({ flows: flow })
        });

        if (response.ok) {
            res.json({ message: "Flow added successfully" });
        } else {
            res.status(500).json({ error: "Failed to add flow" });
        }
    } catch (error) {
        console.error("Error adding flow:", error);
        res.status(500).json({ error: "Internal server error" });
    }
});

// Start the server
server.listen(1880, () => {
    console.log("Server running at http://localhost:1880");
    console.log("Node-RED editor available at http://localhost:1880/red");
});

// Start the Node-RED runtime
RED.start();

```

and if try to exeute `npm run build`

I get following error

```auto
> pkg@5.8.1
(node:6400) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
> Fetching base Node.js binaries to PKG_CACHE_PATH
  fetched-v16.16.0-win-x64 [====================] 100%

> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\index.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\index.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\axios.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\utils.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\helpers\bind.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\core\Axios.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\core\mergeConfig.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\defaults\index.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\helpers\formDataToJSON.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\cancel\CanceledError.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\cancel\CancelToken.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\cancel\isCancel.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\env\data.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\helpers\toFormData.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\core\AxiosError.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\helpers\spread.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\helpers\isAxiosError.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\core\AxiosHeaders.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\adapters\adapters.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\helpers\HttpStatusCode.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\helpers\buildURL.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\core\InterceptorManager.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\core\dispatchRequest.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\core\buildFullPath.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\helpers\validator.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\defaults\transitional.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\helpers\toURLEncodedForm.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\platform\index.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\platform\node\classes\FormData.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\helpers\parseHeaders.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\adapters\http.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\adapters\xhr.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\adapters\fetch.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\form-data-encoder\lib\index.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\lowercase-keys\index.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\p-cancelable\index.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\responselike\index.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\helpers\AxiosURLSearchParams.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\core\transformData.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\helpers\isAbsoluteURL.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\helpers\combineURLs.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\platform\node\index.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\platform\common\utils.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\core\settle.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\helpers\fromDataURI.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\helpers\AxiosTransformStream.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\helpers\formDataToStream.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\helpers\readBlob.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\helpers\ZlibHeaderTransformStream.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\helpers\callbackify.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\helpers\progressEventReducer.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\helpers\parseProtocol.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\helpers\resolveConfig.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\helpers\composeSignals.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\helpers\trackStream.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\mimic-response\index.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\normalize-url\index.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\platform\node\classes\URLSearchParams.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\helpers\speedometer.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\helpers\throttle.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\helpers\isURLSameOrigin.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\axios\lib\helpers\cookies.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\string-width\index.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\@isaacs\cliui\node_modules\strip-ansi\index.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\wrap-ansi\index.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\string-width\node_modules\strip-ansi\index.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\@isaacs\cliui\node_modules\ansi-regex\index.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\ansi-styles\index.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\wrap-ansi\node_modules\strip-ansi\index.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\string-width\node_modules\ansi-regex\index.js
> Warning Failed to make bytecode node16-x64 for file C:\snapshot\nodered\node_modules\wrap-ansi\node_modules\ansi-regex\index.js

```

can anyone help me with it ?

---

<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:** [29 November 2024 12:12 UTC](https://discourse.nodered.org/t/embedding-nr-into-an-existing-app/93561/2 "2024-11-29T12:12:52Z")

</div>

Moved to general category to hopefully get more interest.

---

<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:** [29 November 2024 12:17 UTC](https://discourse.nodered.org/t/embedding-nr-into-an-existing-app/93561/3 "2024-11-29T12:17:23Z")

</div>

what does the `pkg` executable do? Is it a script? If so, can you share that? And if so, does it run esbuild? If so, you probably need to share your esbuild config file as well.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [29 November 2024 15:45 UTC](https://discourse.nodered.org/t/embedding-nr-into-an-existing-app/93561/4 "2024-11-29T15:45:22Z")

</div>

I'm guessing it's this - [GitHub - vercel/pkg: Package your Node.js project into an executable](https://github.com/vercel/pkg) - so sounds like a question for them really (especially when it mentions node16-x64 - which would see to indicate node16 ??? which we don't support any more) - but given that it is deprecated that may not be fruitful.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [29 November 2024 18:42 UTC](https://discourse.nodered.org/t/embedding-nr-into-an-existing-app/93561/5 "2024-11-29T18:42:22Z")

</div>

Embedding it into another Node JS app is one thing.  
But if you're trying to achieve a Native Executable (that houses Node RED)

May I plug one of my projects?

> **[GitHub - marcus-j-davies/Node-RED-SFE: A Single File Executable framework for Node-RED](https://github.com/marcus-j-davies/Node-RED-SFE)**
>
> A Single File Executable framework for Node-RED

Guide  
[GitHub - marcus-j-davies/Node-RED-SFE: A Single File Executable framework for Node-RED](https://github.com/marcus-j-davies/Node-RED-SFE?tab=readme-ov-file#sold-sold-sold)

Follow the guide to the letter - and you will have an executable with an embedded (baked in/read-only flow)

It uses a fork of `pkg` - which is actively maintained, and was developed around Node RED v4

[![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/6/f6dcd175fe04e7eb85cd75b9e2a0a721ec628b26.jpeg) ](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/6/f6dcd175fe04e7eb85cd75b9e2a0a721ec628b26.jpeg)

---

<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:** [27 February 2025 18:42 UTC](https://discourse.nodered.org/t/embedding-nr-into-an-existing-app/93561/6 "2025-02-27T18:42:25Z")

</div>

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