在 opentok 示例中找不到模块 'ejs'
Cannot find module 'ejs' on opentok sample
我正在遵循本指南:
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>
原始文件,没有public路径,只有JS,但那个路径是错误的。
enter image description here
我认为您需要在 index.js:
中使用 view engine
将此添加到您的文件中:
app.set('view engine', 'ejs');
这应该可以解决问题。
Ps:作为健全性检查,确保 运行 npm i ejs
确实安装了 ejs
解决方案是执行这 2 个命令
- npm install ejs --save
- npm install express --save
我正在遵循本指南: 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>
原始文件,没有public路径,只有JS,但那个路径是错误的。
enter image description here
我认为您需要在 index.js:
中使用view engine
将此添加到您的文件中:
app.set('view engine', 'ejs');
这应该可以解决问题。
Ps:作为健全性检查,确保 运行 npm i ejs
解决方案是执行这 2 个命令
- npm install ejs --save
- npm install express --save