简体   繁体   中英

Next.js export failing due to import?

I am getting the following error when exporting my nextjs app:

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)

This is my 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 file:

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);
};

I found this in the docs but I am not sure who to resolve it:

Avoid using new JavaScript features not available in your target Node.js version. next.config.js will not be parsed by Webpack, Babel or TypeScript.

Your code for next.config.js: is written using ES6 syntax.

You should convert it to CommonJS

Try like this:

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: '/'},
        })
    },
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM