简体   繁体   English

错误:在 Node Express 应用程序中找不到模块“js”

[英]Error: Cannot find module 'js' in Node Express app

I have a NodeJS express app which I am busy with.我有一个 NodeJS express 应用程序,我很忙。

After some development, I see this issue:经过一些发展,我看到了这个问题:

Error: Cannot find module 'js'错误:找不到模块“js”

After some Googling, I did not find any resolution.经过一些谷歌搜索,我没有找到任何解决方案。 The usual resolution methods do not help:通常的解决方法无济于事:

  1. ensure the module is available确保模块可用

  2. remove all modules in node_modules , and reinstall them删除node_modules中的所有模块,然后重新安装它们

What is strange is the Express app works out of the box, no changes, etc needed.奇怪的是 Express 应用程序开箱即用,无需更改等。 Since the module js ( I assume ) is missing somehow , I added this to my dependancy list:由于模块js我假设不知何故丢失,我将其添加到我的依赖项列表中:

{
  "dependencies": {
    "cookie-parser": "~1.4.4",
    "debug": "~2.6.9",
    "js": "^0.1.0",                  <------------I added this
    "express": "^4.16.1",
    "http-errors": "~1.6.3",
    "morgan": "~1.9.1",
    "pug": "2.0.0-beta11",
    "ws": "^7.0.1",
    "fs-extra": "^8.0.1",
    "lodash": "4.17.11",
    "solc": "0.5.8",
    "scrypt": "^6.0.3",
    "python2": "^0.0.1",
    "web3": "1.0.0-beta.37"

  }
}

How do I get this problem?我怎么得到这个问题?

Steps:脚步:

  • I run the app in debug mode (Webstorm), and wait for that app to initialize.我在调试模式 (Webstorm) 下运行该应用程序,并等待该应用程序初始化。 (no errors) (没有错误)

Console Output控制台输出

Debugger listening on ws://127.0.0.1:38687/75fc3e83-8a03-46ff-b769-b49dc880e767
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
  commerce-chain-gui:server Listening on port 3000 +0ms
  • Browse to http://localhost:3000 ( where it is hosted ): page has errors.浏览到http://localhost:3000托管它的地方):页面有错误。

Console Output (appended)控制台输出(附加)

GET / 500 2736.227 ms - 3474

Stacktrace (shown in webpage)堆栈跟踪(显示在网页中)

Cannot find module 'js'
Error: Cannot find module 'js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:668:15)
    at Function.Module._load (internal/modules/cjs/loader.js:591:27)
    at Module.require (internal/modules/cjs/loader.js:723:19)
    at require (internal/modules/cjs/helpers.js:14:16)
    at new View (/mnt/win_c/Users/s2134/IdeaProjects/commerce-chain-gui/node_modules/express/lib/view.js:81:14)
    at Function.render (/mnt/win_c/Users/s2134/IdeaProjects/commerce-chain-gui/node_modules/express/lib/application.js:570:12)
    at ServerResponse.render (/mnt/win_c/Users/s2134/IdeaProjects/commerce-chain-gui/node_modules/express/lib/response.js:1008:7)
    at /mnt/win_c/Users/s2134/IdeaProjects/commerce-chain-gui/routes/index.js:6:7
    at Layer.handle [as handle_request] (/mnt/win_c/Users/s2134/IdeaProjects/commerce-chain-gui/node_modules/express/lib/router/layer.js:95:5)
    at next (/mnt/win_c/Users/s2134/IdeaProjects/commerce-chain-gui/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/mnt/win_c/Users/s2134/IdeaProjects/commerce-chain-gui/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/mnt/win_c/Users/s2134/IdeaProjects/commerce-chain-gui/node_modules/express/lib/router/layer.js:95:5)
    at /mnt/win_c/Users/s2134/IdeaProjects/commerce-chain-gui/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/mnt/win_c/Users/s2134/IdeaProjects/commerce-chain-gui/node_modules/express/lib/router/index.js:335:12)
    at next (/mnt/win_c/Users/s2134/IdeaProjects/commerce-chain-gui/node_modules/express/lib/router/index.js:275:10)
    at Function.handle (/mnt/win_c/Users/s2134/IdeaProjects/commerce-chain-gui/node_modules/express/lib/router/index.js:174:3)
    at router (/mnt/win_c/Users/s2134/IdeaProjects/commerce-chain-gui/node_modules/express/lib/router/index.js:47:12)
    at Layer.handle [as handle_request] (/mnt/win_c/Users/s2134/IdeaProjects/commerce-chain-gui/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/mnt/win_c/Users/s2134/IdeaProjects/commerce-chain-gui/node_modules/express/lib/router/index.js:317:13)
    at /mnt/win_c/Users/s2134/IdeaProjects/commerce-chain-gui/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/mnt/win_c/Users/s2134/IdeaProjects/commerce-chain-gui/node_modules/express/lib/router/index.js:335:12)
    at next (/mnt/win_c/Users/s2134/IdeaProjects/commerce-chain-gui/node_modules/express/lib/router/index.js:275:10)

