【问题标题】:Installed socket.io with npm, but require() fails no matter what使用 npm 安装了 socket.io,但无论如何 require() 都会失败
【发布时间】:2023-02-25 13:31:40
【问题描述】:

我有一个持续运行的 tcp 原始套接字,我可以使用现有的应用程序连接到它。我正在尝试在我的 photoshop 插件中使用 socket.io-client,但安装后,无论我使用哪种相对路径或使用什么,我的 require() 语句都会失败。 npm 没有发现任何问题,我也尝试过重新安装。我必须使用 socket.io,因为 Photoshop UXP 提供的所有网络 IO 模块都无法连接到原始 TCP。

过程

  1. 我在与我的 index.js 和现有 package.json 相同的文件夹中启动了一个终端。
  2. 我运行 npm install socket.io-client
  3. 成功。我现在有一个 node_modules 文件夹和一个 package-lock.json
  4. 我无法在项目的任何地方使用 socket.io-client。

    在 index.js 中,这些失败了,而我很确定它们不应该:

    const io = require('socket.io-client')
    const io = require('./node_modules/socket.io-client')
    

    等等,即使使用绝对路径。

    我尝试过的其他事情:

    • 正在卸载/删除 node_modules 文件夹和包锁并重试。
    • 正在使用 -global 参数进行安装。

    我在网上到处求助,但找不到有类似问题的人,请帮忙!

【问题讨论】:

    标签: javascript socket.io node-modules npm-install photoshop


    【解决方案1】:

    使用解构赋值:

    const { io } = require("socket.io-client");
    

    参考:Socket IO Documentation

    【讨论】:

      猜你喜欢
      • 2015-07-07
      • 1970-01-01
      • 1970-01-01
      • 2022-10-26
      • 2014-04-26
      • 1970-01-01
      • 2016-03-26
      • 2013-05-04
      • 1970-01-01
      相关资源
      最近更新 更多