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

Posted

技术标签:

【中文标题】在 opentok 示例上找不到模块“ejs”【英文标题】:Cannot find module 'ejs' on opentok sample 【发布时间】:2017-10-18 18:21:20 【问题描述】:

我正在关注本指南: 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)

我的代码: 包.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>

原始文件,没有公共路径,只有JS,但是那个路径不对。

enter image description here

【问题讨论】:

【参考方案1】:

解决方案是执行这两个命令

npm install ejs --save npm install express --save

【讨论】:

请注意,由于您有一个 package.json 文件,因此您不需要执行两个 npm 命令。您可以使用“npm i”或“npm install”安装所有依赖项 -> 此命令检查 package.json 文件中的依赖项并安装“依赖项”属性下的所有内容。【参考方案2】:

我认为您需要在 index.js 中使用 view engine

将此添加到您的文件中:

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

这应该可以解决它。

Ps:作为健全性检查,通过运行npm i ejs确保实际安装了ejs

【讨论】:

以上是关于在 opentok 示例上找不到模块“ejs”的主要内容,如果未能解决你的问题,请参考以下文章

错误:找不到模块“ejs”

Webpack + Express + EJS:错误:找不到模块“。”

错误:找不到模块“ejs”,即使它已在 package.json 中列出

错误:在 require.resolve('jquery') webpack 上找不到模块 'jquery'

在 Ubuntu 上找不到 Tkinter 模块

错误:在 Heroku 上找不到模块 Discord.js