SyntaxError: Unexpected token *


#1

Hi all
I know, it exists an axios bs version but I will try to implement by myself.
I have the following code:

[@bs.deriving abstract]
type data = {
  coinSymbol: string,
  amount: int,
};

[@bs.deriving abstract]
type config = {
  headers: Js.Dict.t(string)
};

[@bs.module "axios"]
external post :
  (~url: string, ~data: data, ~config: config) => Js.Promise.t('a) =
  "";

let d = data(~coinSymbol="BTC", ~amount=22); 

let dict = Js.Dict.empty();
Js.Dict.set(dict, "Content-Type", "application/json");
let c = config(~headers=dict);

let reqData = post(~url="http://localhost:3000/test", ~data=d, ~config= c);

and the compiler complains:

import * as Axios from "axios";
       ^

SyntaxError: Unexpected token *
    at new Script (vm.js:74:7)
    at createScript (vm.js:246:10)
    at Object.runInThisContext (vm.js:298:10)
    at Module._compile (internal/modules/cjs/loader.js:670:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)

package.json file:

{
  "name": "interoperate",
  "version": "0.1.0",
  "scripts": {
    "build": "bsb -make-world",
    "start": "bsb -make-world -w",
    "clean": "bsb -clean-world"
  },
  "keywords": [
    "BuckleScript"
  ],
  "author": "",
  "license": "MIT",
  "devDependencies": {
    "bs-platform": "^3.1.5"
  },
  "dependencies": {
    "axios": "^0.18.0"
  }
}

Thanks


#2

How do you run generated javascript code?


#3

I changed in bsconfig from es6 into commonjs and it work.
I generated JS with following command:

"mlstart": "bsb -make-world -w",

Did I something wrong?


#4

es6 should be used when you want to run in browser. NodeJS environment supports ‘commonjs’ module type.


#5

To clarify, this is using a new syntax for importing module as part of ES6. Depending on where you’re running (an old version of nodejs or browser) it might not be supported.
Your bsconfig.json probably contains something to the effect of "package-specs": { "module": "es6-global" }, which you can change to "package-specs": { "module": "commonjs", "in-source": true } to compile the import statements into more supported require calls.
That or if you’re going to need to bundle the whole codebase into one JS file for other reasons then you can just run rollup and you’ll get a pile of js without import statements.