How can I resolve this?我该如何解决这个问题?

Update更新

I created a new Express app, copied over everything in pieces to possibly find the culprit...我创建了一个新的 Express 应用程序,将所有内容分片复制以可能找到罪魁祸首......

The app works (in the new express app), but I did not find out what caused the problem.该应用程序有效(在新的 Express 应用程序中),但我没有找出导致问题的原因。

This is still an issue, as no solution has yet been found这仍然是一个问题,因为尚未找到解决方案

I think you have to install it globally npm i -g js and then you can use it in the console.我认为您必须全局安装npm i -g js ,然后才能在控制台中使用它。

From the link you send : js is aa better alternative to node -p and node -p is used in system terminal to print the result of a script从您发送的链接: js is aa better alternative to node -p并且 node -p 用于系统终端中打印脚本的结果

UPDATE : did you correctly register your view engine ?更新:您是否正确注册了您的视图引擎? in the stacktace you give, you can see :在您提供的 stacktace 中,您可以看到:

at new View ([...]/node_modules/express/lib/view.js:81:14)

when looking the code in this file there is :查看此文件中的代码时,有:

    if (!opts.engines[this.ext]) {
      // load engine
      var mod = this.ext.substr(1)
      debug('require "%s"', mod) // Here is a debug function

      // default engine export
      var fn = require(mod).__express // Here is your error

      if (typeof fn !== 'function') {
          throw new Error('Module "' + mod + '" does not provide a view engine.')
      }

      opts.engines[this.ext] = fn
    }

you can view express debug by launching your app with DEBUG=express:* node index.js您可以通过使用DEBUG=express:* node index.js启动您的应用程序来查看快速调试

UPDATE 2 : it seems that your are trying to load a view with the .js extension : express use the extension to find the view engine => for exemple, the file file.pug will be render with the pug engine更新 2:您似乎正在尝试使用.js扩展名加载视图:快速使用扩展名查找视图引擎 => 例如,文件file.pug将使用file.pug引擎呈现

Welcome.欢迎。 Despite this, I have a little more information.尽管如此,我还是有更多的信息。

The problem appeared when I tried to transfer my old page to the nodejs express engine using view engine ejs.当我尝试使用视图引擎 ejs 将旧页面传输到 nodejs express 引擎时出现问题。 As it turned out, the engine for some reason tolerates some javascript files of the site as files to be repaired.事实证明,由于某种原因,引擎允许站点的某些 javascript 文件作为要修复的文件。

A lot should explain this log dump:很多应该解释这个日志转储:

Error: Cannot find module 'ico'
Require stack:
- /root/bot/node_modules/express/lib/view.js
- /root/bot/node_modules/express/lib/application.js
- /root/bot/node_modules/express/lib/express.js
- /root/bot/node_modules/express/index.js
- /root/bot/core.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:892:15)
    at Function.Module._load (internal/modules/cjs/loader.js:785:27)
    at Module.require (internal/modules/cjs/loader.js:956:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at new View (/root/bot/node_modules/express/lib/view.js:81:14)
    at Function.render (/root/bot/node_modules/express/lib/application.js:570:12)
    at ServerResponse.render (/root/bot/node_modules/express/lib/response.js:1012:7)
    at ressend (/root/bot/include/routes/index.js:27:9)
    at Layer.handle [as handle_request] (/root/bot/node_modules/express/lib/router/layer.js:95:5)
    at next (/root/bot/node_modules/express/lib/router/route.js:137:13)
  express:router dispatching GET /server/js/bootstrap.min.js +13ms
  express:router query  : /server/js/bootstrap.min.js +1ms
  express:router expressInit  : /server/js/bootstrap.min.js +0ms
  express:router session  : /server/js/bootstrap.min.js +0ms
  express:view require "js" +0ms

Error: Cannot find module 'js'
Require stack:
- /root/bot/node_modules/express/lib/view.js
- /root/bot/node_modules/express/lib/application.js
- /root/bot/node_modules/express/lib/express.js
- /root/bot/node_modules/express/index.js
- /root/bot/core.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:892:15)
    at Function.Module._load (internal/modules/cjs/loader.js:785:27)
    at Module.require (internal/modules/cjs/loader.js:956:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at new View (/root/bot/node_modules/express/lib/view.js:81:14)
    at Function.render (/root/bot/node_modules/express/lib/application.js:570:12)
    at ServerResponse.render (/root/bot/node_modules/express/lib/response.js:1012:7)
    at ressend (/root/bot/include/routes/index.js:27:9)
    at Layer.handle [as handle_request] (/root/bot/node_modules/express/lib/router/layer.js:95:5)
    at next (/root/bot/node_modules/express/lib/router/route.js:137:13)
  express:router dispatching GET /server/js/demo/widgets.js +3ms
  express:router query  : /server/js/demo/widgets.js +0ms
  express:router expressInit  : /server/js/demo/widgets.js +0ms
  express:router session  : /server/js/demo/widgets.js +0ms
  express:router dispatching GET /server/plugins/sparkline/jquery.sparkline.min.js +1ms
  express:router query  : /server/plugins/sparkline/jquery.sparkline.min.js +0ms
  express:router expressInit  : /server/plugins/sparkline/jquery.sparkline.min.js +0ms
  express:router session  : /server/plugins/sparkline/jquery.sparkline.min.js +0ms
  express:view require "js" +0ms
  express:view require "js" +1ms
  express:router dispatching GET /server/plugins/flot-charts/jquery.flot.min.js +5ms
  express:router query  : /server/plugins/flot-charts/jquery.flot.min.js +1ms
  express:router expressInit  : /server/plugins/flot-charts/jquery.flot.min.js +0ms
  express:router session  : /server/plugins/flot-charts/jquery.flot.min.js +0ms
  express:router dispatching GET /server/js/demo/nifty-demo.min.js +0ms
  express:router query  : /server/js/demo/nifty-demo.min.js +1ms
  express:router expressInit  : /server/js/demo/nifty-demo.min.js +0ms
  express:router session  : /server/js/demo/nifty-demo.min.js +0ms
  express:router dispatching GET /server/plugins/flot-charts/jquery.flot.tooltip.min.js +0ms
  express:router query  : /server/plugins/flot-charts/jquery.flot.tooltip.min.js +0ms
  express:router expressInit  : /server/plugins/flot-charts/jquery.flot.tooltip.min.js +0ms
  express:router session  : /server/plugins/flot-charts/jquery.flot.tooltip.min.js +0ms
(...)

FIX: The problem results from incorrect paths to static files.修复:问题是由错误的静态文件路径引起的。 Step 1: Add all static catalogs with example:第 1 步:添加所有静态目录,例如:

config.www.static_directories.forEach((dir)=>{
  app.use('/'+dir, express.static(path.join(config.www.directory+dir)))
})

or或者

app.use(express.static('public'))

Step: 2 Correct all paths to static files so that they refer to the root directory, eg <script src="/plugins/pace/pace.min.js"></script>步骤:2 更正静态文件的所有路径,使它们指向根目录,例如<script src="/plugins/pace/pace.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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