找不到模块(带有自己的模型)

时间:2016-02-11 02:23:15

标签: node.js express npm

这可能是一个极其重复的问题,但我对node(尤其是express感到满意,而且任何相关问题都可以解决我的问题:

拥有这个项目结构:

├── app
│   ├── models
│   │   ├── user.js
├── package.json
├── server.js

我运行服务器,我有这个例外

jscherman@jscherman:~/Work/express-webservice-demo$ node server.js
Server started on port 8080

Error: Cannot find module './app/models/user'
    at Function.Module._resolveFilename (module.js:339:15)
    at Function.Module._load (module.js:290:25)
    at Module.require (module.js:367:17)
    at require (internal/module.js:16:19)
    at Object.<anonymous> (/home/jscherman/Work/express-webservice-demo/server.js:38:12)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)

我将模型链接如下:

var user = require('./app/models/user');

我正在使用这些版本:

jscherman@jscherman:~/Work/express-webservice-demo$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 15.10
Release:        15.10
Codename:       wily
jscherman@jscherman:~/Work/express-webservice-demo$ node --version
v5.5.0

我不明白发生了什么。我已经明白./表示“从server.js路径开始”,但它不起作用。有什么帮助吗?

修改

以下代码

console.log('__dirname:', __dirname, '\nprocess.cwd():', process.cwd())

输出:

__dirname: /home/jscherman/Work/express-webservice-demo 
process.cwd(): /home/jscherman/Work/express-webservice-demo

server.js文件:

var express = require('express');
var app = express();
var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var port = process.env.PORT || 8080;

var router = express.Router();

router.use(function (req, res, next) {
    console.log('This is a simple middleware');
    next();
})


router.get('/', function(req, res) {
    res.json({ message: 'Welcome to the api' });   
});

app.use('/api', router);


app.listen(port, function () {
  console.log('Server started running on port 3000!');
});


var mongoose   = require('mongoose');
mongoose.connect('mongodb://localhost:27017/prueba', function(){
    console.log('Database connected successfully')
})

var User = require('./app/models/user');

最后,user.js(在模型文件夹内)

var mongoose     = require('mongoose');
var Schema       = mongoose.Schema;

var UserSchema   = new Schema({
    username: String,
    surname: String
});

module.exports = mongoose.model('user', UserSchema);

检查缓存:

// with console.log(require.cache), output:
{ '/home/jscherman/Work/express-webservice-demo/server.js': 
   Module {
     id: '.',
     exports: {},
     parent: null,
     filename: '/home/jscherman/Work/express-webservice-demo/server.js',
     loaded: false,
     children: [],
     paths: 
      [ '/home/jscherman/Work/express-webservice-demo/node_modules',
        '/home/jscherman/Work/node_modules',
        '/home/jscherman/node_modules',
        '/home/node_modules',
        '/node_modules' ] } }

0 个答案:

没有答案