Tips for Resolving SyntaxError: Unexpected token = When Class Import is Done

After creating a class in a separate JavaScript file and importing it into another file, I encountered a syntax error within the exported class.

All seemed fine when the class was not being exported.

Here is the code for Property.js (the exported class):

class Property{

    //constants declarations
    YEAR_AMOUNT = 10;
    PERIOD_AMOUNT = 120;

    //Properties
    closingCosts;
    initialInvestment;

    //arrays declarations
    //income
    adjustedRent= new Array();
    adjustedOtherRevenue = new Array();
    grossRevenue= new Array();
    effectiveRevenue= new Array();

constructor(title, address, askPrice, muniLandEval, muniBuildEval, notes){
 this.title=title;    
    this.address=address;
    this.askPrice=askPrice;
    this.muniLandVal = muniLandEval;
    this.muniBuildVal = muniBuildEval;
    this.notes=notes
}

//other methods
}

export default class Property

And here's how it is imported into propertyManager.js:

//handle property (calculations, creation, save)
const express = require('express');
const router  = express.Router();
const Property = require('./Property.js');

router.post('/', (req, res) => {

    //create a Property
    const property = new Property(req.body.title,
        req.body.address,
        req.body.askPrice,
        req.body.muniLandVal,
        req.body.muniBuildVal,
        req.body.notes
    );
})

module.exports = router;

The issue causing the error is as follows:

D:\ImmoTuul\RE_Analysis_\RE_MVP\server\routes\api\Property.js:4
    YEAR_AMOUNT = 10;
                ^

SyntaxError: Unexpected token =
    at Module._compile (internal/modules/cjs/loader.js:723:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (D:\ImmoTuul\RE_Analysis_\RE_MVP\server\routes\api\propertyManager.js:4:18)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)

To resolve this issue, I need the ability to use the class and its constructor in the propertyManager file.

Answer №1

It appears that there is an error in your Property.js file. You have included an extra class keyword.

export default class Property; /// The presence of 'class' here is incorrect

export default Property; // This should be the correct syntax

Unless you are exporting and declaring in the same line (which is not the case in your code)

Answer №2

Thanks to the helpful guidance from @Valijon, I was able to resolve this issue with the use of

export default Property;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Choose a single conditional element by using the .each method in JavaScript

I am looking to choose just one article that meets a specific condition. In my code, I am using hourDiff==0 as the condition to be true. I am using the 'each' function to loop through the articles. I have set display:none for all articles in the ...

Username not found error during authentication while registering on Node/Express

After following a tutorial on using Express for authentication with Passport and JWT, I encountered an error when creating a route for user registration. https://i.sstatic.net/3ZB8a.png In my authentication middleware, I have the following setup: import ...

I am experiencing an issue where the position of the value in the returned response array keeps changing after an Ajax request is made. How can

I am currently using a script that sends an array of numbers through Ajax to a web server. These numbers are then used to query a database, and the corresponding values from the table are sent back. I then display these values in respective divs within my ...

Finding the nearest ancestor with a specific class using JavaScript

Is it possible to find the closest ancestor by class using a partial class name? For example: <div class="parent-1"> <div class="child"></div> </div> I'd like to achieve something like this: document.get ...

What is the best way to utilize Node.js to serve a single page HTML file on port 80?

This is how my project structure is organized: project assets/images/ css/application.css js/application.js font node_modules index.html server.js package.json I am aiming to run 'node server.js" in the project directory and have it serve on port ...

Looking for an alternative method to compare strings in node.js without causing a call stack crash with if-else statements

I am encountering an issue with a large list of strings that need to be compared to set a variable if a match is found. I am attempting to do this within a node.js express app. However, when I try to run this comparison within an if/else statement in a f ...

Preventing Scope Problems in AngularJS

Spending more than 4 hours trying to figure out why my ng-model directive was not correctly binding with ng-options within my controller was frustrating. The <select> element seemed to be initialized properly, receiving a value from the parent scope, ...

How to use ng-click to upload images with Multer without a form?

I have successfully implemented multer code to upload a file. However, I am looking to enhance it by using ng-click() to send additional data from my controller via $http post. A simple input field with a button would be ideal. Here is the HTML side: < ...

Discover siblings in React component siblings

Creating a parent element (Board) that generates a list of children and provides a method to access this list can be done like so: export default class Board extends React.Component { constructor(props) { super(props); this.getList = t ...

Ajax fails to transmit information

Currently, I am in the process of familiarizing myself with the usage of ajax. An issue that I am encountering is that clicking a submit button in a form does not effectively send data. Below you can find the JQuery code I am using: $('input[name=" ...

The issue of Ejs partial header and footer file only functioning in one file and not the other (both at the same directory level)

I'm in the process of setting up a view page for a post on the main page of a website. Both pages share the same header and footer files, located at the same directory level. However, while one page is functioning correctly, the other is not. The erro ...

Finally, $.getJSON has been triggered

When I click on an element, I want to open a bootstrap modal. The modal contains values retrieved from an ajax call using getJSON. However, the issue is that the getJSON function is only called after the jQuery function has finished executing. Below is my ...

I'm having trouble getting Socket.io and module.exports to work in tandem. Any solutions to this issue?

Recently, I've been diving into Node.js and stumbled upon the concept of module.exports. It appears to be a useful way to maintain clean and organized code by separating different functionalities. After experimenting with a few examples, I managed to ...

Tips for activating a click event on a changing element within a Vue.js application

I am working on creating dynamically generated tabs with a specific range of time (from 8am to 9am). My goal is to automatically trigger a click event when the current time falls within this range. However, I am facing an issue where the ref is being ident ...

How can I change the text of a single button by clicking on it without affecting the other buttons that share the same

Currently, I am implementing a posting system where posts can be added using a button. The posts have two additional buttons - one to show/hide content and the other to delete content. I am utilizing jQuery's slideToggle event to toggle the visibility ...

Tips for managing nested objects within React state and keeping them updated

Could someone kindly point out where I might be going wrong with this code? Upon checking the console.log, it seems like the date function is functioning correctly. However, despite using this.setState, the timestamp remains unchanged. Any help would be gr ...

Difficulty accessing class functions from the test application in Node.js NPM and Typescript

I created an NPM package to easily reuse a class. The package installs correctly and I can load the class, but unfortunately I am unable to access functions within the class. My project is built using TypeScript which compiles into a JavaScript class: For ...

Tips for evaluating an array of objects in JavaScript

Welcome to the world of coding! Here's a scenario with an array: [ { "question1": "Apple", "question2": 5, "question3": "Item 1" }, { "question1": ...

Error Alert: Invalid type specified in React.createElement - Electron-React-Boilerplate

As a designer looking to expand my skills into coding for personal projects, I decided to start with the navigation UI in the [electron-react-boilerplate][1] that I cloned. However, I encountered the following error message: Error Warning: React.createEle ...

Is it possible in Javascript to trace the origins of a particular element's property inheritance for debugging purposes?

I'm currently dealing with an issue where the computed style font-size of a particular element is "16px". I've been attempting to pinpoint where in the CSS or JavaScript this font size setting is coming from, specifically within one of its parent ...