【问题标题】:Build fails with resolve errors构建失败并出现解决错误
【发布时间】:2021-11-27 18:37:30
【问题描述】:

我正在尝试在一个纤细的组件中设置 socket.io-client。

文档说我可以做到import { io } from 'socket.io-client'

但是当我尝试使用 serve 目标运行应用程序时,命令失败并显示以下日志:

Bundle complete. Watching for file changes...
Bundling...
'bufferutil' is imported by bufferutil?commonjs-external, but could not be resolved – treating it as an external dependency
'utf-8-validate' is imported by utf-8-validate?commonjs-external, but could not be resolved – treating it as an external dependency
Circular dependency: node_modules\util\util.js -> node_modules\util\node_modules\inherits\inherits.js -> node_modules\util\util.js
Circular dependency: node_modules\util\util.js -> node_modules\util\node_modules\inherits\inherits.js -> C:\Users\munda\Documents\upwork\upwork-gameshow\node_modules\util\util.js?commonjs-proxy -> node_modules\util\util.js
@rollup/plugin-typescript: Rollup 'sourcemap' option must be set to generate source maps.
No name was provided for external module 'tty' in output.globals – guessing 'tty'
No name was provided for external module 'os' in output.globals – guessing 'os'
No name was provided for external module 'fs' in output.globals – guessing 'fs'
No name was provided for external module 'child_process' in output.globals – guessing 'require$$0'
No name was provided for external module 'http' in output.globals – guessing 'require$$1'
No name was provided for external module 'https' in output.globals – guessing 'require$$2'
No name was provided for external module 'net' in output.globals – guessing 'net'
No name was provided for external module 'tls' in output.globals – guessing 'tls'
No name was provided for external module 'crypto' in output.globals – guessing 'require$$0$3'
No name was provided for external module 'zlib' in output.globals – guessing 'zlib'
No name was provided for external module 'bufferutil' in output.globals – guessing 'require$$1$1'
No name was provided for external module 'stream' in output.globals – guessing 'require$$0$2'
No name was provided for external module 'utf-8-validate' in output.globals – guessing 'require$$0$1'
Creating a browser bundle that depends on Node.js built-in modules ("tty", "os", "http", "https", "zlib" and "stream"). You might need to include https://github.com/snowpackjs/rollup-plugin-polyfill-node

生成应用程序的步骤:

  1. 使用yarn create nx-workspace 启动一个 nx 工作区
  2. 使用yarn add @nxext/svelte 安装svelte 生成器,链接:https://nx.dev/community#:~:text=the%20Nx%20workflow-,%40nxext/svelte,-Nx%20plugin%20to
  3. 使用安装的生成器生成svelte 应用程序。
  4. 在组件中添加socket.io 客户端代码(文档中提到的基本套接字初始化)。
  5. 由于我的服务器和客户端不会托管在同一个域上,因此添加 import { io } from 'socket.io-client' 并使用 const socket = io(SERVER_URL) 初始化套接字(在我的例子中是 http://localhost:3000)
  6. 使用yarn socket.io-client安装socket.io客户端。
  7. 使用nx run-many --target=serve --all --parallel 运行serve 目标,手指交叉。

结果:上面提到的日志。

我错过了什么?

【问题讨论】:

    标签: socket.io svelte nx-workspace


    【解决方案1】:

    我需要安装以下缺少的依赖项:

    • bufferutil
    • utf-8-validate

    一个简单的yarn add bufferutil utf-8-validate 为我修复了它。这在docs for socket.io-client package 或socket.io 官方文档网站中有所提及。

    这确实修复了我的 PC(Windows)上的构建,但我无法在 mac 上运行相同的东西。我尝试删除node_modulesyarn.lock,重新运行yarn

    最后我不得不走 CDN 路线。

    我就是这样做的:

    1. 在函数中移动套接字初始化逻辑。
    <script>
      ...
      const initialiseSocket = () => {
        socket = io('http://localhost:3000');
    
        socket.on('messages', (data) => {
          //...
        });
      }
    </script>
    
    1. 添加一个 svelte:head 标记以从 CDN 加载 socket-io.client 并将函数传递给此脚本的 on:load
    <svelte:head>
      <script
        src="https://cdn.socket.io/4.2.0/socket.io.min.js"
        integrity="sha384-PiBR5S00EtOj2Lto9Uu81cmoyZqR57XcOna1oAuVuIEjzj0wpqDVfD0JA9eXlRsj"
        crossorigin="anonymous"
        on:load={initialiseSocket}></script>
    </svelte:head>
    

    【讨论】:

      猜你喜欢
      • 2020-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      • 2018-05-26
      • 1970-01-01
      相关资源
      最近更新 更多