Nar*_*tel 5 javascript node.js express mern
我正在开发 MERN 应用程序,在导入 Express 时,它显示错误,它显示SyntaxError: Cannot use import statement outside a module
我的代码是
import express from 'express';
const app = express();
const port = process.env.PORT || 3000;
app.get('/',(req,res)=>{
res.send('this is homepage')
})
app.listen( port ,()=>{
console.log('server is running at port number 3000')
});
Run Code Online (Sandbox Code Playgroud)
rem*_*ual 12
import和export都是 ES6 语法。
要在 NodeJS 应用程序中启用 ES6 模块,请添加"type": "module"到您的package.json文件中。
或者,您可以使用.mjs文件的扩展名。
否则,节点默认为使用require和module.exports语法的 CommonJS 模块加载器。
ES6 模块支持在 Node v12 及更高版本中稳定,并且在 v9 - v11 中处于实验阶段,需要传递标志--experimental-module。
Node.js 文档:模块:ECMAScript 模块
| 归档时间: |
|
| 查看次数: |
20472 次 |
| 最近记录: |