react fecth 本地 json

出现 Unexpected token < in JSON at position 0,将文件放到 public目录下

async 和 await 无法识别

  1. Uncaught ReferenceError: regeneratorRuntime is not defined

原因:使用 ES7 async/await 报错,浏览器不能识别
解决方法:

  1. 安装 babel-plugin-transform-runtime

    1. yarn add --dev babel-plugin-transform-runtime
  2. .babelsrc 添加 配置

    1. "plugins": [
    2. [
    3. "transform-runtime",
    4. {
    5. "helpers": false,
    6. "polyfill": false,
    7. "regenerator": true,
    8. "moduleName": "babel-runtime"
    9. }
    10. ]
    11. ]

    Echart 无法打开

    C:\Windows\System32\drivers\etc 下 host 添加一下命令

    1. 151.101.2.132 echarts.apache.org

vue3 项目 安装 web3js 报错

  1. Module not found: Error: Can't resolve 'assert' in '*/node_modules/ethereumjs-util/dist'
  2. BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
  3. This is no longer the case. Verify if you need this module and configure a polyfill for it.
  4. If you want to include a polyfill, you need to:
  5. - add a fallback 'resolve.fallback: { "assert": require.resolve("assert/") }'
  6. - install 'assert'
  7. If you don't want to include a polyfill, you can use an empty module like this:
  8. resolve.fallback: { "assert": false }
  1. 将日志中需要 resolve.fallback的插件汇总,汇总后安装

    1. yarn add --dev stream-http https-browserify crypto-browserify stream-browserify os-browserify url assert
  2. 在vue.config.js 中添加如下配置

    1. module.exports = defineConfig({
    2. configureWebpack: {
    3. plugins: [
    4. new webpack.ProvidePlugin({
    5. Buffer: ['buffer', 'Buffer'],
    6. process: 'process/browser'
    7. })
    8. ],
    9. resolve: {
    10. fallback: {
    11. http: require.resolve('stream-http'),
    12. https: require.resolve('https-browserify'),
    13. crypto: require.resolve('crypto-browserify'),
    14. stream: require.resolve('stream-browserify'),
    15. os: require.resolve('os-browserify/browser'),
    16. url: require.resolve('url'),
    17. assert: require.resolve('assert')
    18. }
    19. }
    20. }
    21. })
  3. 安装 buffer 和 wepack

    refs:https://github.com/ChainSafe/web3.js/issues/4821

Error: EBUSY: resource busy or locked, rmdir

  1. npm cache verify

Vue3 import web3 fail

方法 1 : Polyfill Node globals/modules

思路:web3 配置兼容性,使其能在浏览器使用

  1. 安装以下两个插件

    1. yarn add @esbuild-plugins/node-globals-polyfill --dev
    2. yarn add @esbuild-plugins/node-modules-polyfill --dev
  2. 配置 optimizeDeps.esbuildOptions 使用 ESBuild 插件,通过 define 替换 globalThis

在线 demo

引入了 polyfill 包,导致打包体积增大

方法 2: Use pre-bundled script

思路:web3 打包后的dist可以直接在浏览器运行

  1. 配置 alias 指向这个文件 ```javascript import { defineConfig } from ‘vite’

export default defineConfig({ ⋮ resolve: { alias: { web3: ‘web3/dist/web3.min.js’, },

  1. // or
  2. alias: [
  3. {
  4. find: 'web3',
  5. replacement: 'web3/dist/web3.min.js',
  6. },
  7. ],

}, })

  1. [在线 demo](https://stackblitz.com/edit/vue3-web3-demo-w1lkev?file=vite.config.ts)
  2. > 直接引用 dist ,体积更小
  3. 参考链接:[web3js-fails-to-import-in-vue3-composition-api-project](https://stackoverflow.com/questions/68975837/web3js-fails-to-import-in-vue3-composition-api-project/69021714#69021714)
  4. <a name="hJ2Ym"></a>
  5. ## Sass 问题
  6. <a name="fKo0C"></a>
  7. ### Module build failed: TypeError: this.getResolve is not a function
  8. sass-loader 版本过高,降级到 ^7.3
  9. ```css
  10. yarn add sass-loader@^7.3

PancakeSwap 安装依赖错误分析

错误分析 - 阶段1

