在opentok示例中找不到模块“ejs”

我正在遵循这个指南: https : //github.com/opentok/opentok-node/tree/master/sample/HelloWorld

该项目编译好,但是当我开始,我得到这个错误:

错误:

> Error: Cannot find module 'ejs' > at Function.Module._resolveFilename (module.js:470:15) > at Function.Module._load (module.js:418:25) > at Module.require (module.js:498:17) > at require (internal/module.js:20:19) > at new View (C:\pruebaTokbox\node_modules\express\lib\view.js:80:30) > at Function.render (C:\pruebaTokbox\node_modules\express\lib\application.js:570:12) > at ServerResponse.render (C:\pruebaTokbox\node_modules\express\lib\response.js:971:7) > at C:\pruebaTokbox\index.js:33:7 > at Layer.handle [as handle_request] (C:\pruebaTokbox\node_modules\express\lib\router\layer.js:95:5) > at next (C:\pruebaTokbox\node_modules\express\lib\router\route.js:137:13) 

我的代码:package.json

 { "name": "opentok-helloworld-sample", "version": "0.0.0", "description": "Group video chat app to demonstrate the basic functionality of OpenTok", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "ejs": "^0.8.6", "express": "^3.5.0", "opentok": "^2.5.0" } } 

index.js

 // Dependencies var express = require('express'), OpenTok = require('opentok'); // Verify that the API Key and API Secret are defined var apiKey = 111111, apiSecret = '1111111'; if (!apiKey || !apiSecret) { console.log('You must specify API_KEY and API_SECRET environment variables'); process.exit(1); } // Initialize the express app var app = express(); app.use(express.static(__dirname + '/public')); // Initialize OpenTok var opentok = new OpenTok(apiKey, apiSecret); // Create a session and store it in the express app opentok.createSession(function(err, session) { if (err) throw err; app.set('sessionId', session.sessionId); // We will wait on starting the app until this is done init(); }); app.get('/', function(req, res) { var sessionId = app.get('sessionId'), // generate a fresh token for this client token = opentok.generateToken(sessionId); res.render('index.ejs', { apiKey: apiKey, sessionId: sessionId, token: token }); }); // Start the express app function init() { app.listen(3000, function() { console.log('You\'re app is now ready at http://localhost:3000/'); }); } 

和index.ejs

 <!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <title>OpenTok Hello World</title> <script src="//static.opentok.com/webrtc/v2.2/js/TB.min.js"></script> <script type="text/javascript"> var apiKey = '<%= apiKey %>'; var sessionId = '<%= sessionId %>'; var token = '<%= token %>'; </script> <script src="/public/js/helloworld.js"></script> </head> <body> <h2>Hello, World!</h2> <div id="publisher"></div> <div id="subscribers"></div> </body> </html> 

原来的文件,没有公共path,只是JS,但是这个path是错误的。

在这里input图像说明

我想你需要在你的index.js中使用view engine

将此添加到您的文件中:

 app.set('view engine', 'ejs'); 

这应该解决它。

Ps:作为一个完整的检查,确保ejs实际上是通过运行npm i ejs安装的

解决scheme是执行这2个命令

  • npm安装ejs – 保存
  • npm install express – 保存