0

I am following the code from here which doesn't mention any special requirements apart from Node.js being version 8.9 or higher. I am new to javascript and I am unsure as to why the import function does not work unless there are additional requirements not specified in the link.

I am getting a syntax error when I try to use 'import' in javascript:

import * as tf from '@tensorflow/tfjs'
import {generateData} from './data';

which returns the error:

SyntaxError: Unexpected token import
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:188:16)
    at bootstrap_node.js:609:3

I know that in older versions there used to be problems with the import but I am using the up to date versions for node, npm, and yarn so I am not sure why I am getting this error. The snippet of code that I am running on says that it requires node.js of version 8.9 or higher (which is satisfied) as well as either npm or yarn.

node -v
v8.10.0

npm -v
6.1.0

yarn -v
1.7.0

Also as of 2017, the import function should work in ES6 with node.js.

piccolo
  • 2,093
  • 3
  • 24
  • 56
  • @Hector I am quite new to javascript but the link that I was following https://js.tensorflow.org/tutorials/fit-curve.html, didn't mention need for an experimental-modules flag – piccolo Jul 15 '18 at 01:19
  • It can be a bit difficult to find information on it, but take a look at the following as well: https://stackoverflow.com/questions/37132031/nodejs-plans-to-support-import-export-es6-es2015-modules ...it appears it may be a bit of a wait? – hcs Jul 15 '18 at 01:23
  • @AndyGaskell it is actually in the $ git clone https://github.com/tensorflow/tfjs-examples repository. There is actually a line before it `import * as tf from '@tensorflow/tfjs'` that also doesn't work. for the same reason The json file is called index.js and is found within the the polynomial-regression-core folder in the github directory – piccolo Jul 15 '18 at 01:28
  • @Hector According to this https://medium.com/the-node-js-collection/an-update-on-es6-modules-in-node-js-42c958b890c , the import function should work now – piccolo Jul 15 '18 at 01:50
  • 1
    @ImranRashid Read this: https://www.linkedin.com/pulse/using-es6-babel-nodejs-harshal-yeole/ – Harshal Yeole Jul 15 '18 at 04:26
  • Thanks @Jack that was useful. – piccolo Jul 15 '18 at 12:40

0 Answers0