0

I'm having an issue utilizing an external jQuery file with node. I have several functions that I need to run but it isn't working. I created a simple function to test but the error message I keep getting is TypeError: Object # has no method 'alert_helloworld'. Here are my two files:

example.js

var jsdom = require('jsdom');
var templateparser = require('./templateparser.js');
var test = templateparser.alert_helloworld();

templateparser.js

function alert_helloworld(){
console.log("Hello World");
return false;
}

HELP!!!!

2 Answers 2

1

You need to use the exports object in templateparser.js:

exports = exports || {};

exports.alert_helloworld = function(){
    console.log("Hello World");
    return false;
}

Take a look at the modules docs: http://nodejs.org/docs/latest/api/modules.html

Sign up to request clarification or add additional context in comments.

Comments

0
module.exports.alert_helloworld = function alert_helloworld(){
    console.log("Hello World");
    return false;
};

It's important you actually export the functions you need. node.js modules by default are wrapped in their own isolated module scope.

3 Comments

I'm not looking to reinvent my external file. I want to keep it the same. Is this even possible?
@user1146581 yes/no. You can inject it as a javascript file into jsdom or you can put it through an AST parser in node. Otherwise no, you can't access "global" variables that you define in your file.
So I guess I'm asking, how do I inject it as javascript into jsdom?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.