启动服务报错如下:
Last login: Wed Nov 15 13:44:48 on ttys000 localhost:~ sipeng$ cd /Users/sipeng/Desktop/彭思/2017年学习/Node/myblog localhost:myblog sipeng$ node /Users/sipeng/Desktop/彭思/2017年学习/Node/myblog/index.js /Users/sipeng/Desktop/彭思/2017年学习/Node/myblog/models/posts.js:1 (function (exports, require, module, __filename, __dirname) { import { populate } from 'mongolass/lib/plugins'; ^^^^^^ SyntaxError: Unexpected token import at createScript (vm.js:74:10) at Object.runInThisContext (vm.js:116:10) at Module._compile (module.js:533:28) at Object.Module._extensions..js (module.js:580:10) at Module.load (module.js:503:32) at tryModuleLoad (module.js:466:12) at Function.Module._load (module.js:458:3) at Module.require (module.js:513:17) at require (internal/module.js:11:18) at Object.<anonymous> (/Users/sipeng/Desktop/彭思/2017年学习/Node/myblog/routes/posts.js:8:19) at Module._compile (module.js:569:30) at Object.Module._extensions..js (module.js:580:10) at Module.load (module.js:503:32) at tryModuleLoad (module.js:466:12) at Function.Module._load (module.js:458:3) at Module.require (module.js:513:17)
报错原因:
Node.js不支持import导入第三方框架,使用require导入即可。
// import { populate } from 'mongolass/lib/plugins'; const populate = require('mongolass/lib/plugins') const marked = require('marked') const Post = require('../lib/mongo').Post