奇奇怪怪小经验
  • 五味杂陈·东倒西歪
  • Vue3 踩坑
    • 在vue3中定义filter
    • 父组件调用refs里的方法
  • Electron
    • Electron use vue devtools
  • Collection
    • Little tools
    • virtual box安装win11
    • CentOS 镜像
    • Yarn1 to Yarn2
  • 另一个伊甸-穿越时空的猫
    • 天冥值介紹及角色升星(升級)職業書出處
  • vuetify使用日记
    • 自定义Preset包
  • 前端小店
    • 奇怪知识点
    • 前端工具箱
    • 数组求和的几种方法
    • Vue i18n dynamic set messages
    • 关于H5自动播放被禁止的问题
    • 倒计时时间
    • css禁止选中文本
    • 在chrome dev tool 中使用JQ
  • Yarn-berry
    • Which files should be gitignored
    • yarn upgrade-interactive
  • 错误题集
    • Node "cannot use import statement outside a module"
  • Node
    • 在Node中使用Es6的import
    • express 动态加载route与使用自定义回调和通用回调
  • deno
    • 构建简单的api service
  • Mac使用技巧
    • 提示:已损坏,为无法打开,您应该将它移到废纸篓
    • macOs 升级后提示xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missin
  • winston使用技巧
    • 使用level颜色
    • level大写
  • demo
    • 九宫格抽奖
    • 波浪效果
  • Vue
    • 在组件中同步v-model
    • 工具库
  • 文章记录
    • 前端开发
  • 日常工具
    • 微信小助手
    • Untitled
由 GitBook 提供支持
在本页
  1. Node

在Node中使用Es6的import

上一页Node "cannot use import statement outside a module"下一页express 动态加载route与使用自定义回调和通用回调

最后更新于4年前

这有帮助吗?

CtrlK

这有帮助吗?

  1. 安装必须的包

npm install @babel/core @babel/register @babel/preset-env --save-dev

2. 创建文件

// 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文件

// file start.js
// Transpile all code following this line with babel and use '@babel/preset-env' (aka ES6) preset.
require("@babel/register")({
  presets: ["@babel/preset-env"]
});

// Import the rest of our application.
module.exports = require('./server.js')

4. 运行文件

node start.js