首页 > 解决方案 > 模块解析失败:React 项目中出现意外的令牌。您可能需要适当的加载程序来处理此文件类型

问题描述

我是 React 的新手,所以如果这是一个非常愚蠢和明显的错误,我提前道歉。

我正在尝试为简单的区块链文件传输创建去中心化应用程序(DApp)。我正在关注以下教程:https ://itnext.io/build-a-simple-ethereum-interplanetary-file-system-ipfs-react-js-dapp-23ff4914ce4e (tl;dr 版本 - 基本上是文件传输区块链网络。您将文件上传到 IPFS,然后将 IPFS 位置从一个区块链帐户发送到另一个)。对于前端,我使用的是 React。

有两点需要注意——

1. 我使用的是 Ropsten 网络,而不是本文中使用的 Rinkeby

2. 在 REMIX 中,我将部署环境设置为“Injected Web3”。(原始项目模棱两可)

此项目的依赖项之一是ipfs-api,但现在已弃用。所以我不得不ipfs-http-client改用。npm 安装成功。然而,在编译时,我得到了错误

./node_modules/ipfs-http-client/src/lib/to-url-search-params.js
Module parse failed: Unexpected token (10:61)
You may need an appropriate loader to handle this file type.
|  * @returns {URLSearchParams}
|  */
| module.exports = ({ arg, searchParams, hashAlg, mtime, mode, ...options } = {}) => {
|   if (searchParams) {
|     options = {

to-url-search-params.js 文件 -

'use strict'

const modeToString = require('./mode-to-string')
const { parseMtime } = require('ipfs-unixfs')

/**
 * @param {*} params
 * @returns {URLSearchParams}
 */
module.exports = ({ arg, searchParams, hashAlg, mtime, mode, ...options } = {}) => {
  if (searchParams) {
    options = {
      ...options,
      ...searchParams
    }
  }

  if (hashAlg) {
    options.hash = hashAlg
  }

  if (mtime != null) {
    mtime = parseMtime(mtime)

    options.mtime = mtime.secs
    options.mtimeNsecs = mtime.nsecs
  }

  if (mode != null) {
    options.mode = modeToString(mode)
  }

  if (options.timeout && !isNaN(options.timeout)) {
    // server API expects timeouts as strings
    options.timeout = `${options.timeout}ms`
  }

  if (arg === undefined || arg === null) {
    arg = []
  } else if (!Array.isArray(arg)) {
    arg = [arg]
  }

  const urlSearchParams = new URLSearchParams(options)

  arg.forEach((/** @type {any} */ arg) => urlSearchParams.append('arg', arg))

  return urlSearchParams
}

所有其他文件与https://github.com/mcchan1/eth-ipfs中提供的文件完全相同。

不知道错误是什么......提前感谢您的帮助。

标签: javascriptnode.jsblockchainethereumsolidity

解决方案


推荐阅读