javascriptvue.jswebpackvuejs2gridsome

TypeError when using Gridsome while building a Vue.js project


I'm using Vue.js and Gridsome to create a portfolio for myself. However, when I added a JSON file to contain my profile info in the site, I faced an issue. This is how I imported the file inside my Index.vue component:

<script>
import Intro from "~/components/Intro.vue";
import profile from "~/data/profile.json";

export default {
  components: {
    Intro,
  },
  metaInfo: {
    title: "Farzin Nasiri",
  },
  data: () => ({
    profile
  }),
};
</script>

and here is how I use it:

<Intro :personal="profile.personal" />

When I run the project in development (command: gridsome develop), everything is ok and the data is read properly. However, when I want to build the project (command: gridsome build) which creates a build files in the dist folder, then this error happens:

Initializing plugins...
Load sources - 0.04s
Create GraphQL schema - 0.05s
Create pages and templates - 0.03s
Generate temporary code - 0.03s
Bootstrap finish - 0.67s
Compile assets - 13.28s
Execute GraphQL (6 queries) - 0.03s
Write out page data (6 files) - 0s
Could not generate HTML for "/":
TypeError: Cannot read property '__esModule' of undefined
    at i (/home/farzin/MyProjects/portfolio.farzinnasiri.com/node_modules/vue-server-renderer/build.prod.js:1:68670)

this is my project structure(src folder):

├── components
│   ├── Intro.vue
│   └── README.md
├── data
│   └── profile.json
├── favicon.png
├── layouts
│   ├── Default.vue
│   └── README.md
├── main.js
├── pages
│   ├── About.vue
│   ├── Index.vue
│   └── README.md
├── templates
│   ├── README.md
│   └── Work.vue
└── vendor
    └── bootstrap.min.css

here is my package.json:

{
  "name": "portfolio.farzinnasiri.com",
  "version": "0.0.2",
  "private": true,
  "scripts": {
    "build": "gridsome build",
    "develop": "gridsome develop",
    "explore": "gridsome explore"
  },
  "dependencies": {
    "@fortawesome/fontawesome-svg-core": "^1.2.28",
    "@fortawesome/free-brands-svg-icons": "^5.13.0",
    "@fortawesome/free-solid-svg-icons": "^5.13.0",
    "@fortawesome/vue-fontawesome": "^0.1.9",
    "@gridsome/source-filesystem": "^0.6.2",
    "@gridsome/transformer-remark": "^0.5.0",
    "gridsome": "^0.7.0"
  }
}

I really don't get where the problem is and need help to solve it. Thanks for your time


Solution

  • Gridsome & SSR

    From the Gridsome docs:

    gridsome build uses server-side rendering (SSR) to create a fully rendered page. If your Vue component does not support SSR ... it won't be rendered properly.

    we suggest you to encapsulate the component inside <ClientOnly></ClientOnly> tags and import the library inside Vue's mounted() handler.

    The error you got is from vue-server-renderer, which is from Vue's server-side API. You'll need to implement the instructions above.

    ~ import

    Also, without knowing how you've enabled loading JSON as a module (i.e. ~) in development, perhaps that's an issue, because that doesn't work in the default Vue CLI environment.

    Try removing the ~ from all imports:

    import profile from "@/data/profile.json";