如何从节点中的节点模块使用ES6括号导入语句?我正在使用巴贝尔

时间:2018-03-07 00:58:06

标签: javascript node.js import ecmascript-6 babeljs

我正在构建一个节点后端,但我在使用带括号的import语句时遇到了麻烦。

import {foo} from 'bar';

除非我从我自己的代码中导入它,否则不起作用。

所以

import {User} from "./db"

有效,

import {Op} from 'sequelize';

没有。其他导入语句需要符合

的内容
import * as foo from 'bar';

有没有办法让这项工作?

passport.js

import passport from 'passport';
import {ExtractJwt} from 'passport-jwt';
import LocalStrategy from 'passport-local';
import bcrypt from 'bcrypt';
import {User} from "./db"
const config = require('../config');
import {Op} from 'sequelize';

var localOptions = {
    usernameField: 'email'
};

var localStrategy = new LocalStrategy(localOptions, function(email,         
    password, done){

User.findOne({where:{ email: { [Op.eq]: email } }}).then(function (user) {
    if(!user){
        return done(null, false);
    }
    bcrypt.compare(password, user.password, function(error, isMatch) {
        if(error){
            return done(error);
        }
        if(!isMatch){
            return done(null, false);
        }
            return done(null, user);
        });
    });
});

var jwtOptions = {
    secretOrKey: config.secret,
    jwtFromRequest: ExtractJwt.fromHeader('authorization')
};

var jwtStrategy =  new LocalStrategy(jwtOptions, function(payload, done){
    User.findById(payload.sub, function(error, user){
        if(error) {
            return done(error, false);
        }
        if(user){
            done(null, user);
        } else {
            done(null, false);
        }
    });
});

passport.use(jwtStrategy);
passport.use(localStrategy);

.babelrc

{
  "presets": [
    ["env", {
      "targets": {
        "node": "current"
      }
    }]
  ]
}

的package.json

{
  "name": "example-node-server",
  "version": "0.1.0",
  "description": "Example Node Server w/ Babel",
  "main": "lib/index.js",
  "scripts": {
    "start": "nodemon lib/index.js --exec babel-node --presets 
es2015,stage-2",
   "build": "babel lib -d dist",
    "serve": "node dist/index.js",
    "test": "mocha --compilers js:babel-register"
  },
  "author": "Dude",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.0.0-beta.40",
    "@babel/preset-env": "^7.0.0-beta.40",
    "babel-cli": "^6.11.4",
    "babel-preset-env": "^1.6.1",
    "babel-preset-es2015": "^6.9.0",
    "babel-preset-stage-2": "^6.11.0",
    "babel-register": "^6.11.6",
    "mocha": "^3.0.1",
    "nodemon": "^1.10.0"
  },
  "dependencies": {
    "bcrypt": "^1.0.3",
    "express": "^4.16.2",
    "jsonwebtoken": "^8.1.1",
    "jwt-simple": "^0.5.1",
    "morgan": "^1.9.0",
    "passport": "^0.4.0",
    "passport-jwt": "^3.0.1",
    "passport-local": "^1.0.0",
    "pg": "^7.4.1",
    "rimraf": "^2.6.2",
    "sequelize": "^4.31.2",
    "sequelize-cli": "^3.2.0"
  }
}

0 个答案:

没有答案