Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. Einfügen von JS-Klassen aus eigenem NPM-Modul

    NEWS

    • ioBroker@Smart Living Forum Solingen, 14.06. - Agenda added

    • ioBroker goes Matter ... Matter Adapter in Stable

    • Monatsrückblick - April 2025

    Einfügen von JS-Klassen aus eigenem NPM-Modul

    This topic has been deleted. Only users with topic management privileges can see it.
    • U
      uwe72 @ticaki last edited by

      @ticaki Habe nicht alles verstanden was Du schriebst. Hast Du nun bereits das Ganze in TS?

      T 1 Reply Last reply Reply Quote 0
      • T
        ticaki Developer @uwe72 last edited by

        @uwe72
        Ich bin wohl zu müde 🙂

        Nein, ich habe das nicht in TS.

        Wenn ich die Ausführungen von @fastfoot richtig verstehe, muß man TS erst in Javascript transpilieren, was alle Typenanmerkungen entfernt und kann sie dann importieren.

        In dem Fall kann man diese auch gleich in JS schreiben und in die TS Scripte importieren.

        1 Reply Last reply Reply Quote 0
        • U
          uwe72 @fastfoot last edited by uwe72

          @fastfoot @ticaki

          TS-Lösung funktioniert!

          Wie von @ticaki vorgeschlagen funktioniert. D.h. TS-Script liegt extern und muss transpilierst werden.

          /home/uwe72/clement/docker/my-datas/iobroker/iobrokerdata/my_scripts/buch.ts

          class Buch  {
          
              public getCurrentWeekdayAsString() : string {
                  var now = new Date();
                  let weekday = now.getDay();
                  return this.getWeekdayAsString(weekday);
              }
          
              private getWeekdayAsString(weekday: number) : string {
                  let weekdayAsString;
                  if (weekday == 1) {
                      weekdayAsString = "Montag";
                  } else if (weekday == 2) {
                      weekdayAsString = "Dienstag";
                  } else if (weekday == 3) {
                      weekdayAsString = "Mittwoch";
                  } else if (weekday == 4) {
                      weekdayAsString = "Donnerstag";
                  } else if (weekday == 5) {
                      weekdayAsString = "Freitag";
                  } else if (weekday == 6) {
                      weekdayAsString = "Samstag";
                  } else if (weekday == 7) {
                      weekdayAsString = "Sonntag";
                  } else if (weekday == 0) {
                      weekdayAsString = "Sonntag";
                  }
                  return weekdayAsString;
              }  
          }
          
          module.exports = { Buch};
          

          Dann in der Konsole folgendes eingeben (habe ich noch nicht im Detail verstanden):

          npx tsc buch.ts
          

          --> Es entsteht die Datei buch.js

          ioBroker-Script (TypeScript!)

          const { Buch } = require('/opt/iobroker/my_scripts/buch.js');
          const myBook = new Buch();
          log("Heute ist: " + myBook.getCurrentWeekdayAsString());
          

          Logausgabe:

          21:03:04.274	info	javascript.1 (65633) script.js.common.TEST_IMPORT_TS: Heute ist: Samstag
          

          Bin jetzt super glücklich! DANKE EUCH BEIDEN für das tolle und konstruktive Miteinander!

          @ticaki
          In dem Fall kann man diese auch gleich in JS schreiben und in die TS Scripte importieren.
          Erst jetzt kapiert. Ja in der Tat! Wobei in meinem Fall ich schon alle Scripte in TS habe.

          U 1 Reply Last reply Reply Quote 1
          • U
            uwe72 @uwe72 last edited by uwe72

            @fastfoot

            Noch eine Nachfrage für ein blödes Beispiel:

            Möchte besipielsweise so was verwenden in meinen Scripten:

            sendTo('telegram.0', "TEST");
            

            /home/uwe72/clement/docker/my-datas/iobroker/iobrokerdata/my_scripts/buch.ts

            class Buch  {
            
                public getCurrentWeekdayAsString() : string {
                    var now = new Date();
                    let weekday = now.getDay();
                    sendTo('telegram.0', "TEST"); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                    return this.getWeekdayAsString(weekday);
                }
            
                private getWeekdayAsString(weekday: number) : string {
                    let weekdayAsString;
                    if (weekday == 1) {
                        weekdayAsString = "Montag";
                    } else if (weekday == 2) {
                        weekdayAsString = "Dienstag";
                    } else if (weekday == 3) {
                        weekdayAsString = "Mittwoch";
                    } else if (weekday == 4) {
                        weekdayAsString = "Donnerstag";
                    } else if (weekday == 5) {
                        weekdayAsString = "Freitag";
                    } else if (weekday == 6) {
                        weekdayAsString = "Samstag";
                    } else if (weekday == 7) {
                        weekdayAsString = "Sonntag";
                    } else if (weekday == 0) {
                        weekdayAsString = "Sonntag";
                    }
                    return weekdayAsString;
                }  
            }
            
            module.exports = { Buch};
            

            Transpilieren:

            npx tsc buch.ts
            

            Fehlermeldung:

            root@iobroker:/opt/iobroker/my_scripts# npx tsc buch.ts
            buch.ts:2:5 - error TS2304: Cannot find name 'sendTo'.
            
                  sendTo('telegram.0', "TEST");
                  ~~~~~~
            
            
            Found 1 error in buch.ts
            

            Vermutlich fehlt hier ein "Import-Statement" von den ganzen "Allgemeinen-Sachen"?

            T 1 Reply Last reply Reply Quote 0
            • T
              ticaki Developer @uwe72 last edited by ticaki

              @uwe72

              https://github.com/ioBroker/ioBroker.javascript/blob/master/lib/javascript.d.ts

              tsconfig.json

              {
                "compileOnSave": true,
                "compilerOptions": {
                  "noEmit": true,
                  "allowJs": true,
                  "checkJs": true,
                  "module": "commonjs",
                  "moduleResolution": "node",
                  "esModuleInterop": true,
                  "resolveJsonModule": true,
                  "strict": true,
                  "noImplicitAny": false,
                  "target": "es2018",
                  "typeRoots": [
                    ".iobroker/types",
                    "node_modules/@types"
                  ]
                },
                "include": [
                  "**/*.js",
                  "**/*.ts",
                  ".iobroker/types/javascript.d.ts"
                ],
                "exclude": [
                  "node_modules/**"
                ]
              }
              

              Wenn du VS Code benutzt kannst du dir mit der iobrokererweiterungen, das auch angucken wie das da gelöst ist, weiß aber natürlich nicht ob das compilieren geht. Wird in vs dann aber nicht als Fehler angezeigt.

              U 1 Reply Last reply Reply Quote 1
              • U
                uwe72 @ticaki last edited by uwe72

                @ticaki Nun bin ich zu müde. Komme da nicht weiter, habe keine Ahnung und leider fehlen mir hier (Grund-) kenntnisse.

                {
                  "compileOnSave": true,
                  "compilerOptions": {
                    "noEmit": true,
                    "allowJs": true,
                    "checkJs": true,
                    "module": "commonjs",
                    "moduleResolution": "node",
                    "esModuleInterop": true,
                    "resolveJsonModule": true,
                    "strict": true,
                    "noImplicitAny": false,
                    "target": "es2018",
                    "typeRoots": [
                      ".iobroker/types",
                      "node_modules/@types"
                    ]
                  },
                  "include": [
                    "buch.ts",
                    "/opt/iobroker/node_modules/iobroker.javascript/lib/javascript.d.ts"
                  ],
                  "exclude": [
                  ]
                }
                

                Habe ein tsconfig.json File und transpiliere dies

                npx tsc --build tsconfig.json
                

                --> Nur Fehlermeldungen. Habe keine Ahnung wie ich "javascript.d.ts" einbinde

                b606e419-3efa-4001-a6cd-66ddb90c04d6-image.png

                T 1 Reply Last reply Reply Quote 0
                • T
                  ticaki Developer @uwe72 last edited by

                  @uwe72
                  Morgen kann ich das mal ausprobieren, aber ich bin schon vor 2h müde gewesen. 🙂

                  U 2 Replies Last reply Reply Quote 0
                  • U
                    uwe72 @ticaki last edited by

                    @ticaki Danke dir!! 🙂

                    1 Reply Last reply Reply Quote 0
                    • U
                      uwe72 @ticaki last edited by

                      @ticaki @fastfoot

                      Anbei nochmals das nicht funktionierende Beispiel und den aktuellen Stand in übersichtlicher Form:

                      Hier mein externes TS-File.

                      class Buch  {
                      
                          public getCurrentWeekdayAsString() : string {
                              var now = new Date();
                              let weekday = now.getDay();
                      
                                          sendTo("email.0", {
                                              from:    "uwe.clement@gmail.com",                                                                
                                              to:      "uwe.clement@gmail.com",
                                              subject: "Test1",
                                              html: "test2"
                                          });	
                      
                      
                              return this.getWeekdayAsString(weekday);
                          }
                      
                          public getWeekdayAsString(weekday: number) : string {
                              let weekdayAsString;
                              if (weekday == 1) {
                                  weekdayAsString = "Montag";
                              } else if (weekday == 2) {
                                  weekdayAsString = "Dienstag";
                              } else if (weekday == 3) {
                                  weekdayAsString = "Mittwoch";
                              } else if (weekday == 4) {
                                  weekdayAsString = "Donnerstag";
                              } else if (weekday == 5) {
                                  weekdayAsString = "Freitag";
                              } else if (weekday == 6) {
                                  weekdayAsString = "Samstag";
                              } else if (weekday == 7) {
                                  weekdayAsString = "Sonntag";
                              } else if (weekday == 0) {
                                  weekdayAsString = "Sonntag";
                              }
                              return weekdayAsString;
                          }  
                      
                      }
                      
                      module.exports = { Buch};
                      

                      Ohne das "sendTo" funktionierte es. Mit eben nicht.

                      Habe im gleichen Verzeichnis diese Datei tsconfig.json:

                      {
                        "compileOnSave": true,
                        "compilerOptions": {
                          "noEmit": true,
                          "allowJs": true,
                          "checkJs": true,
                          "module": "commonjs",
                          "moduleResolution": "node",
                          "esModuleInterop": true,
                          "resolveJsonModule": true,
                          "strict": true,
                          "noImplicitAny": false,
                          "target": "es2018",
                          "typeRoots": [
                            ".iobroker/types",
                            "node_modules/@types"
                          ]
                        },
                        "include": [
                          "**/*.js",
                          "**/*.ts",
                          ".iobroker/types/javascript.d.ts"
                        ],
                        "exclude": [
                          "node_modules/**"
                        ]
                      }
                      

                      Auf der Konsole gebe ich folgendes ein:

                      npx tsc --build tsconfig.json
                      

                      Führt zu diesen Fehlern:

                      buch.ts:7:21 - error TS2304: Cannot find name 'sendTo'.
                      
                      7                     sendTo("email.0", {
                                            ~~~~~~
                      
                      buch.ts:42:1 - error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.
                      
                      42 module.exports = { Buch};
                         ~~~~~~
                      
                      
                      Found 2 errors.
                      

                      Beide Dateien liegen im gleichen Verzeichnis, in dem ich auch den Befehl "npx tsc --build tsconfig.json" eingebe:
                      4ccda497-e58f-4019-8f71-ac27a25da5e7-image.png

                      T 2 Replies Last reply Reply Quote 0
                      • T
                        ticaki Developer @uwe72 last edited by ticaki

                        @uwe72

                        "include": [
                            "**/*.js",
                            "**/*.ts",
                            ".iobroker/types/javascript.d.ts"
                          ],
                        

                        da muß die javascript.d.ts datei sein. Kannst das natürlich anpassen.

                        1 Reply Last reply Reply Quote 0
                        • T
                          ticaki Developer @uwe72 last edited by ticaki

                          @uwe72

                          https://github.com/ticaki/script-library-example

                          Hab noch nicht versucht, das im iobroker einzubinden, das complilieren geht schon mal. Hab console und name aus der javascript-d.ts raus werfen müssen.

                          Im Arbeitsverzeichnis npx tsc eingeben.

                          Ne geht doch noch nicht. 🙂

                          U 1 Reply Last reply Reply Quote 0
                          • U
                            uwe72 @ticaki last edited by uwe72

                            Update: Habe das aus Github 1:1 reinkopiert und scheint zu funktionieren über "npx tsc"

                            T 1 Reply Last reply Reply Quote 0
                            • T
                              ticaki Developer @uwe72 last edited by

                              @uwe72
                              Hm im log steht doch alles? Es geht trotzdem noch nicht.

                              U 1 Reply Last reply Reply Quote 1
                              • U
                                uwe72 @ticaki last edited by

                                @ticaki Ja, es kommt nun keine Fehlermeldung mehr. Die lib.js wird leider nicht erstellt

                                T 1 Reply Last reply Reply Quote 0
                                • T
                                  ticaki Developer @uwe72 last edited by

                                  @uwe72 sagte in Einfügen von JS-Klassen aus eigenem NPM-Modul:

                                  @ticaki Ja, es kommt nun keine Fehlermeldung mehr. Die lib.js wird leider nicht erstellt

                                  Jo, das hab ich jetzt alles hinbekommen. Aber der Javascript-Adapter kennt die iobroker eigenen Skriptbefehle nicht, wenn sie in einer externen Datei stecken. Nachvollziehbar.

                                  Kann man zwar lösen in dem man beim Erzeugen der Klasse die Funktionen übergibt. Das ist aber nicht schön.

                                  U 1 Reply Last reply Reply Quote 0
                                  • U
                                    uwe72 @ticaki last edited by

                                    @ticaki said in Einfügen von JS-Klassen aus eigenem NPM-Modul:

                                    Jo, das hab ich jetzt alles hinbekommen.

                                    Was hast Du noch geändert im Vergleich zu zuvor?

                                    T 1 Reply Last reply Reply Quote 0
                                    • T
                                      ticaki Developer @uwe72 last edited by ticaki

                                      @uwe72
                                      Habs auf Github aktualisiert, aber die Verwendung von iobroker eigenen Befehlen endet in undefined oder error. Dafür müsste ich mich wohl durch den Javascript-Adapter arbeiten um zu verstehen wie das genau verarbeitet wird... Em nö 🙂

                                      EDIT: Ach und eine Änderung am Importfile verlangt nach einem Restart der Javascriptinstanze.

                                      U 2 Replies Last reply Reply Quote 0
                                      • U
                                        uwe72 @ticaki last edited by

                                        @ticaki Danke! Ja, lib.js wird nun erstellt. Für alles weitere habe ich heute keine Zeit mehr.

                                        Grüße!

                                        1 Reply Last reply Reply Quote 0
                                        • U
                                          uwe72 @ticaki last edited by

                                          @ticaki Ja, ist bei mir auch so. sendTo() beispielsweise wird dann im iobroker Script nicht erkannt.

                                          lib.ts:

                                          class Person {
                                              private nachname: string;
                                              private vorname: string;
                                              private alter: string;
                                              constructor(vorname, nachname, alter) {
                                                this.vorname = vorname;
                                                this.nachname = nachname;
                                                this.alter = alter;
                                              }
                                            
                                              information(): string {
                                          
                                                              sendTo("email.0", {
                                                                  from:    "uwe.clement@gmail.com",                                                                
                                                                  to:      "uwe.clement@gmail.com",
                                                                  subject: "Test1",
                                                                  html: "test2"
                                                              });	
                                          
                                                return (`Mein Name ist ${this.vorname} ${this.nachname} und ich bin ${this.alter} Jahre alt!!!!1`);
                                              }
                                              log(): void {
                                                  log(this.information());
                                              }
                                            }
                                           
                                          module.exports = { Person};
                                          

                                          ioBroker:

                                          const { Person } = require('/opt/iobroker/my_scripts/lib.js');
                                          const myPerson = new Person();
                                          log(myPerson.information());
                                          

                                          Fehlermeldung:

                                          javascript.1
                                          2023-11-12 12:45:31.652	error	at Script.runInContext (node:vm:135:12)
                                          
                                          javascript.1
                                          2023-11-12 12:45:31.652	error	at script.js.common.TEST_IMPORT_TS:8:14
                                          
                                          javascript.1
                                          2023-11-12 12:45:31.652	error	at Person.information (/opt/iobroker/my_scripts/lib.js:8:9)
                                          
                                          javascript.1
                                          2023-11-12 12:45:31.652	error	ReferenceError: sendTo is not defined
                                          
                                          javascript.1
                                          2023-11-12 12:45:31.652	error	^
                                          
                                          javascript.1
                                          2023-11-12 12:45:31.652	error	sendTo("email.0", {
                                          
                                          javascript.1
                                          2023-11-12 12:45:31.652	error	script.js.common.TEST_IMPORT_TS: /opt/iobroker/my_scripts/lib.js:8
                                          
                                          U 1 Reply Last reply Reply Quote 0
                                          • U
                                            uwe72 @uwe72 last edited by uwe72

                                            @fastfoot Wollte Dich fragen, ob Du eine Idee hast wie man es schafft im externen Script "Dinge" wie sendTo() zu verwenden? Extern lässt es sich fehlerfrei in ein *.js umwandeln. In Iobroker wird der Inhalt des Scriptes angemeckert, da sendTo() unbekannt.

                                            U F 2 Replies Last reply Reply Quote 0
                                            • First post
                                              Last post

                                            Support us

                                            ioBroker
                                            Community Adapters
                                            Donate

                                            518
                                            Online

                                            31.7k
                                            Users

                                            79.7k
                                            Topics

                                            1.3m
                                            Posts

                                            4
                                            51
                                            2698
                                            Loading More Posts
                                            • Oldest to Newest
                                            • Newest to Oldest
                                            • Most Votes
                                            Reply
                                            • Reply as topic
                                            Log in to reply
                                            Community
                                            Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
                                            The ioBroker Community 2014-2023
                                            logo