繁体   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