2017-07-27 56 views
4

這裏是我的index.js文件:錯誤:無法找到模塊 '哈巴狗'

const express = require('express') 
const app = express() 

app.set('views', __dirname + '/views'); 
app.set('view engine', 'pug') 

app.get('/', function (req, res) { 
    res.render('index', { title: 'Hey', message: 'Hello there!' }) 
}) 


app.listen(3333, function() { 
    console.log('Example app listening on port 3333!') 
}) 

index.pug文件:

html 
    head 
    title= title 
    body 
    h1= Hello 

的package.json文件:

{ 
    "name": "@npm-private/pug_with_node", 
    "version": "1.0.0", 
    "description": "", 
    "main": "index.js", 
    "scripts": { 
    "test": "echo \"Error: no test specified\" && exit 1" 
    }, 
    "keywords": [], 
    "author": "", 
    "license": "ISC", 
    "dependencies": { 
    "express": "^4.15.3", 
    "jade": "^1.11.0", 
    "pug": "^2.0.0-rc.2" 
    } 
} 

當我運行我的服務器文件時,它顯示我一個錯誤。其實,我安裝的哈巴狗和玉都NPM模塊:

Error: Cannot find module 'pug' at Function.Module._resolveFilename (module.js:485:15) at Function.Module._load (module.js:437:25) at Module.require (module.js:513:17) at require (internal/module.js:11:18) at new View (/home/software/node_modules/express/lib/view.js:80:30) at Function.render (/home/software/node_modules/express/lib/application.js:570:12) at ServerResponse.render (/home/software/node_modules/express/lib/response.js:971:7) at /home/software/Harsh Patel/pug_with_node/index.js:8:7 at Layer.handle [as handle_request] (/home/software/node_modules/express/lib/router/layer.js:95:5) at next (/home/software/node_modules/express/lib/router/route.js:137:13)

+1

你用什麼命令來安裝這兩個模塊? –

+0

package.json在哪裏?你有沒有做npm install –

+0

確保你有'pug'裏面的遊覽package.json – Ved

回答

0

當有整體與局部之間的模塊安裝不匹配,你會遇到,即使你已經安裝了所有的模塊這個問題。我建議你通過包括依賴於的package.json

npm install --save express jade pug 
+1

但我在本地和全球使用相同的版本。 –

+0

@HarshPatel我建議你從package.json中刪除「依賴」,並使用命令'npm install --save express jade pug'重新安裝。 –

+1

爲什麼要安裝'jade'和'pug'? 「玉」不只是「帕格」的老版本嗎? – aboveyou00

5

嘗試添加此行

app.engine('pug', require('pug').__express) 

app.set('views', path.join(__dirname, 'views')); 
app.set('view engine', 'pug'); 

這解決了安裝所有本地項目同樣的問題對我來說!