Let's say I have a code.js file with the following node.js script:
const axios = require('axios')
async function getData(){
const response = await axios.get('https://mypage.com.br')
console.log(response.data)
}
getData()
If I execute it with node code.js it works perfectly fine... However, I'd like to execute it as a module, just so I can use the import statement and use the await command as top level. I'd like to accomplish that without creating a project with a package.json file. My final result would be something like this:
import axios from 'axios'
const response = await axios.get('https://mypage.com.br')
console.log(response.data)
I haven't managed to make it work with the node command. I know there's a --input-type=module parameter I can use with it. But I've tried running node --input-type=module code.js and I've received the following error:
SyntaxError: Cannot use import statement outside a module
So, that means it's not even being recognized as a module yet. Is it possible to do? Can I execute an isolated script with the command node as a module (while using await on top level)?