Javascript 由于导入,Next.js导出失败?
导出我的nextjs应用程序时出现以下错误:Javascript 由于导入,Next.js导出失败?,javascript,webpack,next.js,Javascript,Webpack,Next.js,导出我的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
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版本。Web包Babel不会解析next.config.js
或打字稿
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: '/'},
})
},
};
我认为这确实是问题所在,但仍然得到{错误:现在找不到模块'./src/api''错误。我是否在src/api中以错误的方式导出?您可以在此处找到:,只需运行Thread安装和Thread导出。如果我尝试使用module.exports导出函数,我将得到一个“当提供了'--isolatedmodules'标志时,所有文件都必须是模块”警告,这是由next.js预配置的,但它会看到ms to be required()也许我应该这样做:是的,你应该转换
next.config.js
中所需的所有代码谢谢所有的帮助,现在将转换它,很遗憾es6模块不受支持,可能是出于某种原因,但是的。。。