0

Hello is it possible to run ES6 module function which is using axios to call API as package.json script using node v12 ?

I was trying to use package like run-func and turning file into .mjs but it didn't work.

I would like to have something like that:

"scripts": {
  "mocks:enable": "run-func mocks.js enableMocks"
}

Function while create api and myApi lives in another file

// instance.js
import axios from "axios";

createApiInstance = (baseUrl) => {
  const api = axios.create({
    baseURL: baseUrl,
  });

  return api;
};

export const myApi = createApiInstance(url)

//mocks.js
export function enableMocks() {
  return myApi.post("mocks", STATUS.enabled);
}
Verthon
  • 2,937
  • 3
  • 20
  • 32
  • 1
    you can run any js file using node (which you have installed) using: `node file.js` – MauriceNino Jun 28 '21 at 11:13
  • 1
    Here is what I got => import { myApi } from "@/api/instances"; ^^^^^^ SyntaxError: Cannot use import statement outside a module – Verthon Jun 28 '21 at 11:16
  • Adding --experimental-modules in the end of command doesnt help either – Verthon Jun 28 '21 at 11:17
  • I would suggest you updating your node version. If you can't do that, look here to find a solution for your problem: https://stackoverflow.com/questions/58384179/syntaxerror-cannot-use-import-statement-outside-a-module – MauriceNino Jun 28 '21 at 14:00

0 Answers0