0

So, I was trying to create a simple web scraper to extract data from Wikipedia using Node.js, and I used Axios & JSDOM to do it.

When I tried to run it, this was the message I received on my Terminal:

Timothy@Arthurs-MacBook-Air-2 ~ % node scraper.js
/Users/Timothy/scraper.js:1
import axios from 'axios';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47

For reference, I'm using Visual Studio Code on macOS Big Sur, and this is the full reference code for my web scraper (in case it's necessary):

import axios from 'axios';
let jsdom;

jsdom = require('jsdom');

({ jsdom } = jsdom);

axios
    .get('https://en.wikipedia.org/wiki/Fauna_of_Scotland');
    .then(function ({ data: html }) {
            const { document } = new jsdom(html).window;
            const nickname = document.querySelector('.mammal');
            if (nickname)
                console.log(nickname.textContent);
        });
    .catch(e => {
        console.log(e)
    });

The Axios documentation doesn't seem to provide any guidance on these error messages, and neither does the JSDOM readme either.

I'd love some help on how to remove the error message, please, as well as where to look further into the matter! Thanks!

istionia
  • 5
  • 4

1 Answers1

0

i don't have enough reputation to comment

seems the problem is not with axios, check if this can solve your problem: "Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6

1sina1
  • 937
  • 7
  • 11
  • Thanks for the tip! Seems that this may not be the full solution. Where do you think I should place the type="module" in my code? (since that's what the top answers are telling me) – istionia Jan 24 '22 at 17:57
  • add it to your package.json file. – 1sina1 Jan 24 '22 at 18:10