2

My config class: ProjectDir/classes/config.js

'use strict';
class config{
    getMongo(){
        var MongoClient = require('mongodb').MongoClient;
        MongoClient.connect('mongodb://127.0.0.1:27017/nodedb', (err, database) => {
            var db;
            if (err) return console.log(err)
            db = database;
            console.log('Connected to mongo');
        });
    }
}

module.exports = config;

My server.js file(here all routes are defined): ProjectDir/server.js

console.log('May Node be with you');
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));

var configClass = require('./classes/config');//this is line 18
var config = new configClass();

Error:

SyntaxError: Invalid or unexpected token
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:588:28)
    at Object.Module._extensions..js (module.js:635:10)
    at Module.load (module.js:545:32)
    at tryModuleLoad (module.js:508:12)
    at Function.Module._load (module.js:500:3)
    at Module.require (module.js:568:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (D:\node\server.js:18:19)
[nodemon] app crashed - waiting for file changes before starting...

With out nodemon:

    >node server.js
    May Node be with you
D:\node\classes\config.js:1
(function (exports, require, module, __filename, __dirname) { ��'
    SyntaxError: Invalid or unexpected token
        at createScript (vm.js:80:10)
        at Object.runInThisContext (vm.js:139:10)
        at Module._compile (module.js:588:28)
        at Object.Module._extensions..js (module.js:635:
        at Module.load (module.js:545:32)
        at tryModuleLoad (module.js:508:12)
        at Function.Module._load (module.js:500:3)
        at Module.require (module.js:568:17)
        at require (internal/module.js:11:18)
        at Object.<anonymous> (D:\node\server.js:18:19)

Package.json

{
  "name": "node",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "nodemon server.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.18.2",
    "ejs": "^2.5.7",
    "express": "^4.16.2",
    "mongodb": "^2.2.33",
    "node-uuid": "^1.4.8",
    "swagger-tools": "^0.10.3"
  },
  "devDependencies": {
    "nodemon": "^1.12.1"
  }
}

I am new to node js, i am trying to make my code modular so i create a class called config.js i want to use this class in my index file where all of my routes are written. Please help!!

28
  • **line 18:** ?? why this in your code?? Commented Nov 10, 2017 at 15:05
  • 1
    @wrangler I'm assuming he's pointing out that it's line 18 which is where the error is coming from maybe? Commented Nov 10, 2017 at 15:06
  • 1
    What about this, May Node be with you D:\node\classes\config.js:1 (function (exports, require, module, __filename, __dirname) { ��' Commented Nov 10, 2017 at 15:32
  • 1
    Ok, I can see a little space at the beginning of your config.js file. Is it the result of copy-pasting or do you have it in the actual file? If you do, then remove it, so the file starts with 'use strict';. Check all your files. I'm saying it because this (function (exports, require, module, __filename, __dirname) { ��' looks suspicious Commented Nov 10, 2017 at 15:43
  • 1
    This looks not like space, but some bytes that are invalid in the used encoding. Try another editor. Commented Nov 10, 2017 at 16:00

1 Answer 1

0

Try this in server.js instead (not tested)

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));

var { config } = require('./classes/config');//this is line 18
var config = new config();
Sign up to request clarification or add additional context in comments.

4 Comments

Its soo frustrating.. same issue. when i comment line 18 it works perfectly fine
so try old fashion way instead with prototype. I must admit am new to es6 features
Yeah i went through this before posting this question.

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.