错误日志

  1. warning Error running install script for optional dependency: "D:\\tk\\company\\dccswap_front_swap\\node_modules\\sqlite3: Command failed.
  2. Exit code: 1
  3. Command: node-pre-gyp install --fallback-to-build
  4. Arguments:
  5. Directory: D:\\tk\\company\\dccswap_front_swap\\node_modules\\sqlite3
  6. Output:
  7. node-pre-gyp info it worked if it ends with ok
  8. node-pre-gyp info using node-pre-gyp@0.11.0
  9. node-pre-gyp info using node@16.16.0 | win32 | x64
  10. node-pre-gyp WARN Using request for node-pre-gyp https download
  11. node-pre-gyp info check checked for \"D:\\tk\\company\\dccswap_front_swap\\node_modules\\sqlite3\\lib\\binding\\node-v93-win32-x64\\node_sqlite3.node\" (not found)
  12. node-pre-gyp http GET https://mapbox-node-binary.s3.amazonaws.com/sqlite3/v4.2.0/node-v93-win32-x64.tar.gz
  13. node-pre-gyp http 403 https://mapbox-node-binary.s3.amazonaws.com/sqlite3/v4.2.0/node-v93-win32-x64.tar.gz
  14. node-pre-gyp WARN Tried to download(403): https://mapbox-node-binary.s3.amazonaws.com/sqlite3/v4.2.0/node-v93-win32-x64.tar.gz
  15. node-pre-gyp WARN Pre-built binaries not found for sqlite3@4.2.0 and node@16.16.0 (node-v93 ABI, unknown) (falling back to source compile with node-gyp)
  16. node-pre-gyp http 403 status code downloading tarball https://mapbox-node-binary.s3.amazonaws.com/sqlite3/v4.2.0/node-v93-win32-x64.tar.gz
  17. gyp info it worked if it ends with ok
  18. gyp info using node-gyp@8.4.1
  19. gyp info using node@16.16.0 | win32 | x64
  20. gyp info ok
  21. gyp info it worked if it ends with ok
  22. gyp info using node-gyp@8.4.1
  23. gyp info using node@16.16.0 | win32 | x64
  24. gyp ERR! find Python
  25. gyp ERR! find Python Python is not set from command line or npm configuration
  26. gyp ERR! find Python Python is not set from environment variable PYTHON
  27. gyp ERR! find Python checking if \"python3\" can be used
  28. gyp ERR! find Python - \"python3\" is not in PATH or produced an error
  29. gyp ERR! find Python checking if \"python\" can be used
  30. gyp ERR! find Python - \"python\" is not in PATH or produced an error
  31. gyp ERR! find Python checking if Python is C:\\Users\\fetk\\AppData\\Local\\Programs\\Python\\Python39\\python.exe
  32. gyp ERR! find Python - \"C:\\Users\\fetk\\AppData\\Local\\Programs\\Python\\Python39\\python.exe\" could not be run
  33. gyp ERR! find Python checking if Python is C:\\Program Files\\Python39\\python.exe
  34. gyp ERR! find Python - \"C:\\Program Files\\Python39\\python.exe\" could not be run
  35. gyp ERR! find Python checking if Python is C:\\Users\\fetk\\AppData\\Local\\Programs\\Python\\Python39-32\\python.exe
  36. gyp ERR! find Python - \"C:\\Users\\fetk\\AppData\\Local\\Programs\\Python\\Python39-32\\python.exe\" could not be run
  37. gyp ERR! find Python checking if Python is C:\\Program Files\\Python39-32\\python.exe
  38. gyp ERR! find Python - \"C:\\Program Files\\Python39-32\\python.exe\" could not be run
  39. gyp ERR! find Python checking if Python is C:\\Program Files (x86)\\Python39-32\\python.exe
  40. gyp ERR! find Python - \"C:\\Program Files (x86)\\Python39-32\\python.exe\" could not be run
  41. gyp ERR! find Python checking if Python is C:\\Users\\fetk\\AppData\\Local\\Programs\\Python\\Python38\\python.exe
  42. gyp ERR! find Python - \"C:\\Users\\fetk\\AppData\\Local\\Programs\\Python\\Python38\\python.exe\" could not be run
  43. gyp ERR! find Python checking if Python is C:\\Program Files\\Python38\\python.exe
  44. gyp ERR! find Python - \"C:\\Program Files\\Python38\\python.exe\" could not be run
  45. gyp ERR! find Python checking if Python is C:\\Users\\fetk\\AppData\\Local\\Programs\\Python\\Python38-32\\python.exe
  46. gyp ERR! find Python - \"C:\\Users\\fetk\\AppData\\Local\\Programs\\Python\\Python38-32\\python.exe\" could not be run
  47. gyp ERR! find Python checking if Python is C:\\Program Files\\Python38-32\\python.exe
  48. gyp ERR! find Python - \"C:\\Program Files\\Python38-32\\python.exe\" could not be run
  49. gyp ERR! find Python checking if Python is C:\\Program Files (x86)\\Python38-32\\python.exe
  50. gyp ERR! find Python - \"C:\\Program Files (x86)\\Python38-32\\python.exe\" could not be run
  51. gyp ERR! find Python checking if Python is C:\\Users\\fetk\\AppData\\Local\\Programs\\Python\\Python37\\python.exe
  52. gyp ERR! find Python - \"C:\\Users\\fetk\\AppData\\Local\\Programs\\Python\\Python37\\python.exe\" could not be run
  53. gyp ERR! find Python checking if Python is C:\\Program Files\\Python37\\python.exe
  54. gyp ERR! find Python - \"C:\\Program Files\\Python37\\python.exe\" could not be run
  55. gyp ERR! find Python checking if Python is C:\\Users\\fetk\\AppData\\Local\\Programs\\Python\\Python37-32\\python.exe
  56. gyp ERR! find Python - \"C:\\Users\\fetk\\AppData\\Local\\Programs\\Python\\Python37-32\\python.exe\" could not be run
  57. gyp ERR! find Python checking if Python is C:\\Program Files\\Python37-32\\python.exe
  58. gyp ERR! find Python - \"C:\\Program Files\\Python37-32\\python.exe\" could not be run
  59. gyp ERR! find Python checking if Python is C:\\Program Files (x86)\\Python37-32\\python.exe
  60. gyp ERR! find Python - \"C:\\Program Files (x86)\\Python37-32\\python.exe\" could not be run
  61. gyp ERR! find Python checking if Python is C:\\Users\\fetk\\AppData\\Local\\Programs\\Python\\Python36\\python.exe
  62. gyp ERR! find Python - \"C:\\Users\\fetk\\AppData\\Local\\Programs\\Python\\Python36\\python.exe\" could not be run
  63. gyp ERR! find Python checking if Python is C:\\Program Files\\Python36\\python.exe
  64. gyp ERR! find Python - \"C:\\Program Files\\Python36\\python.exe\" could not be run
  65. gyp ERR! find Python checking if Python is C:\\Users\\fetk\\AppData\\Local\\Programs\\Python\\Python36-32\\python.exe
  66. gyp ERR! find Python - \"C:\\Users\\fetk\\AppData\\Local\\Programs\\Python\\Python36-32\\python.exe\" could not be run
  67. gyp ERR! find Python checking if Python is C:\\Program Files\\Python36-32\\python.exe
  68. gyp ERR! find Python - \"C:\\Program Files\\Python36-32\\python.exe\" could not be run
  69. gyp ERR! find Python checking if Python is C:\\Program Files (x86)\\Python36-32\\python.exe
  70. gyp ERR! find Python - \"C:\\Program Files (x86)\\Python36-32\\python.exe\" could not be run
  71. gyp ERR! find Python checking if the py launcher can be used to find Python 3
  72. gyp ERR! find Python - \"py.exe\" is not in PATH or produced an error
  73. gyp ERR! find Python
  74. gyp ERR! find Python **********************************************************
  75. gyp ERR! find Python You need to install the latest version of Python.
  76. gyp ERR! find Python Node-gyp should be able to find and use Python. If not,
  77. gyp ERR! find Python you can try one of the following options:
  78. gyp ERR! find Python - Use the switch --python=\"C:\\Path\\To\\python.exe\"
  79. gyp ERR! find Python (accepted by both node-gyp and npm)
  80. gyp ERR! find Python - Set the environment variable PYTHON
  81. gyp ERR! find Python - Set the npm configuration variable python:
  82. gyp ERR! find Python npm config set python \"C:\\Path\\To\\python.exe\"
  83. gyp ERR! find Python For more information consult the documentation at:
  84. gyp ERR! find Python https://github.com/nodejs/node-gyp#installation
  85. gyp ERR! find Python **********************************************************
  86. gyp ERR! find Python
  87. gyp ERR! configure error
  88. gyp ERR! stack Error: Could not find any Python installation to use
  89. gyp ERR! stack at PythonFinder.fail (D:\\tk\\company\\dccswap_front_swap\\node_modules\\node-gyp\\lib\\find-python.js:330:47)
  90. gyp ERR! stack at PythonFinder.runChecks (D:\\tk\\company\\dccswap_front_swap\\node_modules\\node-gyp\\lib\\find-python.js:159:21)
  91. gyp ERR! stack at PythonFinder.<anonymous> (D:\\tk\\company\\dccswap_front_swap\\node_modules\\node-gyp\\lib\\find-python.js:228:18)
  92. gyp ERR! stack at PythonFinder.execFileCallback (D:\\tk\\company\\dccswap_front_swap\\node_modules\\node-gyp\\lib\\find-python.js:294:16)
  93. gyp ERR! stack at exithandler (node:child_process:406:5)
  94. gyp ERR! stack at ChildProcess.errorhandler (node:child_process:418:5)
  95. gyp ERR! stack at ChildProcess.emit (node:events:527:28)
  96. gyp ERR! stack at Process.ChildProcess._handle.onexit (node:internal/child_process:289:12)
  97. gyp ERR! stack at onErrorNT (node:internal/child_process:478:16)
  98. gyp ERR! stack at processTicksAndRejections (node:internal/process/task_queues:83:21)
  99. ...

