簡體   English   中英

Node.js - “找不到模塊‘module.js’”

[英]Node.js - “Cannot find module 'module.js' ”

我正在使用 Heroku,但出現此錯誤..

2016-10-10T03:34:11.188366+00:00 app[web.1]: Error: Cannot find module 'module1.js'
2016-10-10T03:34:11.188369+00:00 app[web.1]:     at Function.Module._resolveFilename (module.js:339:15)
2016-10-10T03:34:11.188370+00:00 app[web.1]:     at Function.Module._load (module.js:290:25)
2016-10-10T03:34:11.188371+00:00 app[web.1]:     at Module.require (module.js:367:17)
2016-10-10T03:34:11.188371+00:00 app[web.1]:     at require (internal/module.js:20:19)
2016-10-10T03:34:11.188372+00:00 app[web.1]:     at Object.<anonymous> (/app/server.js:42:15)
2016-10-10T03:34:11.188373+00:00 app[web.1]:     at Module._compile (module.js:413:34)
2016-10-10T03:34:11.188374+00:00 app[web.1]:     at Object.Module._extensions..js (module.js:422:10)
2016-10-10T03:34:11.188374+00:00 app[web.1]:     at Module.load (module.js:357:32)
2016-10-10T03:34:11.188375+00:00 app[web.1]:     at Function.Module._load (module.js:314:12)
2016-10-10T03:34:11.188375+00:00 app[web.1]:     at Function.Module.runMain (module.js:447:10)

應用程序.js

/**CUSTOM_MODULES**/

var module1 = require('module1.js');

/**MODULES_END**/

模塊1.js

function module1(){
    //My code
}

module.exports = module1;

module1.js 與我的 app.js 位於同一目錄中。

我嘗試了無數的事情,但似乎沒有任何效果。

我試過:

 var module1 = require('module1.js');
 var module1 = require('./module1.js');
 var module1 = require('../module1.js');
 var module1 = require('module1');

package.json : app.js

{
  "name": "nano-server",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "dependencies": {
    "express": "^4.14.0",
    "mysql": "^2.11.1",
    "socket.io": "^1.4.8",  
    "module1": "0.0.0"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "author": "",
  "license": "ISC"
}

package.json:module1

 {
  "name": "module1",
  "version": "0.0.0",
  "description": "get something",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": {
    "name": "sergey"
  },
  "license": "BSD-2-Clause",
  "readme": "ERROR: No README data found!",
  "_id": "module1@0.0.0",
  "dist": {
    "shasum": "dc9b96f8a43b596bf735da4d2004ba410429bde0",
    "tarball": "https://registry.npmjs.org/module1/-/module1-0.0.0.tgz"
  },
  "_from": "module1@latest",
  "_npmVersion": "1.3.11",
  "_npmUser": {
    "name": "sergey-user",
    "email": "aaa@mail.ru"
  },
  "maintainers": [
    {
      "name": "sergey-user",
      "email": "aaa@mail.ru"
    }
  ],
  "directories": {},
  "_shasum": "dc9b96f8a43b596bf735da4d2004ba410429bde0",
  "_resolved": "https://registry.npmjs.org/module1/-/module1-0.0.0.tgz"
}

目錄

_server
    .git
    node_modules
        .bin
        express
        module1
            index.js
            package.json
        mysql
        node-mysql
        node-uuid
        socket.io
    package.json
    server.js

試試 var module1 = require('./module1');

安裝npm -g並在之后重試。

將您的模塊作為應用程序的依賴項添加將解決此問題。 要實現這一點,請嘗試以下命令。

npm install module1 --save

希望這有幫助!

就我而言,在嘗試了一切但沒有成功后,更新節點有所幫助(v10.3.1 -> v12.13.1)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM