在Node中使用Es6的import
安装必须的包
npm install @babel/core @babel/register @babel/preset-env --save-dev2. 创建文件
// file server.js
const express = require('express')
const app = express()
// respond with "hello world" when a GET request is made to the homepage
app.get('/', function (req, res) {
res.send('hello world')
})
app.listen(3000, () => console.log('Example app listening on port 3000!'))将require替换成import
// file server.js
import express from 'express'
const app = express()
// respond with "hello world" when a GET request is made to the homepage
app.get('/', function (req, res) {
res.send('hello world')
})
app.listen(3000, () => console.log('Example app listening on port 3000!'))3. 增加start.js文件
4. 运行文件
最后更新于
这有帮助吗?