Koa项目使用ES6语法

2020-03-29  本文已影响0人  时不我与_

上篇文章我们说到了使用Mongoose进行增删改查的操作,我们都知道ES6新增了很多的新特性,那么我们如果在Koa项目中使用ES6语法会如何呢?

ES6, 全称 ECMAScript 6.0 ,是 JavaScript 的下一个版本标准,其中提供了很多的新语法新特性,但是很多情况下他是没有办法直接进行使用的,需要使用Babel来进行转换,那么我们的Koa项目是否能够正常使用?

//首先我们安装koa模块
npm install koa
//然后在App.js里面进行引入,通常我们使用都是
const Koa = require('koa')
import Koa from 'koa'
import Koa from 'koa'
       ^^^

SyntaxError: Unexpected identifier
    at Module._compile (internal/modules/cjs/loader.js:872:18)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
    at Module.load (internal/modules/cjs/loader.js:790:32)
    at Function.Module._load (internal/modules/cjs/loader.js:703:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:999:10)
    at internal/main/run_main_module.js:17:11
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! koa-test@1.0.0 start: `node app.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the koa-test@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm install @babel/core @babel/preset-env @babel/register @babel/cli @babel/node
require('@babel/register');
require('./src/app');
{
  "presets": ["@babel/preset-env"]
}
//当然 我简单的在我的文件中console一下
console.log('GoodGoodGoGoGo')
console
"build": "babel src --watch --out-dir dist"
完工~
上一篇 下一篇

猜你喜欢

热点阅读