• Hey!

    I want to read out a file inside my plugin:

    const fs = require("fs");
    const config = JSON.parse(fs.readFileSync("./utils/config.json", "utf-8"));
    

    My Folder-Structure looks like this:

    ubuntu@ubuntu:~/nodebb-linked-modules/nodebb-plugin-sync-bot$ ls
    lib  library.js  node_modules  package.json  plugin.json  static  utils
    

    Inside of utils is the file config.json which I have to read via the fs.

    const config = JSON.parse(fs.readFileSync("./utils/config.json", "utf-8"));


    On startup I receive the following error:

    error: Error: ENOENT: no such file or directory, open './utils/config.json'

    How can I import files in the same directory to work with it?

    I checked the file permissions. They are identical.

    Looking forwar to your answers. šŸ™‚


  • @pitaj Many thanks for your effort šŸ™

    Okay so I just npm link'ed my plugin again so I thought some file will be updated. But they weren't.

    Here is my full source code of libary.js:

    'use strict';
    
    
    /* NodeBB Basic Plugin Libraries */
    const controllers = require('./lib/controllers');
    const plugin = {};
    
    
    /* Third Party Libraries */
    const Discord = require("discord.js");
    const fs = require("fs");
    
    // Nodebb Discord Bot 
    const config = JSON.parse(fs.readFileSync("./utils/config.json", "utf-8"));
    var client = new Discord.Client();
    
    
    
    let debug = true;
    
    plugin.init = function (params, callback, client) {
    	
    	myDebug("nodebb-discord-bot-started");
    
    	callback();
    };
    
    
    function myDebug(msg){
    	if(debug){
    		console.log(msg);
    	}
    }
    
    module.exports = plugin;
    
    
    2021-01-12T10:46:19.318Z [4567/4689] - error: Error: ENOENT: no such file or directory, open './utils/config.json'
        at Object.openSync (fs.js:462:3)
        at Object.readFileSync (fs.js:364:35)
        at Object.<anonymous> (/home/ubuntu/nodebb-linked-modules/nodebb-plugin-makesmart-discord-bot/library.js:15:30)
        at Module._compile (internal/modules/cjs/loader.js:999:30)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
        at Module.load (internal/modules/cjs/loader.js:863:32)
        at Function.Module._load (internal/modules/cjs/loader.js:708:14)
        at Module.require (internal/modules/cjs/loader.js:887:19)
        at require (internal/modules/cjs/helpers.js:74:18)
        at Object.Plugins.requireLibrary (/home/ubuntu/nodebb/src/plugins/index.js:70:39)
    

    I tried it with path again:

    // Nodebb Discord Bot 
    var configpath = path.join(__dirname, 'utils', 'config.json');
    const config = JSON.parse(fs.readFileSync(configpath, "utf-8"));
    var client = new Discord.Client();
    

    an suddenly ... it worked! o.O

    2021-01-12T10:51:15.711Z [4567/4765] - info: [api] Adding 2 route(s) to `api/v3/plugins`
    2021-01-12T10:51:15.889Z [4567/4765] - info: Routes added
    2021-01-12T10:51:15.933Z [4567/4765] - info: NodeBB Ready
    2021-01-12T10:51:15.943Z [4567/4765] - info: Enabling 'trust proxy'
    2021-01-12T10:51:15.972Z [4567/4765] - info: NodeBB is now listening on: 0.0.0.0:4567
    

    I think it was really because I ran npm link again. Funny.* I'm sorry. Most of the time, the problem is in front of the computer. šŸ™„

    But still a strange behavior in my opinion. Well ... Good to know for the future. @PitaJ as mentioned thank you for you effort. šŸ™


    For any others having the same issue:

    1. Read the Stackoverflow Solution posted by @PitaJ right here
    2. If it still not work use npm link in ./nodebb again to refresh something in the plugin folder ...I dont know tbh ... šŸ˜„
  • Global Moderator


  • @pitaj My folder with the config.json is in the same directory as library.js is. I tried it with path but it didn't work either.

    my-plugin
    ā”œā”€ā”€ library.js
    ā””ā”€ā”€ utlis
        ā””ā”€ā”€ config.json
    
    var configpath = path.join(__dirname, 'utils', 'config.json');
    

    Results also in:

    error: Error: ENOENT: no such file or directory, open './utils/config.json'

    What can I do to get it work? Any other suggestions or tips? Would be awesome! šŸ™‚

  • Global Moderator

    @dogs what you have should work. Are you sure you're using configpath inside the readfilesync?


  • @pitaj Oh okay...

    Yes I am sure it is the correct path. I als tried different folders like static which is being initialized for scripts and templates. Didn't work either.

    Maybe I should open an issue on that topic...

  • Global Moderator

    @dogs no please don't open an issue for this. This is certainly not a problem with NodeBB. In fact this is a basic Node.js programming issue

    Please share the full source of library.js

    The reason I know there's something wrong with your code is that the error mentions ./utils/config.json, but if the correct path with __dirname was passed in, it would be something like node_modules/nodebb-plugin-blah/utils/config.json


  • @pitaj Many thanks for your effort šŸ™

    Okay so I just npm link'ed my plugin again so I thought some file will be updated. But they weren't.

    Here is my full source code of libary.js:

    'use strict';
    
    
    /* NodeBB Basic Plugin Libraries */
    const controllers = require('./lib/controllers');
    const plugin = {};
    
    
    /* Third Party Libraries */
    const Discord = require("discord.js");
    const fs = require("fs");
    
    // Nodebb Discord Bot 
    const config = JSON.parse(fs.readFileSync("./utils/config.json", "utf-8"));
    var client = new Discord.Client();
    
    
    
    let debug = true;
    
    plugin.init = function (params, callback, client) {
    	
    	myDebug("nodebb-discord-bot-started");
    
    	callback();
    };
    
    
    function myDebug(msg){
    	if(debug){
    		console.log(msg);
    	}
    }
    
    module.exports = plugin;
    
    
    2021-01-12T10:46:19.318Z [4567/4689] - error: Error: ENOENT: no such file or directory, open './utils/config.json'
        at Object.openSync (fs.js:462:3)
        at Object.readFileSync (fs.js:364:35)
        at Object.<anonymous> (/home/ubuntu/nodebb-linked-modules/nodebb-plugin-makesmart-discord-bot/library.js:15:30)
        at Module._compile (internal/modules/cjs/loader.js:999:30)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
        at Module.load (internal/modules/cjs/loader.js:863:32)
        at Function.Module._load (internal/modules/cjs/loader.js:708:14)
        at Module.require (internal/modules/cjs/loader.js:887:19)
        at require (internal/modules/cjs/helpers.js:74:18)
        at Object.Plugins.requireLibrary (/home/ubuntu/nodebb/src/plugins/index.js:70:39)
    

    I tried it with path again:

    // Nodebb Discord Bot 
    var configpath = path.join(__dirname, 'utils', 'config.json');
    const config = JSON.parse(fs.readFileSync(configpath, "utf-8"));
    var client = new Discord.Client();
    

    an suddenly ... it worked! o.O

    2021-01-12T10:51:15.711Z [4567/4765] - info: [api] Adding 2 route(s) to `api/v3/plugins`
    2021-01-12T10:51:15.889Z [4567/4765] - info: Routes added
    2021-01-12T10:51:15.933Z [4567/4765] - info: NodeBB Ready
    2021-01-12T10:51:15.943Z [4567/4765] - info: Enabling 'trust proxy'
    2021-01-12T10:51:15.972Z [4567/4765] - info: NodeBB is now listening on: 0.0.0.0:4567
    

    I think it was really because I ran npm link again. Funny.* I'm sorry. Most of the time, the problem is in front of the computer. šŸ™„

    But still a strange behavior in my opinion. Well ... Good to know for the future. @PitaJ as mentioned thank you for you effort. šŸ™


    For any others having the same issue:

    1. Read the Stackoverflow Solution posted by @PitaJ right here
    2. If it still not work use npm link in ./nodebb again to refresh something in the plugin folder ...I dont know tbh ... šŸ˜„

Suggested Topics

  • 2
  • 4
  • 2
  • 63
  • 2
| |