导出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);
};
我在文档中找到了这个,但是我不确定谁来解决它:
避免使用目标中不可用的新JavaScript功能 Node.js版本。 Webpack,Babel将无法解析next.config.js 或TypeScript。
答案 0 :(得分:1)
您的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: '/'},
})
},
};