错误信息

  1. sqlit3 安装失败,依赖所在包404
  2. gyp ERR! find Python 没有找到 python
  3. node-pre-gyp ERR! System Windows_NT 10.0.22000

方案:先解决 python 错误,再看错误信息
gyp ERR! find Python - 没有找到 python
根据 find python的报错信息,推断需要安装 >= 3.6.0 以上的python 版本。

我是通过安装 3.6.0 以下版本,它提示得出,并不是推断得出

下载地址:https://www.python.org/downloads/windows/
解决:

  1. 安装 python
  2. 配置 环境变量
  3. cmd 通过 python命令 检测是否安装成功

sqlite 3 安装失败
方案:版本降到 14.15.0

  1. nvm use 14.15.0

错误分析 - 阶段2

错误日志

  1. gyp ERR! find VS
  2. gyp ERR! find VS msvs_version not set from command line or npm config
  3. gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt
  4. gyp ERR! find VS could not use PowerShell to find Visual Studio 2017 or newer, try re-running with '--loglevel silly' for more details
  5. gyp ERR! find VS looking for Visual Studio 2015
  6. gyp ERR! find VS - not found
  7. gyp ERR! find VS not looking for VS2013 as it is only supported up to Node.js 8
  8. gyp ERR! find VS
  9. gyp ERR! find VS **************************************************************
  10. gyp ERR! find VS You need to install the latest version of Visual Studio
  11. gyp ERR! find VS including the \"Desktop development with C++\" workload.
  12. gyp ERR! find VS For more information consult the documentation at:
  13. gyp ERR! find VS https://github.com/nodejs/node-gyp#on-windows
  14. gyp ERR! find VS **************************************************************
  15. gyp ERR! find VS
  16. gyp ERR! configure error
  17. gyp ERR! stack Error: Could not find any Visual Studio installation to use
  18. gyp ERR! stack at VisualStudioFinder.fail (D:\\tk\\company\\dccswap_front_swap\\node_modules\\node-gyp\\lib\\find-visualstudio.js:122:47)
  19. gyp ERR! stack at D:\\tk\\company\\dccswap_front_swap\\node_modules\\node-gyp\\lib\\find-visualstudio.js:75:16
  20. gyp ERR! stack at VisualStudioFinder.findVisualStudio2013 (D:\\tk\\company\\dccswap_front_swap\\node_modules\\node-gyp\\lib\\find-visualstudio.js:363:14)
  21. gyp ERR! stack at D:\\tk\\company\\dccswap_front_swap\\node_modules\\node-gyp\\lib\\find-visualstudio.js:71:14
  22. gyp ERR! stack at D:\\tk\\company\\dccswap_front_swap\\node_modules\\node-gyp\\lib\\find-visualstudio.js:384:16
  23. gyp ERR! stack at D:\\tk\\company\\dccswap_front_swap\\node_modules\\node-gyp\\lib\\util.js:54:7
  24. gyp ERR! stack at D:\\tk\\company\\dccswap_front_swap\\node_modules\\node-gyp\\lib\\util.js:33:16
  25. gyp ERR! stack at ChildProcess.exithandler (child_process.js:315:5)
  26. gyp ERR! stack at ChildProcess.emit (events.js:315:20)
  27. gyp ERR! stack at maybeClose (internal/child_process.js:1048:16)
  28. gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:288:5)
  29. gyp ERR! System Windows_NT 10.0.22000
  30. gyp ERR! command \"C:\\\\Program Files\\\\nodejs\\\\node.exe\" \"D:\\\\tk\\\\company\\\\dccswap_front_swap\\\\node_modules\\\\node-gyp\\\\bin\\\\node-gyp.js\" \"configure\" \"--fallback-to-build\" \"--module=D:\\\\tk\\\\company\\\\dccswap_front_swap\\\\node_modules\\\\sqlite3\\\\lib\\\\binding\\\\node-v83-win32-x64\\\\node_sqlite3.node\" \"--module_name=node_sqlite3\" \"--module_path=D:\\\\tk\\\\company\\\\dccswap_front_swap\\\\node_modules\\\\sqlite3\\\\lib\\\\binding\\\\node-v83-win32-x64\" \"--napi_version=7\" \"--node_abi_napi=napi\" \"--napi_build_version=0\" \"--node_napi_label=node-v83\"
  31. gyp ERR! cwd D:\\tk\\company\\dccswap_front_swap\\node_modules\\sqlite3
  32. gyp ERR! node -v v14.15.0
  33. gyp ERR! node-gyp -v v8.4.1
  34. gyp ERR! not ok
  35. node-pre-gyp ERR! build error
  36. node-pre-gyp ERR! stack Error: Failed to execute 'C:\\Program Files\\nodejs\\node.exe D:\\tk\\company\\dccswap_front_swap\\node_modules\\node-gyp\\bin\\node-gyp.js configure --fallback-to-build --module=D:\\tk\\company\\dccswap_front_swap\\node_modules\\sqlite3\\lib\\binding\\node-v83-win32-x64\\node_sqlite3.node --module_name=node_sqlite3 --module_path=D:\\tk\\company\\dccswap_front_swap\\node_modules\\sqlite3\\lib\\binding\\node-v83-win32-x64 --napi_version=7 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v83' (1)
  37. node-pre-gyp ERR! stack at ChildProcess.<anonymous> (D:\\tk\\company\\dccswap_front_swap\\node_modules\\node-pre-gyp\\lib\\util\\compile.js:83:29)
  38. node-pre-gyp ERR! stack at ChildProcess.emit (events.js:315:20)
  39. node-pre-gyp ERR! stack at maybeClose (internal/child_process.js:1048:16)
  40. node-pre-gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:288:5)
  41. node-pre-gyp ERR! System Windows_NT 10.0.22000
  42. node-pre-gyp ERR! command \"C:\\\\Program Files\\\\nodejs\\\\node.exe\" \"D:\\\\tk\\\\company\\\\dccswap_front_swap\\\\node_modules\\\\node-pre-gyp\\\\bin\\\\node-pre-gyp\" \"install\" \"--fallback-to-build\"
  43. node-pre-gyp ERR! cwd D:\\tk\\company\\dccswap_front_swap\\node_modules\\sqlite3
  44. node-pre-gyp ERR! node -v v14.15.0
  45. node-pre-gyp ERR! node-pre-gyp -v v0.11.0
  46. node-pre-gyp ERR! not ok
  47. Failed to execute 'C:\\Program Files\\nodejs\\node.exe D:\\tk\\company\\dccswap_front_swap\\node_modules\\node-gyp\\bin\\node-gyp.js configure --fallback-to-build --module=D:\\tk\\company\\dccswap_front_swap\\node_modules\\sqlite3\\lib\\bin$ husky install
  48. husky - Git hooks installed
  49. Done in 30.81s.

错误信息

  1. sqlit3 安装失败,依赖所在包404
  2. gyp ERR! find VS - vs 模块缺失
  3. node-pre-gyp ERR! System Windows_NT 10.0.22000

vs模块缺失
解决:安装 window-build-tools(安装后要重启)

  1. npm install --global --production windows-build-tools

TypeScript Error

ts(2349)

错误复现伪代码

  1. export default function useBoolean(def = false) {
  2. // ***
  3. return [isBoolean, exchangeBoolean]
  4. }
  5. const [isBoolean, exchangeBoolean] = useBoolean()
  6. exchangeBoolean()
  7. // error : This expression is not callable.Not all constituents of type 'Ref<boolean> | ((payload?: boolean | undefined) => void)' are callable.ts(2349)

分析:返回的是一个复合类型的数组,类型是里面元素类型的集合,不是所有类型都能函数调用,需要把类型锁定。
解决:通过 const 断言

  1. export default function useBoolean(def = false) {
  2. // ***
  3. return [isBoolean, exchangeBoolean] as const
  4. }