簡體   English   中英

Next.js 導出因導入失敗?

[英]Next.js export failing due to import?

導出我的 nextjs 應用程序時出現以下錯誤:

SyntaxError: Unexpected token {
    at new Script (vm.js:80:7)
    at createScript (vm.js:274:10)
    at Object.runInThisContext (vm.js:326:10)
    at Module._compile (internal/modules/cjs/loader.js:664:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)

這是我的next.config.js

import {getBlogPosts} from "./src/api";
import {mapPosts} from "./src/mappers/BlogMapper";
module.exports = {
    async exportPathMap() {
        const response = await getBlogPosts();
        const posts = mapPosts(response);
        const pages = posts.reduce(
            (pages, post) =>
                Object.assign({}, pages, {
                    [`/blog/${post.uid}`]: {page: '/blog/[uid]'},
                }),
            {}
        );
        // combine the map of post pages with the home
        return Object.assign({}, pages, {
            '/': {page: '/'},
        })
    },
};

src/api 文件:

import Prismic from "prismic-javascript";
import {Client} from "./prismic-configuration";
export const getBlogPosts = (req) => {
    return Client(req).query(
        Prismic.Predicates.at("document.type", "post"),
        {orderings: "[my.post.date desc]"}
    );
};
export const getBlogPost = (req, uid) => {
    return Client(req).getByUID("post", uid, null);
};

我在文檔中找到了這個,但我不確定誰來解決它:

避免使用目標 Node.js 版本中不可用的新 JavaScript 功能。 next.config.js 不會被 Webpack、Babel 或 TypeScript 解析。

您的next.config.js:代碼是使用 ES6 語法編寫的。

您應該將其轉換為 CommonJS

像這樣嘗試:

const getBlogPosts = require('./src/api').getBlogPosts
const mapPosts = require('./src/mappers/BlogMapper').mapPosts

module.exports = {
    exportPathMap: async function () {
        const response = await getBlogPosts();
        const posts = mapPosts(response);
        const pages = posts.reduce(
            (pages, post) =>
                Object.assign({}, pages, {
                    [`/blog/${post.uid}`]: {page: '/blog/[uid]'},
                }),
            {}
        );
        // combine the map of post pages with the home
        return Object.assign({}, pages, {
            '/': {page: '/'},
        })
    },
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM