1

Code runs without errors when I require compared to when I import, then I receive an error: va

app.js:

// require("@babel/polyfill");
// require("@babel/core");
import babel from '@babel/core';
import babelpoly from '@babel/polyfill';
import axios from 'axios';

const BASE_URL = 'https://jsonplaceholder.typicode.com';

const getTodos = async () => {
  try {
    const res = await axios.get(`${BASE_URL}/todos`);

    const todos = res.data;

    console.log(`GET: Here's the list of todos`, todos);

    return todos;
  } catch (e) {
    console.error(e);
  }
};

Based on what I understand, require and import have the same functionality but they have performance trade offs. I must be wrong, otherwise it would've worked both ways

David
  • 159
  • 1
  • 14
  • 1
    Does this answer your question? [Using Node.js require vs. ES6 import/export](https://stackoverflow.com/questions/31354559/using-node-js-require-vs-es6-import-export) – caladeve Feb 09 '20 at 20:54
  • Try importing without the variable names, just the file. – Gaurav Punjabi Feb 09 '20 at 21:02
  • @caladeve I did check it out before, but I was not sure. Is it because Node (its engine) does not support ES6 modules? It still confuses me because that is a client side application, not server side. So the javascript should not be running on the backend--in the accepted answer it is said that no javascript engine exists that supports ES6 modules, I assume it refers to runtime enviroments such as Node, but can on the browser, otherwise why would it exist if it cannot be used anywhere (`import` does work sometimes) – David Feb 10 '20 at 17:34

1 Answers1

-1

import is an ES6 feature and needs babel to convert import to Common JS require. You should require babel to use import.

Veysi YILDIZ
  • 99
  • 1
  • 2