Nuxt.js: Async in module.exports : "Cannot assign to read only property 'exports' of object '#<Object>'"

Created on 12 Jun 2017  路  10Comments  路  Source: nuxt/nuxt.js

Hello !
Anyone know what's wrong using async in Nuxt.js module.exports ?
This works well if i simply return a Promise without async & await stuff.

module.exports = {

  async getPosts (perPage = 10) {
    const {data} = await axios.get(endpoint + '/posts?_embed&per_page=' + perPage)
    return data
  }

}

This question is available on Nuxt.js community (#c747)
question

Most helpful comment

All 10 comments

Hi @nyl-auster

Where are you using async/await in your project?

If it's in nuxt.config.js, you need to use at least node 7 or 8.

Hi ! It's a custom module to fetch Content from wordpress API with _axios_, it is called from "asyncData" function of my components pages. Here is my terminal output :

nuxt:render Rendering url /posts/ +6s
[vue-router] Failed to resolve async component default: TypeError: Cannot assign to read only property 'exports' of object '#<Object>'
[vue-router] uncaught error during route navigation:
TypeError: Cannot assign to read only property 'exports' of object '#<Object>'
    at Object.<anonymous> (2.server-bundle.js:45:16)

@nyl-auster If possible please provide code snipped (or js file) used to fetch. Also suggest using axios module for better nuxt integration :)

@pi0 Here it is.
Works fine if i call this module from a test file launched with nodejs command in a terminal.

/**
 * Get content from wordpress via REST Api
 */
const endpoint = 'https://public-api.wordpress.com/wp/v2/sites/yineo.fr'
const axios = require('axios')

module.exports = {

  async getPosts (perPage = 10) {
    const {data} = await axios.get(endpoint + '/posts?_embed&per_page=' + perPage)
    return data
  }

}

@pi0 oups sorry ... this is a dedicated file located in "services/wpContentApi.js".

@pi0 thanks ! but if i use _export default_ instead of _require_ in my Vue component; i can't run anymore my file from the server, correct ?
I use this service from server to "pre-fetch" some results, launched by "node" command, is there a way to "require" or "import it" that works both for client and server ? Sorry for the noob questions !

SyntaxError: Unexpected token export

@nyl-auster I'm sorry but from webpack/webpack#4039 this is stated:

You can mix require and export. You can't mix import and module.exports.

So i think for your launcher script ( if it is separated from nuxt and needs require api.js) you may use babel-node that adds import/export support on the fly :)

okay, thanks a lot for your time

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

msudgh picture msudgh  路  3Comments

bimohxh picture bimohxh  路  3Comments

vadimsg picture vadimsg  路  3Comments

lazycrazy picture lazycrazy  路  3Comments

vadimsg picture vadimsg  路  3Comments