1

I have just started working on node.js and I am unable to import tensorlfow js models into my application. The code I am using is:

import * as cocoSsd from '@tensorflow-models/coco-ssd'
const model = cocoSsd.load();

After running the above code I get the following error:

import * as cocoSsd from '@tensorflow-models/coco-ssd'
       ^

SyntaxError: Unexpected token *
    at Module._compile (internal/modules/cjs/loader.js:721:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787: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 Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
  • Which version of node are you currently on? – Yury Tarabanko Jun 27 '19 at 11:33
  • 1
    Node has experimental es modules support. You might need a [flag](https://nodejs.org/api/esm.html) or use commonjs "import" format `const cocoSsd = require('@tensorflow-models/coco-ssd')` – Yury Tarabanko Jun 27 '19 at 11:37
  • @YuryTarabanko I am using v10.16.0 – Syed Nauyan Rashid Jun 27 '19 at 11:49
  • Try to use commonjs format as I have written before. node 10 esm support was quite experimental. Even with the current stable version it is still behind the `--experimental-modules` flag and requires some additional ceremony to work (`.mjs` extension or `type` field in package.json) `const cocoSsd = require('@tensorflow-models/coco-ssd')` should work – Yury Tarabanko Jun 27 '19 at 12:26
  • Now I am getting the error: Error: Cannot find module '@tensorflow-models/coco-ssd' – Syed Nauyan Rashid Jun 27 '19 at 12:39
  • 1
    Maybe you need to first install `tensorflow-models/coco-ssd`. Can you add your package.json to your question ? – edkeveked Jun 27 '19 at 14:08

1 Answers1

1

How about installing babel as a dev dependency

https://medium.com/@onlykiosk/complete-babel-7-guide-for-beginners-in-2019-7dd78214c464

Check out that post on what babel is and how it works