最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot find module in Electron - Stack Overflow

programmeradmin4浏览0评论

I am currently working on Electron with Babylon. I found this repo which I basically used as a boilerplate for my own project. Everything was working well until I tried to add jquery.pep.js for other needs. I keep on having this mistake :

Uncaught Error: Cannot find module 'jquery.pep.js'

I installed both libraries with "npm i -S jquery" and "npm i -S jquery.pep.js". In order to make jquery works, I added this script in the head of my index.html

<script> delete window.module; </script>

and this line in the top of my main.js :

window.$ = window.jQuery = require('jquery');

Now, jquery is working fine but for some reasons, jquery.pep.js module still can't be found. I tried to use 'require' but I have the same error

main.js

window.$ = window.jQuery = require('jquery'); 
var pep = require('jquery.pep.js');

Project structure

css/
img/
js/
-- main.js
node_modules/
index.html
index.js
package.json
renderer.js

I am currently working on Electron with Babylon. I found this repo which I basically used as a boilerplate for my own project. Everything was working well until I tried to add jquery.pep.js for other needs. I keep on having this mistake :

Uncaught Error: Cannot find module 'jquery.pep.js'

I installed both libraries with "npm i -S jquery" and "npm i -S jquery.pep.js". In order to make jquery works, I added this script in the head of my index.html

<script> delete window.module; </script>

and this line in the top of my main.js :

window.$ = window.jQuery = require('jquery');

Now, jquery is working fine but for some reasons, jquery.pep.js module still can't be found. I tried to use 'require' but I have the same error

main.js

window.$ = window.jQuery = require('jquery'); 
var pep = require('jquery.pep.js');

Project structure

css/
img/
js/
-- main.js
node_modules/
index.html
index.js
package.json
renderer.js

Share Improve this question edited Jan 3, 2018 at 11:02 Nagah asked Jan 3, 2018 at 10:47 NagahNagah 731 gold badge1 silver badge8 bronze badges 3
  • We don't have enough info to help you. What we see is that the module is not found. We also need to see the exact file that tries to load the module and perhaps also the folder structure of your project. See stackoverflow./help/mcve – Adelin Commented Jan 3, 2018 at 10:52
  • Ok thanks, I just added some informations. As far as the problem doesn't concern WebGL, I only kept my main.js file and imported the librairies – Nagah Commented Jan 3, 2018 at 11:06
  • And you confirm that by cd-ing to the js folder, running node and then printing module.paths, you have 'jquery.pep.js' somewhere in the paths listed by this mand? – Adelin Commented Jan 3, 2018 at 11:10
Add a ment  | 

1 Answer 1

Reset to default 12

You are requesting something and node is not able to find it. You can read this dedicated article on requiring modules in node, which explains it quite simply. Quoting:

When we require a 'find-me' module, without specifying a path:

require('find-me');

Node will look for find-me.js in all the paths specified by module.paths — in order.

$ node
> module.paths
[ '/Users/samer/learn-node/repl/node_modules',
  '/Users/samer/learn-node/node_modules',
  '/Users/samer/node_modules',
  '/Users/node_modules',
  '/node_modules',
  '/Users/samer/.node_modules',
  '/Users/samer/.node_libraries',
  '/usr/local/Cellar/node/7.7.1/lib/node' ]

The paths list is basically a list of node_modules directories under every directory from the current directory to the root directory. It also includes a few legacy directories whose use is not remended.

If Node can’t find find-me.js in any of these paths, it will throw a “cannot find module error.”

~/learn-node $ node
> require('find-me')
Error: Cannot find module 'find-me'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at repl:1:1
    at ContextifyScript.Script.runInThisContext (vm.js:23:33)
    at REPLServer.defaultEval (repl.js:336:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.onLine (repl.js:533:10)

Make sure you have your module installed somewhere in what node knows as module.paths, or reference the file by providing absolute path.

发布评论

评论列表(0)

  1. 暂无评论