简体   繁体   中英

Cannot find module 'socket.io'

I am aware of the other questions about this issue . The answers on them are of no help, however. I've been dealing with Node and npm for a few years and I've never come across something so confusing. This is my first time dealing with socket.io, however.

I've installed socket.io as a dependency on a git submodule of my project:

npm install --save socket.io

Then I require it:

const io = require("socket.io")(8099);

However, when I attempt to run the app, I get an error:

[error:app 10:31:32.339 C:\Users\<username>\dev\projects\js-ps-app\generator\generator-core\app.js:336:37] Unable to load plugin at 'C:\Users\<username>\dev\projects\js-ps-app\generator\plugins\generator-starter': Could not load plugin at path 'C:\Users\<username>\dev\projects\js-ps-app\generator\plugins\generator-starter': Cannot find module 'socket.io'

When attempting to reference the module relatively:

const io = require(".\\node_modules\\socket.io")(8099);

The error seems to shift to engine.io , which isn't even referenced in my project:

[error:app 10:38:40.630 C:\Users\<username>\dev\projects\js-ps-app\generator\generator-core\app.js:336:37] Unable to load plugin at 'C:\Users\<username>\dev\projects\js-ps-app\generator\plugins\generator-starter': Could not load plugin at path 'C:\Users\<username>\dev\projects\js-ps-app\generator\plugins\generator-starter': Cannot find module 'engine.io'

I am at a loss to know what to do.

For context: This is in the middle of working on Davide Barranca 's Native Photoshop Apps video course—Lesson #15, specifically. Adobe Generator is working and connecting to Adobe Photoshop just fine. You can see where this project stands currently at its repo .

For example, here's the main repo's package.json :

{
  "name": "js-ps-app",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint",
    "nwdev": "concurrently --kill-others --raw \"npm:serve\" \".\\node_modules\\.bin\\run .\\nwdev\" --success 'first'",
    "generator": "node --inspect .\\generator\\generator-core\\app -f .\\generator\\plugins -v"
  },
  "dependencies": {
    "@mdi/font": "^3.6.95",
    "core-js": "^3.6.5",
    "roboto-fontface": "*",
    "vue": "^2.6.11",
    "vue-router": "^3.2.0",
    "vue-the-mask": "^0.11.1",
    "vuetify": "^2.2.11",
    "vuex": "^3.4.0"
  },
  "devDependencies": {
    "7zip-bin-win": "^2.2.0",
    "@vue/cli-plugin-babel": "~4.5.0",
    "@vue/cli-plugin-eslint": "~4.5.0",
    "@vue/cli-plugin-router": "~4.5.0",
    "@vue/cli-plugin-vuex": "~4.5.0",
    "@vue/cli-service": "~4.5.0",
    "babel-eslint": "^10.1.0",
    "concurrently": "^5.3.0",
    "eslint": "^6.7.2",
    "eslint-plugin-vue": "^7.1.0",
    "node-sass": "^4.12.0",
    "nw-vue-devtools-prebuilt": "0.0.10",
    "nwjs-builder-phoenix": "^1.15.0",
    "sass": "^1.19.0",
    "sass-loader": "^8.0.2",
    "vue-cli-plugin-vuetify": "~2.0.7",
    "vue-template-compiler": "^2.6.11",
    "vuetify-loader": "^1.3.0"
  }
}

And here's generator-starter 's package.json , which is what needs socket.io :

{
  "name": "generator-starter",
  "version": "1.0.0",
  "description": "Blank Adobe Generator Plugin",
  "main": "main.js",
  "generator-core-version": "~3",
  "menu": {
    "id": "generator-starter",
    "label": "Generator Plugin"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Davide Barranca",
  "license": "ISC",
  "dependencies": {
    "engine.io": "^4.0.1",
    "socket.io": "^2.3.0"
  }
}

"Running the app" means running npm run generator from the root of the project. It uses node to run app.js inside generator\\generator-core , which takes the sibling directory generator\\plugins as an argument.

Disclaimer: It's impossible for me to reproduce your error given that a) I'm on Mac, b) I don't have Photoshop and c) I didn't watch the video tutorial. Still, if you bare with me, I'm pretty sure the following will hint you in the proper direction.

The error you are getting is not actually related to socket.io or engine.io. Any dependency that you would declare from your plugin (and that it is not already a dependency of the generator) would trigger the same issue.

The problem is essentially that your plugin's node_modules directory is not actually used for dependency resolution. I'd expect that you can confirm this by adding something like console.log(require.paths) at the top of your plugin's source file.

Then why is it not used? Because generator hijack modules resolutions to disallow loading dependencies from outside of your plugin's node_modules (see Generator's main file: app.js ). Note that this is a recent change ( July 15, 2020 ) which explains why the setup you use may work in the tutorial but no longer works.

Now, I'm not sure exactly what's wrong with generator's patched module resolution. Apparently, it doesn't support symlinked plugins directories , but then again, I don't think this is your case. Could it be that some of your dependency may have been hoisted to a parent node_module directory? Another thing is that the construction of safe paths seems to include the "plugins" folder itself (rather than the directory of a specific plugin)... Could be a problem.

Anyway, at this point, I suggest that you 1) try downgrading generator-core's source to tag 3.12.0, 2) if it works, open a ticket on generator-core's bug tracker, then 3) revert generator-core to currently version (this is a security fix, so you should not ignore it) and try to work out a solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM