1

After several days i found a working project with mongo, vuejs, express and passport. https://github.com/searsaw/vue2-auth

This project was originally wrote for Babel 6.

I updated the dependencies in package.json in order to work with Babel 7 :

  "devDependencies": {
    "@babel/core": "^7.7.5",
    "@babel/plugin-syntax-dynamic-import": "^7.7.4",
    "@babel/plugin-transform-modules-commonjs": "^7.7.5",
    "@babel/plugin-transform-runtime": "^7.7.6",
    "@babel/plugin-transform-strict-mode": "^7.7.4",
    "@babel/preset-env": "^7.7.6",
    "@babel/preset-react": "^7.7.4",
    "@babel/runtime": "^7.7.6",
    "babelify": "^10.0.0",
    "browserify": "^16.5.0",
    "browserify-hmr": "^0.4.1",
    "concurrently": "^5.0.1",
    "eslint": "^6.7.2",
    "eslint-config-airbnb-base": "^14.0.0",
    "eslint-plugin-html": "^6.0.0",
    "eslint-plugin-import": "^2.19.1",
    "nodemon": "^2.0.2",
    "vue-hot-reload-api": "^2.3.4",
    "vueify": "^9.2.4",
    "watchify": "^3.11.1"
  },
  "dependencies": {
    "axios": "^0.19.0",
    "body-parser": "^1.19.0",
    "connect-mongo": "^3.2.0",
    "express": "^4.17.1",
    "express-session": "^1.17.0",
    "flash": "^1.1.0",
    "node-uuid": "^1.4.8",
    "passport": "^0.4.1",
    "passport-local": "^1.0.0",
    "pug": "^2.0.4",
    "vue": "^2.6.10"
  }

.babelrc :

{
  "presets": [
    ["@babel/preset-env", {
        "targets": {
          "ie": "11"
        }
      }]
  ],
  "plugins": [
    "@babel/plugin-transform-runtime"
  ]
}

When i type "npm run dev" i have an error :

Error: Parsing file
import axios from 'axios';
^ D:\xxx\src\exclamations_viewer.vue: 'import' and 'export' may only appear at the top level (31:0)

Below the vue code :

<style>
  .exclamations-viewer,
  .add-form-container {
    margin-top: 20px;
  }
</style>

<template>
  <div class="container">
    <div class="row add-form-container" v-if='canAdd()'>
      <div class="col-md-12">
        <Exclamation-Add-Form :onAdd='onExclamationAdded'></Exclamation-Add-Form>
      </div>
    </div>
    <div class="row exclamations-viewer">
      <div class="col-md-4">
        <Exclamation-List :user='user' :onRemove='onExclamationRemoved' title='All Exclamations' :exclamations='exclamations'></Exclamation-List>
      </div>
      <div class="col-md-4">
        <Exclamation-List :user='user' :onRemove='onExclamationRemoved' title='Your Exclamations' :exclamations='userExclamations'></Exclamation-List>
      </div>
      <div class="col-md-4">
        <Exclamation-Search-List :user='user' :onRemove='onExclamationRemoved' :exclamations='exclamations'></Exclamation-Search-List>
      </div>
    </div>
  </div>
</template>

<script>
  import axios from 'axios';  
  import ExclamationList from './exclamation_list.vue';
  import ExclamationSearchList from './exclamation_search_list.vue';
  import ExclamationAddForm from './exclamation_add_form.vue';

  export default {
    name: 'ExclamationsViewer',
    data: () => ({
      user: {
        scopes: [],
      },
      exclamations: [],
    }),
    beforeMount() {
      Promise.all([
        axios.get('/api/me'),
        axios.get('/api/exclamations'),
      ]).then(([{ data: meData }, { data: exclamationData }]) => {
        this.user = meData.user;
        this.exclamations = exclamationData.exclamations;
      });
    },
    methods: {
      onExclamationAdded(text) {
        axios.post('/api/exclamations', { text }).then(({ data }) => {
          this.exclamations = [data.exclamation].concat(this.exclamations);
        });
      },
      canAdd() {
        return this.user.scopes.includes('add');
      },
      onExclamationRemoved(id) {
        axios.delete(`/api/exclamations/${id}`)
          .then(() => {
            this.exclamations = this.exclamations.filter(e => e.id !== id);
          });
      },
    },
    computed: {
      userExclamations() {
        return this.exclamations.filter(exc => exc.user === this.user.username);
      },
    },
    components: {
      ExclamationList,
      ExclamationSearchList,
      ExclamationAddForm,
    },
  };
</script>

I think it is related to the old preset-es2015 but after spending one day i am out of options. Thank you for helping me.

Marcus Vilete
  • 210
  • 4
  • 8
kenran
  • 13
  • 3

0 Answers0