找不到模块,指向文件

时间:2018-03-20 22:17:06

标签: node.js

我想要包含文件" coinModel.js"在文件夹" model"。它给出了以下错误。我在linux和Windows 10上有错误。

Error: Cannot find module '/model/Coinmodel'
at Function.Module._resolveFilename (module.js:542:15)
at Function.Module._load (module.js:472:25)
at Module.require (module.js:585:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (C:\_node\Api_coinmarktecap\index.js:7:17)
at Module._compile (module.js:641:30)
at Object.Module._extensions..js (module.js:652:10)
at Module.load (module.js:560:32)
at tryModuleLoad (module.js:503:12)
at Function.Module._load (module.js:495:3)

的package.json

    {
  "name": "coinhistory",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/HowestNMCT/proj-VolckaertAchiel.git"
  },
  "author": "Achiel Volckaert",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/HowestNMCT/proj-VolckaertAchiel/issues"
  },
  "homepage": "https://github.com/HowestNMCT/proj-VolckaertAchiel#readme",
  "dependencies": {
    "express": "^4.16.3",
    "model-generator": "^0.7.0",
    "mysql": "^2.15.0",
    "node-schedule": "^1.2.5",
    "request": "^2.83.0",
    "tedious": "^2.1.1"
  }
}

index.js

let coinmodel = require("/model/coinModel");
let Cast = require("model-generator")().Cast;
let mysql = require('mysql');
let express = require('express');
let app = express();   

let con = mysql.createConnection({
    host: "host.be",
    user: "user",
    password: "supersecretpassword",
    database: "mydb"
});
con.connect(function (err) {
    if (err) throw err;
});
let server = app.listen(6000 , function () {
    let host = server.address().address;
    let port = server.address().port;

    console.log("Example app listening at http://%s:%s", host, port)

});

1 个答案:

答案 0 :(得分:1)

您确定/model/Coinmodel是正确的路径吗?如果是本地model目录,则路径应为./model/Coinmodel。另外,请确保该文件是_actually Coinmodel.js而不是coinmodel.js