错误:在Node Express应用中找不到模块'js'

时间:2019-06-27 07:03:30

标签: javascript node.js npm

我有一个忙于使用的NodeJS Express应用程序。

经过一些开发,我看到了这个问题:

  

错误:找不到模块“ js”

经过谷歌搜索后,我没有找到任何解决方案。常用的解决方法无济于事:

  1. 确保模块可用

  2. 删除node_modules中的所有模块,然后重新安装

奇怪的是Express应用程序开箱即用,无需更改等。由于模块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"

  }
}

如何解决此问题?

步骤:

  • 我以调试模式(Webstorm)运行该应用程序,然后等待该应用程序初始化。 (没有错误)

控制台输出

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
  • 浏览到http://localhost:3000(托管它的 ):页面有错误。

控制台输出(附加)

GET / 500 2736.227 ms - 3474

Stacktrace (显示在网页中)

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)

我该如何解决?

更新

我创建了一个新的Express应用,将所有内容分块复制,以找出罪魁祸首...

该应用程序正常工作(在新的Express应用程序中),但我没有找到导致问题的原因。

这仍然是一个问题,因为尚未找到解决方案

2 个答案:

答案 0 :(得分:0)

我认为您必须在全球范围内安装它 npm i -g js 然后您可以在控制台中使用它。

通过您发送的链接:js is a a better alternative to node -p和节点-p在系统终端to print the result of a script中使用

更新:您是否正确注册了视图引擎?在您提供的堆栈中,您可以看到:

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

在此文件中查找代码时,将显示:

    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
    }

您可以通过使用DEBUG=express:* node index.js启动应用来查看快速调试

更新2:您似乎正在尝试使用扩展名.js加载视图:例如,使用扩展名来查找视图引擎=>例如,文件file.pug将使用哈巴狗引擎

答案 1 :(得分:0)

欢迎。尽管如此,我还有更多信息。

当我尝试使用视图引擎ejs将旧页面传输到nodejs表达引擎时,出现了问题。事实证明,该引擎出于某种原因允许网站的某些javascript文件作为要修复的文件。

很多人应该解释此日志转储:

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: 该问题是由错误的静态文件路径引起的。 步骤1: 使用示例添加所有静态目录:

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

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

步骤:2 更正静态文件的所有路径,以便它们引用根目录,例如 <script src="/plugins/pace/pace.min.js"></script>