Friday 14 April 2017

modules using requirejs is not exported properly

I am a freelancer webdesigner, graphics designer , motion graphics designer and animator .. I love to code and craft new tech,,, I am currently developing a local javascript app that will help me to dynamically generate some managerial documents such as invoices and vouchers for unpaid services for a specific client.

Ill be using node_modules , and so far i am using (["jsonfile", "uniqid", "mkdirp"]) these modules need require to be stored in a variable.

the problem is that i am calling jsonfile as mention in its documentation

var jsonfile = require("jsonfile");

when console.log(jsonfile) from javascript file it returns function localRequire() unlike if i wrote that command inside browsers console .. for that it will return

>> console.log(jsonfile)
Object { spaces: null, readFile: readFile(), readFileSync: readFileSync(),     writeFile: writeFile(), writeFileSync: writeFileSync() } 
undefined

and to make the problem more complicated;

when calling readFile() from console or from the script it returns that readFile() is not a defined function rather for jsonfile or fs which is a node module that is required for jsonfile

>>function readFile()
jsonfile.readFile()
TypeError: fs.readFile is not a function

i dont really know what is the difference and how to use that variable from console than the variable of script. specially that i dont redefine it in console after running the app..

some code snippets you may need, to help me solving this problem!...

i am calling two scripts in `index.html'

<!--requirejs -->
<script data-main="app" src="node_modules/requirejs/require.js"></script>
<!-- a script which will use $ which is not defined -->
<script src="services/specifyDocType.js"></script>
<!--the script the call require and have the jsonfile variable -->
<script src="jsonfile-test.js"></script>

app.js code:

requirejs.config({
  //"baseUrl": "node_modules",
  "paths": {
    "app": "app",
    "jsonfile": "jsonfile/index",
    "graceful-fs": "jsonfile/node_modules/graceful-fs/graceful-fs",
    "fs": "/node_modules/jsonfile/node_modules/graceful-fs/fs",
    "jquery": "node_modules/jquery/dist/jquery.min"
    }
});

requirejs(["dist/js/main"]);

main.js :

define("jquery", function($) {
  $('body');
});

for the local script that will create jsonfile variable, here is the initial code i had to do the working test:

var data = 'DOCS/voucher/voucher.json';
var jsonfile = require(["node_modules/jsonfile/index"]);

console.log(jsonfile);
//this console that returns the function localRequire() mentioned above.

jsonfile.readFile(data, function(err, obj) {
    console.dir(obj);
});

i need to solve this problem as soon as possible to finish working on this simple app. there for i dont need to use a framework such as angularjs, and i need to use require for each module i am using so far to create unique ids and directories in the root directory ..

please HELP! thanks in advance =D

one more thing this is my console on the first run:

function localRequire()  jsonfile-test.js:4:1
TypeError: jsonfile.readFile is not a function[Learn More]  jsonfile-test.js:6:1
ReferenceError: module is not defined[Learn More]  index.js:133:1
Error: Module name "fs" has not been loaded yet for context: _. Use require([]) http://requirejs.org/docs/errors.html#notloaded  require.js:168:17
Use of getPreventDefault() is deprecated.  Use defaultPrevented instead.



via Mutaz alHawash

No comments:

Post a Comment