简体   繁体   中英

ES Modules in a Next.js server.js

I checked a few examples from the Next.js repository

and noticed that examples use ES Modules in React components and CommonJS modules in the server.js file. I'd like to keep my code written in one style, is it possible to use only ES Modules in a Next.js project?

This is sitting as a feature request in the Next.js backlog. https://github.com/zeit/next.js/issues/9607

According to node.js document Yes, you should save the server.js with ES6 modules with .mjs ( server.mjs ) extension and change package.json

from:

"scripts": {
  "dev": "node server.js",
  "build": "next build",
  "start": "NODE_ENV=production node server.js"
},

to:

"scripts": {
  "dev": "node --experimental-modules server.mjs",
  "build": "next build",
  "start": "NODE_ENV=production node --experimental-modules server.mjs"
},

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