SyntaxError: Unexpected token *

Viewed 5857

I want to import code from the module BodyPix but I keep getting the following error message:

import * as bodyPix from '@tensorflow-models/body-pix';
       ^

SyntaxError: Unexpected token *
    at Module._compile (internal/modules/cjs/loader.js:718:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)
    at Module.load (internal/modules/cjs/loader.js:641:32)
    at Function.Module._load (internal/modules/cjs/loader.js:556:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:837:10)
    at internal/main/run_main_module.js:17:11

I've tried different ways of importing but nothing seems to work.

3 Answers

CommonJS - style import should work:

const bodyPix = require('@tensorflow-models/body-pix');

That's because current Node.js does not support import * as XXX syntax (at least in v10.15.3 on my computer).

To run this unsupported statement, you need to save the Node.js file with modules as .mjs extension and run it like:

node --experimental-modules index.mjs

Node.js does not support import in this syntax either save the file with modules as .mjs extension and run like this

node --experimental-modules index.mjs

as mentioned by shaochuancs OR another alternative is to use a transpiler. That converts modern JavaScript to older versions for you. Good options are TypeScript, Babel, and Rollup.

Related