gpt4 book ai didi

javascript - 如何将 Node 模块的 require 转换为 es6 import/export

转载 作者:行者123 更新时间:2023-12-03 06:13:25 25 4
gpt4 key购买 nike

我想将 pokedex-promise 用于 pokemonapi,但 docs只向您展示如何以普通 javascript 风格要求它:

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

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

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

最佳答案

您需要添加 json-loader对于网页包。通过 NPM 安装:

npm install --save json-loader

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

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

这将允许 webpack 在打包时加载 json 文件。

关于javascript - 如何将 Node 模块的 require 转换为 es6 import/export,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39217257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com