NEWS
Funktion deklarierte Datei JavaScript
-
file 13-add.js
function add(a, b) { return parseInt(a) + parseInt(b); }
die main.js-Datei
const add = require('./13-add').add; console.log(add(3, 5));
Ich versuche, die add-Methode in 13-add.js von main.js aufzurufen, aber wenn ich main.js ausführe, gibt es einen Fehler $./13-main.js zurück
console.log(add(3, 5)); ^ TypeError: add is not a function at Object.<anonymous>
Am Anfang der Datei 13-add.js habe ich add() eingefügt:
add(); function add(a, b) { return parseInt(a) + parseInt(b); }
Der Fehler bleibt jedoch derselbe.
-
@jacusi
Mit require() lädt man Module, die als solche deklariert sind.'use strict'; module.exports = function (a, b) { return a + b; };
Aufruf:
const add = require('./13-add'); // Pfad muss passen! log(add(3,5));
Damit eigene Module mit gesichert werden, habe ich sie unter "iobroker-data.modules" gespeichert.
const add = require('../../../iobroker-data/modules/13-add'); log(add(3,5));