goja_nodejs

repository·master·Indexed 19 days ago

https://github.com/dop251/goja_nodejs

A collection of Go modules providing Node.js compatibility, including the require system and core modules, for the Goja JavaScript engine. It includes a require.Registry for attaching the compatibility layer to Goja runtimes and provides TypeScript type definitions via npm packages under the @dop251/types-goja_nodejs-MODULE pattern.

Tokens
843
Snippets
4
Records
5
Agent score
64%

What's inside goja_nodejs

  1. Configure TypeScript type definitions for goja_nodejs

    master

    Type definitions for the various modules are published to npm under the @dop251/types-goja_nodejs-MODULE pattern (e.g., @dop251/types-goja_nodejs-buffer).

    To use these definitions in a TypeScript project:

    1. Install the specific module type package via npm.
    2. Update your tsconfig.json to include the @dop251 directory in your typeRoots.

    Note: Modules are split into separate packages so they can be enabled or disabled individually in your Go environment.

    {
      "compilerOptions": {
        "typeRoots": [
          "./node_modules/@types",
          "./node_modules/@dop251"
        ]
      }
    }
  2. Enable Node.js compatibility in Goja

    master

    To use Node.js modules (like require) within a Goja runtime, you must use a require.Registry. A single registry can be shared across multiple runtimes.

    1. Create a new require.Registry.
    2. Call registry.Enable(runtime) to attach the Node.js compatibility layer to your Goja instance.
    3. Use require.Require(path) on the returned object to load specific modules.
    package main
    
    import (
        "github.com/dop251/goja"
        "github.com/dop251/goja_nodejs/require"
    )
    
    func main() {
        // This registry can be shared by multiple runtimes
        registry := new(require.Registry)
    
        runtime := goja.New()
        // Enable Node.js compatibility for this runtime
        req := registry.Enable(runtime)
    
        // You can now use require() inside the Goja runtime
        runtime.RunString(`
        var m = require("./m.js");
        m.test();
        `)
    
        // Or require modules from Go code
        m, err := req.Require("./m.js")
        _, _ = m, err
    }
  3. Install @dop251/types-goja_nodejs-buffer

    master

    To use type definitions for the goja_nodejs buffer module in your development environment, install the @dop251/types-goja_nodejs-buffer package as a development dependency. These definitions only include features currently implemented by the goja_nodejs buffer module.

    npm install --save-dev @dop251/types-goja_nodejs-buffer
  4. Use core type definitions for goja_nodejs

    master
    The @dop251/types-goja_nodejs-global package contains the core type definitions used across the goja_nodejs ecosystem. This package is intended as a dependency for other specific type definition packages (such as buffer or url types) and is generally not meant to be installed directly by end-users.
  5. Install @dop251/types-goja_nodejs-url type definitions

    master

    To use type definitions for the goja_nodejs url module in your TypeScript project, install the @dop251/types-goja_nodejs-url package as a development dependency. Note that these definitions only include features currently implemented by the goja_nodejs url module.

    npm install --save-dev @dop251/types-goja_nodejs-url