Unsolved Error when importing library


  • Hello !
    I follow the doc to import an external library in my plugin (https://github.com/PokeAPI/pokedex-promise-v2)

    I my library.js, in plugin.init, I add this :

    routeHelpers.setupPageRoute(router, '/pokemon/:param1', [(req, res, next) => {

      winston.info(`[plugins/pokemon] In middleware. This argument can be either a single middleware or an array of middlewares`);
    
      setImmediate(next);
    

    }], async (req, res) => {

      const pokemonName = req.params.param1;
    
      let pokemonSpec, frenchName
    
      require('pokedex-promise-v2', async (Pokedex) => {
    
      	const P = new Pokedex();
    
      	pokemonSpec = await P.getPokemonSpeciesByName(pokemonName);
    
      	// // pokemonSpec = await P.getCharacteristicById(pokemonName);
    
      	frenchName = pokemonSpec.names.filter(pokeAPIName => pokeAPIName.language.name === 'fr')[0].name;
    
    
    
      });
    
    
    
    
    
      
    
    
    
      winston.info(`[plugins/pokemon] Navigated to ${nconf.get('relative_path')}/pokemon`);
    
      res.render('pokemon', { pokemon: pokemonName, frenchName : frenchName });
    

    });

    In plugin.json, I have this

    "modules": {

      "../client/quickstart.js": "./static/lib/quickstart.js",
    
      "../admin/plugins/quickstart.js": "./static/lib/admin.js",
    
      "pokedex-promise-v2" : "./node_modules/pokedex-promise-v2"
    

    },

    When I try to go on the route, I have this error :
    alt text

    What I am doing wrong ?


  • @Goby03 require doesn't work like like on the server side.

    Just put

    const Pokedex = require('pokedex-promise-v2');
    

    At the top of the file, then use it later


  • @PitaJ It's the first think I try, like I will do for any classical node projet. It doesn't work. I uust try again and it seems to block. NodeBB can't find my routes

  • NodeBB Admin

    Did you try

    const Pokedex = await import('pokedex-promise-v2');
    const P = new Pokedex();
    

    As suggested by the above link.


  • @baris I just try and it doesn't change anything 😞
    Does it mean I will have to edit the base js of nodeBB to convert it to esm ?

  • NodeBB Admin

    That would be too much work, can you use an older version of 'pokedex-promise-v2' that allows commonjs usage.

    Also what happened when you used.

    const Pokedex = await import('pokedex-promise-v2');
    const P = new Pokedex();
    

    Are you seeing any error messages or is it just not returning anything in Pokedex?


  • @baris No error, exept a "not found" on every route of libray.js

    I feel silly. I hadn't thought to use an older version. It works ! Thank you very much !!!!


Suggested Topics

  • 2
  • 2
  • 6
  • 2
  • 1
| | | |