Thursday, 17 September 2020

Why can't I use import/export in node.js enviroment?

Hi everybody, I'm working in my first node.js app coming from a vue/laravel background where I alsways used import/exports, now I'm trying to use it in my node.js but I get the following error:``` (node:13072) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension. C:\xampp\htdocs\myapp\app.js:7 import MapsService from '../services/MapsService'; ^SyntaxError: Cannot use import statement outside a module ``` I thought node.js was pretty common, how can I not use import/exports? This is what I have in my app.js``` var express = require('express'); var app = express(); //process.env.DB_HOST require('dotenv').config()import MapsService from '../services/MapsService';app.get('/', async (req, res) => {MapsService.scrapeAddress(); res.send('Hello World!'); });app.listen(3000, function () { console.log('Example app listening on port 3000!'); }); ```And in my MapsService file I use imports/exports too, does that mean I will have to use this require thing I see? How can I transform my MapsService file to be require friendly?``` //import { Client } from '@googlemaps/google-maps-services-js'; import * as geometry from 'spherical-geometry-js'; import axios from "axios"; //const axios = require('axios').default ??; import VenuesService from '../services/VenuesService';export class MapsService {} ```

Submitted September 17, 2020 at 05:38PM by Gabotron_ES

No comments:

Post a Comment