新手 请教“TypeError: express is not a function”
发布于 9 年前 作者 coolpanda2015 10369 次浏览 最后一次编辑是 8 年前 来自 问答

我是新手,在网上看到一个node.js的教程,初始化一个项目,跟着教程打下来,代码启动总是提示错误:

D:\nodejs\cms\app.js:3
var app = express();
          ^

TypeError: express is not a function
    at Object.<anonymous> (D:\nodejs\cms\app.js:3:11)
    at Module._compile (module.js:410:26)
    at Object.Module._extensions..js (module.js:417:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Module.require (module.js:354:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (D:\nodejs\cms\bin\www:3:11)
    at Module._compile (module.js:410:26)
    at Object.Module._extensions..js (module.js:417:10) 



app.js代码如下:
var express = require('./config/express');
var app = express();
module.exports = app;


/config/express.js代码如下:
var express = require('express');
var bodyParser = require('body-parser');
module.express = function(){
	console.log('init express...');
	var app = express();
	app.use(bodyParser.json());
	app.use(function(req, res, next){
		res.status(404);
		try {
			return res.json('Not found');
		} catch(e) {
			console.error('404 set header after sent');
		}
	});
	return app;
}; 

bin/www代码如下:
#!/usr/bin/env node
var app = require('../app');
var config = require('../config/config');
app.listen(config.port, function(){
	console.log('app started, listening on port:' + config.port);
});
\n```
 
package.json :
{
  "name": "cms",
  "version": "1.0.0",
  "description": "demo app",
  "main": "bin/www",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "abc",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.15.0",
    "express": "^4.13.4"
  }
}

请麻烦帮忙看一下,  谢谢。
3 回复

module.express应该是module.exports 或者写成module.exports.express = …

@xuduo35 您说的对,多谢。

回到顶部