# Creating a library that works in both 'global' and ' instance' mode?

**URL:** <https://discourse.processing.org/t/creating-a-library-that-works-in-both-global-and-instance-mode/50358>\
**Category:** Development\
**Created:** [November 29, 2026, 2:55pm UTC](https://discourse.processing.org/t/creating-a-library-that-works-in-both-global-and-instance-mode/50358 "2026-11-29T14:55:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [November 29, 2026, 2:55pm UTC](https://discourse.processing.org/t/creating-a-library-that-works-in-both-global-and-instance-mode/50358/1 "2026-11-29T14:55:47Z")

</div>

I am trying to create a new library that will work transparently in both global and instance modes.

I am using VSC with Typescript and have installed the nodes

- p5
- @types/p5

and updated the `tsconfig.json` file to include them

```auto
"include": [
    "src_ts/**/*",
    "./node_modules/p5/types/global.d.ts",
    "./node_modules/p5/types/p5.d.ts"
    // "libraries"
  ]
}

```

The core class is called `S3D`. An instance of this class will provide access to all the functionality of the library. So far all i have is this

```js
class S3D {

    /** @hidden */ protected _renderer: p5.Renderer;
    /** @hidden */ protected _p: p5;
    /** @hidden */ protected _uid: string;
    /** @hidden */ protected _children: Array<Shape3D>;

    /**
     * @param name a unique string identifier for this controller
     * @param p5c reference to Processing canvas returned by the 
     * 'createCanvas' function
     * @param p reference to the 'instance' variable
     * (defaults to global instance)
     */
    constructor(name: string, p5c: p5.Renderer, p = p5.instance) {
        this._uid = name;
        this._children = [];
        this._p = p;
        this._renderer = p5c;
    }
}

```

At this stage I have 2 errors I need to resolve  
`protected _p: p5;`

- Cannot use namespace ‘p5’ as type ts(2709)

`constructor(name: string, p5c: p5.Renderer, p = p5.instance) {`

- Property ‘instance’ does not exist on type ‘typeof import(“/Users/peter/Documents/js-workspace/Shapes3D/node\_modules/p5/types/p5”)’.

The idea is that the class attribute `_p` will reference the instance variable or if no value passed the default (global) instance variable.

Any suggestions or ideas will be gratefully received.  
😀

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [November 29, 2026, 4:17pm UTC](https://discourse.processing.org/t/creating-a-library-that-works-in-both-global-and-instance-mode/50358/2 "2026-11-29T16:17:29Z")

</div>

> [@quark](#):
>
> At this stage I have 2 errors I need to resolve…

Those aren’t errors but missing types defined in:

> **[UNPKG](https://app.unpkg.com/p5@2.4.0/files/types)**
>
> The CDN for everything on npm

> **[p5 CDN by jsDelivr - A free, fast, and reliable Open Source CDN](https://cdn.jsdelivr.net/npm/p5/types/)**
>
> p5 CDN by jsDelivr - A free, fast, and reliable Open Source CDN for npm and GitHub

> [@quark](#):
>
> `constructor(name: string, p5c: p5.Renderer, p = p5.instance) {`

Parameter _p_’s default value `p5.instance` is valid at runtime regardless of what is defined or missing in “p5.d.ts”.

As a workaround, you can define an extra augmented “.d.ts” type file.  
I’ve asked Gemini to write 1 for it:

```javascript
import type p5 from 'p5';

declare module 'p5' {
  namespace p5 {
    /**
     * The global-mode `p5` sketch instance, or `null` otherwise.
     */
    var instance: p5 | null;
  }
}

```
