如何翻译需要es6导入/导出节点模块

时间:2016-08-30 00:48:14

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

我想将pokedex-promise用于pokemonapi,但docs只显示如何以vanilla javascript风格要求它:

npm install pokedex-promise-v2 --save
var Pokedex = require('pokedex-promise-v2');
var P = new Pokedex();

如何使用导入来获取此模块?我试过了

import {Pokedex} from 'pokedex-promise-v2'

import Pokedex from 'pokedex-promise-v2'

但两者都会在模块中抛出错误:

in ./~/tough-cookie/package.json
Module parse failed: /Users/jlei/Desktop/pokeapp/node_modules/tough-cookie/package.json Unexpected token (2:9)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (2:9)
    at Parser.pp$4.raise (/Users/jlei/Desktop/pokeapp/node_modules/acorn/dist/acorn.js:2221:15)
    at Parser.pp.unexpected (/Users/jlei/Desktop/pokeapp/node_modules/acorn/dist/acorn.js:603:10)
    at Parser.pp.semicolon (/Users/jlei/Desktop/pokeapp/node_modules/acorn/dist/acorn.js:581:61)
    at Parser.pp$1.parseExpressionStatement (/Users/jlei/Desktop/pokeapp/node_modules/acorn/dist/acorn.js:966:10)
    at Parser.pp$1.parseStatement (/Users/jlei/Desktop/pokeapp/node_modules/acorn/dist/acorn.js:730:24)
    at Parser.pp$1.parseBlock (/Users/jlei/Desktop/pokeapp/node_modules/acorn/dist/acorn.js:981:25)
    at Parser.pp$1.parseStatement (/Users/jlei/Desktop/pokeapp/node_modules/acorn/dist/acorn.js:709:33)
    at Parser.pp$1.parseTopLevel (/Users/jlei/Desktop/pokeapp/node_modules/acorn/dist/acorn.js:638:25)
    at Parser.parse (/Users/jlei/Desktop/pokeapp/node_modules/acorn/dist/acorn.js:516:17)
    at Object.parse (/Users/jlei/Desktop/pokeapp/node_modules/acorn/dist/acorn.js:3098:39)
 @ ./~/tough-cookie/lib/cookie.js 38:14-40

1 个答案:

答案 0 :(得分:1)

您需要为webpack添加https://github.com/bkeepers/dotenv#note-on-load-order。通过NPM安装:

module: {
    loaders: [
        /* ...other loaders */
        {
            test: /\.json$/,
            loader: 'json',
        },
    ],
},

然后将其添加到您的webpack配置中,如下所示:

{{1}}

这将允许webpack在捆绑时加载json文件。