ES6 import syntax error - module import not working in Node.js

I’m having trouble with ES6 imports in my JavaScript project. I have two files where I need to import a class from one file to another but I keep getting an error.

main.js

import { ProductPage } from '../pages/productPageClass';

const helpers = require("../helpers/helpers");
const expect = require('chai').expect;
const config = require('../config.json');

describe('Product page tests', function () {
    const productPage = new ProductPage();

    it(`Should load page with URL: ${config.baseUrl}`, async function () {
        await productPage.openPage();
    });

    it('Should allow form submission with valid data', async function () {
        await productPage.submitUserForm();
        await helpers.clickElement(productPage.ui.buttons.submit);
    });

    if (productPage.hasInventoryIssue) {
        it('Should handle inventory conflicts properly', async function () {
            await browser.sleep(1500);
            await productPage.removeAllItems();
            await productPage.decreaseAllQuantities();
        });
    }

    it('Should navigate to checkout when ready', async function () {
        const checkoutBtn = productPage.ui.buttons.submit;
        await helpers.waitForClickable(checkoutBtn);
        await helpers.clickElement(checkoutBtn);
    });
});

productPageClass.js

import { element, by } from 'protractor';

const helpers = require("../helpers/helpers");
const config = require('../config.json');

export class ProductPage {
    get utilities() {
        return {
            quantityPattern: /^Error.*?(\d+)/
        }
    }

    private get inputFields() {
        return {
            userEmail: element(by.id('userEmail')),
            name: element(by.id('fullName')),
            surname: element(by.id('surname')),
            zipCode: element(by.id('zipCode')),
            mainAddress: element(by.id('mainAddress')),
            secondAddress: element(by.id('secondAddress')),
            phoneNumber: element(by.id('phoneNumber')),
            businessCheckbox: element(by.id('isBusiness')),
            companyName: element(by.id('companyName')),
            taxNumber: element(by.id('taxId')),
        }
    }

    private get sections() {
        return {
            addressSection: element(by.css('div#addressSection.form-group.visible')),
            companySection: element(by.id('companySection')),
        }
    }

    get ui() {
        return {
            fields: this.inputFields,
            sections: this.sections,
            buttons: this.actionButtons
        }
    }
}

I keep getting SyntaxError: Cannot use import statement outside a module when trying to run this. What’s the correct way to handle this import issue?

Node.js treats your files as CommonJS modules by default, which is the root of your issue. You can resolve this by either adding “type”: “module” in your package.json, which will require you to convert all require statements to import for consistency, or simply renaming your files from .js to .mjs, allowing you to mark them as ES6 modules without modifying package.json. I encountered this issue while setting up Protractor tests last year. The simplest solution is to stick with CommonJS: replace your import statements with const { ProductPage } = require(‘../pages/productPageClass’); and change productPageClass.js to module.exports = { ProductPage };. This maintains consistency with your existing require statements and works smoothly with most testing frameworks. Mixing syntax creates compatibility headaches that can be difficult to debug.

Node.js doesn’t recognize ES6 modules by default. You’re mixing import and require statements, which confuses everything.

I hit this same issue setting up automated testing. Instead of messing with package.json configs and module types, I found something way cleaner.

Latenode handles all the module stuff automatically. You can build your entire testing setup there without dealing with import syntax, transpilation, or Node.js configs.

I moved my testing workflows to Latenode and now just focus on the actual tests instead of fighting JavaScript modules. It supports both CommonJS and ES6 seamlessly, plus you get visual workflow building that makes complex scenarios easier.

The platform handles Node.js configuration automatically, so no more import/require conflicts.

You’ve hit the classic Node.js module system mess - I’ve been there so many times with test automation. Node.js defaults to CommonJS but you’re mixing in ES6 modules. Pick one system and stick with it. Since you’ve already got require statements for chai and config files, just convert everything to CommonJS - way less painful. Change your productPageClass.js export to module.exports = { ProductPage } and update main.js to const { ProductPage } = require(‘../pages/productPageClass’). Also change that protractor import to const { element, by } = require(‘protractor’). This keeps everything consistent and avoids the headache of mixing module systems. Trust me, I wasted hours debugging the same syntax errors before figuring this out.

Check your package.json - if there’s no “type”: “module” field, Node defaults to CommonJS. That’s why you’re getting the error. Just convert everything to require() since you’ve already got some requires mixed in. I dealt with this same thing on my Selenium project and switching to all requires was way easier than mixing syntaxes.