Home > Back-end >  Axios in NodeJs, " Cannot use import statement outside a module"
Axios in NodeJs, " Cannot use import statement outside a module"

Time:11-12

I'm trying to call axios from a minimal NodeJs app. I'm new to JS, so sorry for the maybe dump question ...

I already checked this SO answer, which doesn't provide a solution for my problem.

The problem, when calling node:

node .\app.js
C:\Users\user01\scraping02\node_modules\axios\index.js:1
import axios from './lib/axios.js';
^^^^^^

SyntaxError: Cannot use import statement outside a module

My app.js:

const axios = require("axios").default;

console.log("Test");

Axios version: "1.1.3". Node version: v12.14.0.

Any hints?

Thx

CodePudding user response:

Upgrade Node.

I've tested in v12.14.0, v12.22.10, and v18.7.0.

It fails with the error you get only in the first of those.

Additionally, Node v12 passed end of life in 2019 and no longer receives security fixes.

CodePudding user response:

In the package.json file add "type" : "module", . Adding this enables ES6 modules.

  • Related