我尝试在我的项目中安装新模块,但是一旦安装并需要它,我就会收到错误
Cannot find the module 'xxxxx'
早些时候,我已经安装了 express 和 body-parser 等模块,这些模块都运行顺畅,但我看不到它们需要新的模块。我查看了我的_node_modules_文件夹,在安装新模块后,创建了该模块的新文件。似乎 require 无法要求它,因为有些奇怪,路由可能?
结构:
-project
-client
-front-end stuff
-server
-node_modules
-src
-.DS_Store
-Dockerfile
-package-lock.json
-package.json
当我运行 npm install 命令时,我在服务器文件夹中。
server.js文件:
'use strict';
// NPM dependencies.
var express = require('express'),
bodyParser = require('body-parser'),
morgan = require('morgan'),
sequelize = require('sequelize'),
passport = require('passport'),
jwt = require('jsonwebtoken'),
path = require('path'),
models = require("./models"),
compression = require('compression');
// App related modules.
var hookJWTStrategy = require('./services/passportStrategy');
// Initializations.
var app = express();
// Parse as urlencoded and json.
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Hook up the HTTP logger.
app.use(morgan('dev'));
// Hook up Passport.
app.use(passport.initialize());
// Hook the passport JWT strategy.
hookJWTStrategy(passport);
// Set the static files location.
app.use(express.static(__dirname + '../public'));
// Bundle API routes.
app.use('/api', require('./routes/api')(passport));
// Catch all route.
app.get('*', function(req, res) {
res.send('Hello there');
});
// sync() will create all table if they doesn't exist in database
models.sequelize.sync().then(function () {
// Start the server.
app.listen('3000', function() {
console.log('Magic happens at http://localhost:3000s/! We are now all now doomed!');
});
生成错误:
module.js:559
server_1 | throw err;
server_1 | ^
server_1 |
server_1 | Error: Cannot find module 'compression'
server_1 | at Function.Module._resolveFilename (module.js:557:15)
server_1 | at Function.Module._load (module.js:484:25)
server_1 | at Module.require (module.js:606:17)
server_1 | at require (internal/module.js:11:18)
server_1 | at Object.<anonymous> (/app/src/server.js:12:19)
server_1 | at Module._compile (module.js:662:30)
server_1 | at Object.Module._extensions..js (module.js:673:10)
server_1 | at Module.load (module.js:575:32)
server_1 | at tryModuleLoad (module.js:515:12)
server_1 | at Function.Module._load (module.js:507:3)
server_1 | at Function.Module.runMain (module.js:703:10)
server_1 | at startup (bootstrap_node.js:193:16)
server_1 | at bootstrap_node.js:660:3
server_1 | npm ERR! code ELIFECYCLE
server_1 | npm ERR! errno 1
server_1 | npm ERR! james-auth@1.0.0 start: `node ./src/server.js`
server_1 | npm ERR! Exit status 1
server_1 | npm ERR!
server_1 | npm ERR! Failed at the james-auth@1.0.0 start script.
server_1 | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
server_1 |
server_1 | npm ERR! A complete log of this run can be found in:
server_1 | npm ERR! /root/.npm/_logs/2018-04-25T07_26_17_609Z-debug.log
的package.json:
{
"name": "james-auth",
"version": "1.0.0",
"main": "src/server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node ./src/server.js"
},
"repository": {
"type": "git"
},
"keywords": [
"express",
"jwt-authentication",
"jwt",
"mysql",
"angular",
"sequelize",
"authentication"
],
"author": "Nick C. Vasile",
"license": "MIT",
"dependencies": {
"angular": "^1.6.2",
"bcrypt": "^1.0.2",
"body-parser": "^1.17.1",
"compression": "^1.7.2",
"express": "^4.15.2",
"jsonwebtoken": "^7.3.0",
"morgan": "^1.8.1",
"mysql": "^2.13.0",
"nodemon": "^1.15.1",
"passport": "^0.3.2",
"passport-jwt": "^2.2.1",
"sequelize": "^3.32.1",
"sequelize-cli": "^4.0.0"
}
}