MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.

    Error Node_helper custome Module

    Scheduled Pinned Locked Moved Development
    6 Posts 2 Posters 722 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C Offline
      Chiiki58
      last edited by sdetweil

      Hey Guys i’m trying to set up a custome Module to screen the menu of my scools canteen. I ask ChatGPT for the Scripts but he can’t help me anymore thats the reason why i write here.

      The module is called MenuZFV and has a .js file as well as .css and also a node_helper.js which I paste below. The problem is the data can be accessed from the website: (‘https://app.food2050.ch/de/stfw/restaurant -stfw/menu/mittagsmenue/weekly?week=26’) cannot be accessed. Npm install in the folder was done. ChatGPT recommended puppeteer to me because there is no DOM on the website, I installed it via npm install, but when I run node node_helper.js this log comes up - can anyone help me?

      LOG:

      node:internal/modules/cjs/loader:1051
        throw err;
        ^
      
      Error: Cannot find module 'node_helper'
      Require stack:
      - /home/pi/MagicMirror/modules/MMM-MenuZFV/node_helper.js
          at Module._resolveFilename (node:internal/modules/cjs/loader:1048:15)
          at Module._load (node:internal/modules/cjs/loader:901:27)
          at Module.require (node:internal/modules/cjs/loader:1115:19)
          at require (node:internal/modules/helpers:130:18)
          at Object.<anonymous> (/home/pi/MagicMirror/modules/MMM-MenuZFV/node_helper.js:1:20)
          at Module._compile (node:internal/modules/cjs/loader:1241:14)
          at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
          at Module.load (node:internal/modules/cjs/loader:1091:32)
          at Module._load (node:internal/modules/cjs/loader:938:12)
          at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12) {
        code: 'MODULE_NOT_FOUND',
        requireStack: [ '/home/pi/MagicMirror/modules/MMM-MenuZFV/node_helper.js' ]
      }
      

      Node.js v20.8.0

      .JS:

      const puppeteer = require('puppeteer');
      
      (async () => {
        const browser = await puppeteer.launch({ headless: true });
        const page = await browser.newPage();
        const url = 'https://app.food2050.ch/de/stfw/restaurant-stfw/menu/mittagsmenue/weekly?week=26';
      
        try {
          await page.goto(url, { waitUntil: 'networkidle2' });
      
          const menuItems = await page.evaluate(() => {
            const items = [];
            document.querySelectorAll('p.sc-e418db57-3.eOyIUO').forEach(item => {
              items.push(item.textContent.trim());
            });
            return items;
          });
      
          console.log('Menu Items:');
          console.log(menuItems);
      
        } catch (error) {
          console.error('Error fetching menu:', error);
        } finally {
          await browser.close();
        }
      })();
      

      .CSS:

      .MMM-MenuZFV .day {
          font-weight: bold;
          padding-top: 10px;
      }
      
      .MMM-MenuZFV .menu-name {
          text-align: left;
      }
      
      .MMM-MenuZFV .menu-price {
          text-align: right;
      }
      

      NODE_HELPER.JS:

      const NodeHelper = require('node_helper');
      const puppeteer = require('puppeteer');
      
      module.exports = NodeHelper.create({
        start: function () {
          console.log('Starting node helper for: ' + this.name);
        },
      
        socketNotificationReceived: function (notification, payload) {
          if (notification === 'GET_MENU') {
            console.log('Fetching menu from URL: ' + payload.url); // Log the URL being fetched
            this.getMenu(payload.url); // Pass the URL from payload
          }
        },
      
        getMenu: async function (url) {
          try {
            const browser = await puppeteer.launch({ headless: true });
            const page = await browser.newPage();
            await page.goto(url, { waitUntil: 'networkidle2' });
      
            const menuItems = await page.evaluate(() => {
              const items = [];
              document.querySelectorAll('p.sc-e418db57-3.eOyIUO').forEach(item => {
                items.push(item.textContent.trim());
              });
              return items;
            });
      
            await browser.close();
      
            console.log('Extracted menu items:');
            console.log(menuItems);
      
            if (menuItems.length === 0) {
              menuItems.push('Kein Menü gefunden.');
            }
      
            this.sendSocketNotification('MENU_RESULT', menuItems);
          } catch (error) {
            console.error('Error fetching menu:', error);
            this.sendSocketNotification('MENU_RESULT', ['Fehler beim Abrufen des Menüs.']);
          }
        }
      });
      

      Thank you very much for your time and help.

      S 1 Reply Last reply Reply Quote 0
      • S Offline
        sdetweil @Chiiki58
        last edited by sdetweil

        @Chiiki58 where are you building this module?? are the files located in a folder off the MagicMirror/modules folder??

        we create alias entries to the includes for some things…

        so your files have to be in the right place…

        pupetteer is really about executing a web page, clicking, scrolling

        you shouldn’t need that I don’t think…

        cheerio will let you extract parts of the page and
        https://www.npmjs.com/package/cheerio
        https://www.npmjs.com/package/html-to-json-parser
        html_to_json to convert them to json

        also, see my sampleModule that has working node_helper.js
        https://github.com/sdetweil/SampleModule

        also, names matter… MagicMirror is case sensitive

        and the link to the doc for module creators
        https://docs.magicmirror.builders/development/introduction.html

        Sam

        How to add modules

        learning how to use browser developers window for css changes

        S 1 Reply Last reply Reply Quote 0
        • S Offline
          sdetweil @sdetweil
          last edited by sdetweil

          @Chiiki58 also note the required files

          module_name(in config.js) = folder_name = filename.js = register inside this file
          all MUST MATCH exactly

          your .js doesn’t have the register

          css file name has to match the return from

          getStyles: function

          Sam

          How to add modules

          learning how to use browser developers window for css changes

          C 1 Reply Last reply Reply Quote 0
          • C Offline
            Chiiki58 @sdetweil
            last edited by

            @sdetweil Yeah of course I have everything in the right folder with the right name

            I also had cheerio installed, I’ll have to check it out

            I’ll try it with httptojson

            Thank you very much, Sam

            1 Reply Last reply Reply Quote 0
            • C Offline
              Chiiki58
              last edited by

              It works, thank you very much for the input. I’ll probably upload it to GIT once I get the layout right. Now I’ve just unstructured all the text from the website

              S 1 Reply Last reply Reply Quote 0
              • S Offline
                sdetweil @Chiiki58
                last edited by

                @Chiiki58 awesome, glad you’re making progress.

                Sam

                How to add modules

                learning how to use browser developers window for css changes

                1 Reply Last reply Reply Quote 0

                Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                With your input, this post could be even better 💗

                Register Login
                • 1 / 1
                • First post
                  Last post
                Enjoying MagicMirror? Please consider a donation!
                MagicMirror created by Michael Teeuw.
                Forum managed by Sam, technical setup by Karsten.
                This forum is using NodeBB as its core | Contributors
                Contact | Privacy Policy