0

In my Node.js project I am trying to import a module of helper functions. I am getting this error:

/home/Projects/my_app/helpers.js:3
    var randomWeight = function(letters) {
        ^^^^^^^^^^^^ // <-- SyntaxError: Unexpected identifier with imported module Node.js
SyntaxError: Unexpected identifier

helpers.js:

module.exports = {

    function randomWeight (letters) {
        var total = letters.reduce(function (a, b) {
                        return a + b;
                    });
        var r = (Math.random() * (0 - total) + total.tofixed(5));
        var upto = 0;
        for (var i = 0; i<=letters.length; i++) {
            if ((upto + letters[i][0]) > r) {
                return letters[i][1];
            };
            upto += letters[i][0];
        };
    }

/routes/index.js:

var express = require('express');
var router = express.Router();
var logic = require('../logic.js');
console.log(logic.letterSet)

I have tried lots of different variations of the import statement, with result in the module being imported as an empty object. From searching SO it appears this is usually because of a circular import, but I am sure I'm not importing logic.js anywhere else in my project (specifically /server.js.) I am new to Node so troubleshooting this has been sort of like shooting in the dark.

EDIT:

I seem to have solved the problem by importing the appropriate functions individually, like:

exports.letterSet = letterSet;
exports.randomWeight = randomWeight; 

but I don't quite see how/why I can't import the whole module. I'm sorry if this seems like a ridiculous question but I am used to python where module imports are trivial.

4
  • instead of just posting a bunch of code, try and explain some things you've tried as well as some context for your code. Commented Jul 23, 2015 at 3:04
  • Your code doesn't match the error message. The helper.js code block is invalid. Maybe you meant module.exports = { randomWeight: function(letters) {...} }; Commented Jul 23, 2015 at 3:05
  • I think I see what's happening. I read that module.exports = {} was Node.js specific syntax for exporting a module, thinking I could wrap a module in it without it having to actually be a syntactially correct object...then again, having module.exports = function () {} results in the same error Commented Jul 23, 2015 at 3:11
  • 1
    try to use a lint tool, this can help you get rid of most of these error Commented Jul 23, 2015 at 3:57

1 Answer 1

1

you are returning an object {} in modules.exports, so you need to use object notation

module.exports = {

    randomWeight: function (letters) {
        var total = letters.reduce(function (a, b) {
                        return a + b;
                    });
        var r = (Math.random() * (0 - total) + total.tofixed(5));
        var upto = 0;
        for (var i = 0; i<=letters.length; i++) {
            if ((upto + letters[i][0]) > r) {
                return letters[i][1];
            };
            upto += letters[i][0];
        };
    }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.