1

I try to move some actions into a class and import it to my main route.

I created a simple class

./ops/standardactions.js

export default class StandardActions {
    constructor () {}

    async today () {
        var today =  new Date();
        var date = today.getDate()+'-'+(today.getMonth()+1)+'-'+today.getFullYear();
        return date;
    }
}

Then I try to import this class into the route. The route looks like:

const express = require('express');
const router = express.Router();
var bodyParser = require('body-parser');
const fs = require('fs');
const path = require('path');
const uuidv1 = require('uuid/v1');
const BrowserStack = require("testcafe-browser-provider-browserstack");
const createTestCafe = require('testcafe');
var allure = require('allure-commandline');
const { ensureAuthenticated, forwardAuthenticated } = require('../config/auth');

const {default: PQueue} = require('p-queue');
const spooler = new PQueue({concurrency: 1});

const Logs = require('../models/log');


import StandardActions from '../ops/standardactions.js';
const repeatFunctions = new StandardActions();

import AllureHelper from '../ops/allurehelper.js';
const allureFunctions = new AllureHelper();

But I get always on import the error:

/Users/ingofoerster/Downloads/development/clipcafe2/routes/spoolertests.js:18
import StandardActions from '../ops/standardactions.js';
       ^^^^^^^^^^^^^^^

SyntaxError: Unexpected identifier
    at Module._compile (internal/modules/cjs/loader.js:760:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
    at Module.load (internal/modules/cjs/loader.js:685:32)
    at Function.Module._load (internal/modules/cjs/loader.js:620:12)
    at Module.require (internal/modules/cjs/loader.js:723:19)
    at require (internal/modules/cjs/helpers.js:14:16)
    at Object.<anonymous> (/Users/ingofoerster/Downloads/development/clipcafe2/app.js:105:26)
    at Module._compile (internal/modules/cjs/loader.js:816:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
    at Module.load (internal/modules/cjs/loader.js:685:32)
    at Function.Module._load (internal/modules/cjs/loader.js:620:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:877:12)
    at internal/main/run_main_module.js:21:11
npm ERR! code ELIFECYCLE

What is wrong with this way? Iam very confused. Hope someone can help me out.

ingo
  • 776
  • 1
  • 10
  • 25
  • Depending on your node version, it won't support modules without an experimental flag. Non-mjs supporting node versions use CommonJS, so you'll need to use `module.exports`. See the [modules docs](https://nodejs.org/api/esm.html). – Phix Mar 02 '20 at 23:10
  • Node version? its module.exports={} and require("./xyz") – Ajjo Mar 02 '20 at 23:21

0 Answers0