goodcow
goodcow

Reputation: 4795

NodeJS exports not defined in external JS file

I am calling a function from an external JS file in my routes file. So, I am trying to export a function from my externalFile.js file. However, when I run the node server, it throws Uncaught ReferenceError: exports is not defined . externalFile.js is in public/javascripts/ and the route files is in routes/.

External file (externalFile.js):

exports.capsolvingComplete = function (stdout){
    //Received, display text and hide the spinner, put check in place.
    $('#capsolv-complete').css("display", "block")
    $('#capsolv-output').text(stdout);
}

Route file:

var express = require('express');
var router = express.Router();
var script = require('../public/javascripts/externalFile.js')

/* GET home page. */
router.get('/', function(req, res) {
  res.render('index', { title: 'CapSolv' });
});

router.post('/file-upload', function(req, res){
    script.capsolvingComplete("HI");
    res.end("success");
})

module.exports = router;

Thank you for your help! Bit of a node noob here.

Upvotes: 2

Views: 332

Answers (1)

Nicholas Young
Nicholas Young

Reputation: 233

Looking at your example, mscdex is right. Anything you require in Node is designed to run on the server, and doesn't have access to the browser's DOM.

I would recommend building a REST API that accepts the file upload, stores it, and returns a JSON object containing metadata: i.e. where the file was stored, size, etc., and let your client deal with that.

Upvotes: 1

Related Questions