Nuxt错误:安装后出现语法意外的令牌导出

14

我正在使用Nuxt作为我的Vue项目框架,一切都很正常。由于其他项目的问题,我删除了yarn和NPM缓存。我重新安装了Nuxt应用程序的软件包。该应用程序是通用且使用express。已经安装并运行开发服务器,但是当我尝试访问http://localhost:3000/时,出现以下错误:

SyntaxError:意外的令牌export

我知道这是babel的问题,但我不知道如何在Nuxt上解决这个问题。

Nuxt配置:

const pkg = require('./package')

module.exports = {
  mode: 'universal',

  /*
  ** Headers of the page
  */
  head: {
    title: pkg.name,
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: pkg.description }
    ],
    link: [
      { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
    ]
  },

  /*
  ** Customize the progress-bar color
  */
  loading: { color: '#fff' },

  /*
  ** Global CSS
  */
  css: [
    'element-ui/lib/theme-chalk/index.css',
    '@mdi/font/css/materialdesignicons.min.css'
  ],

  /*
  ** Plugins to load before mounting the App
  */
  plugins: [
    '@/plugins/element-ui',
    '~/plugins/vee-validate.js'
  ],

  /*
  ** Nuxt.js modules
  */
  modules: [
    // Doc: https://github.com/nuxt-community/axios-module#usage
    '@nuxtjs/axios',
    '@nuxtjs/apollo'
  ],
  apollo: {
    tokenName: 'yourApolloTokenName', // optional, default: apollo-token
    tokenExpires: 10, // optional, default: 7
    includeNodeModules: true, // optional, default: false (this includes graphql-tag for node_modules folder)
    authenticationType: 'Basic', // optional, default: 'Bearer'
    // optional
    errorHandler (error) {
      console.log('%cError', 'background: red; color: white; padding: 2px 4px; border-radius: 3px; font-weight: bold;', error.message)
    },
    // required
    clientConfigs: {
      default: {
        // required  
        httpEndpoint: 'http://localhost:4000',
        // optional
        // See https://www.apollographql.com/docs/link/links/http.html#options
        httpLinkOptions: {
          credentials: 'same-origin'
        },
        // You can use `wss` for secure connection (recommended in production)
        // Use `null` to disable subscriptions
        wsEndpoint: null, // optional
        // LocalStorage token
        tokenName: 'apollo-token', // optional
        // Enable Automatic Query persisting with Apollo Engine
        persisting: false, // Optional
        // Use websockets for everything (no HTTP)
        // You need to pass a `wsEndpoint` for this to work
        websocketsOnly: false // Optional
      },
      test: {
        httpEndpoint: 'http://localhost:5000',
        wsEndpoint: 'ws://localhost:5000',
        tokenName: 'apollo-token'
      },
      // alternative: user path to config which returns exact same config options
    }
  },
  /*
  ** Axios module configuration
  */
  axios: {
    // See https://github.com/nuxt-community/axios-module#options
  },

  /*
  ** Build configuration
  */
  build: {
    /*
    ** You can extend webpack config here
    */
    extend(config, ctx) {

    }
  }
}

我的package.json文件

{
  "name": "app",
  "version": "1.0.0",
  "description": "My exceptional Nuxt.js project",
  "author": "Saima",
  "private": true,
  "scripts": {
    "dev": "cross-env NODE_ENV=development nodemon server/index.js --watch server",
    "build": "nuxt build",
    "start": "cross-env NODE_ENV=production node server/index.js",
    "generate": "nuxt generate"
  },
  "dependencies": {
    "@mdi/font": "^3.3.92",
    "@nuxtjs/apollo": "^4.0.0-rc2.3",
    "@nuxtjs/axios": "^5.0.0",
    "cross-env": "^5.2.0",
    "element-ui": "^2.4.6",
    "express": "^4.16.3",
    "graphql-tag": "^2.10.1",
    "less": "^3.9.0",
    "less-loader": "^4.1.0",
    "nuxt": "^2.0.0",
    "vee-validate": "^2.1.5"
  },
  "devDependencies": {
    "babel-preset-env": "^1.7.0",
    "babel-register": "^6.26.0",
    "nodemon": "^1.11.0"
  }
}

需要帮助,谢谢。


@BeniaminH 已添加 package.json。 - saima dattuu
1
@BeniaminH 的 package.json 是有效的。 - saima dattuu
我在其他电脑上尝试了这个问题,但仍然出现相同的错误。 - saima dattuu
presets:['@nuxt/babel-preset-app'] - Beniamin H
1
可能由插件与 SSR 发生问题所致。您可以尝试像这样更改 nuxt.config.js 文件:plugins: [{src: '~/plugins/element-ui', ssr: false},{src: '~/plugins/vee-validate.js', ssr: false}] 并在 build 部分中加入 transpile :[ '/plugins'],其他元素不变。 - Andrew1325
显示剩余13条评论
4个回答

20

我刚刚查看了你的问题,发现当你在Nuxt中使用Element UI时会出现问题。请按照Andrew的回答更新你的Nuxt配置:

plugins: [
  {src: '~/plugins/element-ui', ssr: false},
  {src: '~/plugins/vee-validate.js', ssr: true},
],

1
没错,说得对。虽然我没有测试过,但我想它必须是其中之一。 - Andrew1325
我测试了你的代码片段,一切正常。 - Ilyas karim

11

如果您正在导入一个需要转译才能加载到 UI 中的 ES6 模块,就会出现此错误。在这种情况下,您需要将该模块添加到 nuxt.config.jsbuild 部分的 transpile 键中来修复此问题(截至本篇文章发布时,Nuxt transpile 文档有些令人困惑)。

例如,如果您要导入名为 @stylelib 的模块,则应在您的 nuxt.config.js 中添加以下内容:

export default {
  ...
  build: {
    ...
    transpile: ['@stylelib']
  }
}

0

我曾经遇到过同样的问题,发现是在我的nuxt.config.js文件中,我放置了一些额外的代码,并在该代码末尾加上了一个逗号。有问题的代码位于文件顶部。

这段代码:

env: {  
  strapiBaseUri: process.env.API_URL || "http://localhost:1337"
},

我的设置详情如下:

Nuxtjs(截至2020年3月20日的版本) Apollo和Graphql Strapi(后端)


0

对我来说(使用typescript Nuxt),它是这样的:

npm run start

改为:

npm run dev 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接