Cannot use import statement outside a module

Cannot use import statement outside a module

Cannot use import statement outside a module_第1张图片
问题描述

import Koa from 'koa';
import Router from 'koa-router' 
const app = new Koa();
const koa = new Router();

// const app = require('koa')()
//   , koa = require('koa-router')()
//   , json = require('koa-json')
//   , logger = require('koa-logger'); // 引入各种依赖

app.use(require('koa-bodyparser')());
app.use(json());
app.use(logger());

app.use(function* (next){
  let start = new Date;
  yield next;
  let ms = new Date - start;
  console.log('%s %s - %s', this.method, this.url, ms); // 显示执行的时间
});

app.on('error', function(err, ctx){
  console.log('server error', err);
});

app.listen(8889,() => {
  console.log('Koa is listening in 8889');
});

module.exports = app;

node中使用import
解决方案
package.json中加入"type":"module"即可

{
  "name": "demo2",
  "version": "1.0.0",
  "description": "A Vue.js project",
  "private": true,
  "type":"module",
    },

你可能感兴趣的:(node,node.js)