M
M
MaxRumata2016-09-01 23:11:22
JavaScript
MaxRumata, 2016-09-01 23:11:22

Can't connect js files as commonjs modules (where is the error)?

Please help me find the error in the code.
We have:
test.json - contains the content (text) that will be loaded on the web page.
test.js - contains a function that sends an Ajax request to test.json, parses, compiles with Handelbars Temlate, and puts the content into an HTML page (using innerHTML).
addcontent.js - javascript file that calls the function from test.js
index.html - contains a Handlebars Template, a Div where the content will be placed and a link (link) to addcontent.js.
Everything works if index.html contains a link directly to test.js .
Everything works if you wrap the code in test.jsinto a function with variables and call it in the same file.
But when I try to call this function in addcontent.js , connecting addcontent.js and test.js as commonJS modules, it doesn't work (running a local server, I see a blank page at localhost:8080).
I don't see an error in the syntax.
Checked files with online validators. I checked it through the developer console, it swears at the
commonjs syntax that it does not understand: ReferenceError
: module is not defined
ReferenceError: require is not defined
(correct?) - the result is the same.
Please help me figure out this test case, I'm just learning the modular method.

PS I'm using NodeJS, NPM, I'll end up concatenating all js files with browserify.

https://jsfiddle.net/pf33u8km/ - here in the CSS window I put the code from test.json and addcontent.js , separated by a comment, just to make it easier to see the code in all files.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question