# NodeJs Support

# Node.js Support in App Functions

## Overview

App Functions are executed in a V8 isolate runtime, which is a lightweight and secure environment. This runtime is distinct from a full Node.js environment. Consequently, while App Functions support JavaScript, they do not support all the native APIs and core modules that are available in Node.js.

This document outlines the level of support for Node.js features within the App Functions environment.

## Unsupported Node.js Core Modules

The following Node.js core modules are not available in App Functions. Attempting to use them will result in an error.

-   `child_process`
-   `cluster`
-   `dgram`
-   `fs`
-   `fs/promises`
-   `http` (for creating servers)
-   `https2`
-   `net`
-   `repl`
-   `readline`
-   `tls`
-   `worker_threads`
-   `zlib` (Node.js API)

### Examples of Unsupported Usage

Here are some examples of code that will not work in the App Functions environment.

**Filesystem Access:**
```javascript
// This will fail because the 'fs' module is not available.
import fs from 'fs';
const data = fs.readFileSync('/tmp/file.txt');
```

**Creating a TCP Server:**
```javascript
// This will fail because the 'net' module is not available.
import net from 'net';
net.createServer(() => {}).listen(3000);
```

**Spawning a Subprocess:**
```javascript
// This will fail because the 'child_process' module is not available.
import { exec } from 'child_process';
exec('ffmpeg -i input.mp4 out.mp4');
```

**Creating an HTTP Server:**
```javascript
// This will fail because the 'http' module cannot be used to create servers.
import http from 'http';
http.createServer((req, res) => res.end('hi')).listen(80);
```

**Creating a UDP Socket:**
```javascript
// This will fail because the 'dgram' module is not available.
import dgram from 'dgram';
dgram.createSocket('udp4').bind(1234);
```

## Partially Supported Modules

Some Node.js modules are partially supported, meaning that only a subset of their functionality is available.

-   **`crypto`**: Only the [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API) is supported.
-   **`buffer`**: Basic buffer functionality is available.
-   **`stream`**: Only [WHATWG streams](https://streams.spec.whatwg.org/) are supported.
-   **`os`**: Partially supported.
-   **`path`**: Partially supported.
-   **`util`**: Partially supported.
-   **`events`**: Partially supported.

## Supported Patterns

While direct Node.js APIs for networking are limited, you can make outbound HTTP requests using the `fetch` API, which is a standard web API.

```javascript
async function fetchData() {
  try {
    const response = await fetch("https://api.example.com");
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    const text = await response.text();
    console.log(text);
    return text;
  } catch (error) {
    console.error('Error fetching data:', error);
  }
}
```

## External Packages

Currently, App Functions do not support importing external packages or libraries from sources like npm. We are actively working on enhancing the platform to include support for external libraries in future versions.

