0

I am learning automation for work and currently a little stuck. So far stackoverflow has been a life saver :)

I am writing a test for selenium in visualstudio in javascript (nodes). I understand this is not a great combination but thats what work wants.

I have a test in the app.js file (see screenshot). It references a function in the functions.js file. I cannot get it to recognise the function though. I presume I need to reference the files containing the function. I have tried import 'import cellFromXLS from "functions.js";' and it does not work(Unexpected token import error).

Any ideas on what I can do? Anything fancy like modifying the package.json file to include all files with functions in them? I am on the latest node.js and the latest drivers.

Also it seems intellisense does not work for javascript in visual studio. Is this right or anyway to fix it? VisualStudio screenshot

Endorium
  • 15
  • 9
  • 2
    Possible duplicate of [Node error: SyntaxError: Unexpected token import](https://stackoverflow.com/questions/37634198/node-error-syntaxerror-unexpected-token-import) – Hitmands Jul 26 '17 at 08:48
  • In functions.js: cellFromXLS = function (cellId) { //Define sheetNumber var sheetNumber = 0; //Define file Path name var fileNamePath = ('trial.xls'); //NodeJs read file var XLS; if (typeof require !== 'undefined') { XLS = require('xlsjs'); } //Working with workbook var workbook = XLS.readFile(fileNamePath); var sheetNamelist = workbook.SheetNames; var value = workbook.Sheets[sheetNamelist[sheetNumber]][cellId].v; return value; }; module.exports = { cellFromXLS } – Endorium Jul 26 '17 at 09:15
  • In apps.js: const functions = require("./functions.js"); functions.cellFromXLS('A1'); – Endorium Jul 26 '17 at 09:16

2 Answers2

0

Node doesn't support import natively just yet.

In your functions file you can do something like

function blah(){
    console.log("I am blah")
}

function wah(){
    console.log("Wah wah")
}

module.exports = {
    blah,
    wah
}

then in app.js you can do:

const functions = require('./functions.js')
functions.blah()
functions.wah()
wafs
  • 2,836
  • 2
  • 17
  • 12
  • I have tried the above but I get a messge saying my function is not defined? I put the export lines in functions.js and the 'const' line in my app.js – Endorium Jul 26 '17 at 09:02
  • http://i.imgur.com/OiRefD7.png Here is a screenshot of it in action. Is what you had close to this? – wafs Jul 26 '17 at 09:22
  • Not sure then, it's working in my instance and screenshot. Oh well hopefully you get to resolve it. – wafs Jul 26 '17 at 09:36
  • 1
    started from scratch and got it working with your method :) Thank you very much. Dont think I can up vote yet but really appreciate you help – Endorium Jul 26 '17 at 09:44
0

The error already tells you what's wrong. You need to use

const cellFromXLS = require("./functions.js");

instead of

import cellFromXLS from "functions.js"

if you want to use the import syntax, check out Babel

Anditthas
  • 531
  • 1
  • 3
  • 11
  • ok, I dont want to use babel as its extra thing the business has to validate (Very heavily audited industry). I thought nodejs now supported import from version 8? I wil ltry the other way as you suggested. – Endorium Jul 26 '17 at 08:56