質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

Q&A

解決済

1回答

9484閲覧

Node.jsの実行エラー

11h45m

総合スコア12

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

0グッド

0クリップ

投稿2020/03/05 10:01

現状

OS:Macノート
エディタ:VSCode

Node.js超入門という書籍で学び始め、テンプレートを表示してみるという段階で以下のエラーが出ており解消ができず止まってしまいました。
このエラーを解消してテンプレートを使用した表示ができることを確認私することが目的です。

console

1●●●●●●●@●●●●●●● node-app % ls 2app.js index.ejs index.html 3●●●●●●●@●●●●●●● node-app % node -v 4v12.16.1 5●●●●●●●@●●●●●●● node-app % node app.js 6internal/modules/cjs/loader.js:985 7 throw err; 8 ^ 9 10Error: Cannot find module 'ejs' 11Require stack: 12 /Users/●●●●●●●/projects/node-app/app.js 13 at Function.Module._resolveFilename (internal/modules/cjs/loader.js:982:15) 14 at Function.Module._load (internal/modules/cjs/loader.js:864:27) 15 at Module.require (internal/modules/cjs/loader.js:1044:19) 16 at require (internal/modules/cjs/helpers.js:77:18) 17 at Object.<anonymous> (/Users/●●●●●●●/projects/node-app/app.js:3:13) 18 at Module._compile (internal/modules/cjs/loader.js:1158:30) 19 at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10) 20 at Module.load (internal/modules/cjs/loader.js:1002:32) 21 at Function.Module._load (internal/modules/cjs/loader.js:901:14) 22 at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) { 23 code: 'MODULE_NOT_FOUND', 24 requireStack: [ '/Users/●●●●●●●/projects/node-app/app.js' ] 25} 26●●●●●●●@●●●●●●● node-app %

確認したこと

Error: Cannot find module 'ejs'を調べた際、カレントディレクトリが合っていないといった解決が複数あったので確認しましたが、現在プロジェクトファイルはこれしか存在せず、テンプレート追加前には実行出来ていたため別の問題であはと考えています。

appjs

1const http = require('http'); 2const fs = require("fs") 3const ejs = require("ejs"); 4const index_page = fs.readFileSync('./index.ejs', 'utf8') 5var server = http.createServer(getFromClient); 6server.listen(3000); 7console.log("server start") 8 9function getFromClient(request,response){ 10 var content = ejs.render(index_page, { 11 title:"Index", 12 content:"テンプレートの表示です", 13 }); 14 response.writeHead(200, {'Content-Type': 'text/html'}); 15 response.write(content); 16 response.end(); 17}

indexejs

1<!DOCTYPE html> 2<html lang="ja"> 3 <head> 4 <meta http-equiv="content-type" 5 content="text/html; charset=UTF-8"> 6 <title><%=title %></title> 7 </head> 8 <body> 9 <head> 10 <h1><%=title %></h1> 11 </head> 12 <div role="main"> 13 <p><%=content %></p> 14 </div> 15 </body> 16</html>

また、EJSが見つからないとのことなので、書籍にも乗っていた対処法として以下のコマンドを実行した状態で上記のpエラーが発生しています。

conosole

1●●●●●●●@●●●●●●● ~ % cd Applications 2●●●●●●●@●●●●●●● Applications % npm init 3This utility will walk you through creating a package.json file. 4It only covers the most common items, and tries to guess sensible defaults. 5 6See `npm help json` for definitive documentation on these fields 7and exactly what they do. 8 9Use `npm install <pkg>` afterwards to install a package and 10save it as a dependency in the package.json file. 11 12Press ^C at any time to quit. 13package name: (applications) 14version: (1.0.0) 15description: 16entry point: (index.js) 17test command: 18git repository: 19keywords: 20author: 21license: (ISC) 22About to write to /Users/●●●●●●●/Applications/package.json: 23 24{ 25 "name": "applications", 26 "version": "1.0.0", 27 "description": "", 28 "main": "index.js", 29 "scripts": { 30 "test": "echo \"Error: no test specified\" && exit 1" 31 }, 32 "author": "", 33 "license": "ISC" 34} 35 36Is this OK? (yes) 37●●●●●●●@●●●●●●● Applications % npm install --save ejs 38 39> ejs@3.0.1 postinstall /Users/●●●●●●●/Applications/node_modules/ejs 40> node ./postinstall.js 41 42Thank you for installing EJS: built with the Jake JavaScript build tool (https://jakejs.com/) 43 44npm notice created a lockfile as package-lock.json. You should commit this file. 45npm WARN applications@1.0.0 No description 46npm WARN applications@1.0.0 No repository field. 47 48+ ejs@3.0.1 49added 1 package from 1 contributor and audited 1 package in 0.929s 50found 0 vulnerabilities 51 ╭────────────────────────────────────────────────────────────────╮ 52 │ │ 53 │ New minor version of npm available! 6.13.4 → 6.14.2 │ 54 │ Changelog: https://github.com/npm/cli/releases/tag/v6.14.2 │ 55 │ Run npm install -g npm to update! │ 56 │ │ 57 ╰────────────────────────────────────────────────────────────────╯

他に何か必要な情報がありましたら教えていただけますと幸いです。
よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

●●●●●●●@●●●●●●● ~ % cd Applications ●●●●●●●@●●●●●●● Applications % npm init This utility will walk you through creating a package.json file. It only covers the most common items, and tries to guess sensible defaults ●●●●●●●@●●●●●●● Applications % npm install --save ejs
●●●●●●●@●●●●●●● node-app % ls app.js index.ejs index.html ●●●●●●●@●●●●●●● node-app % node -v v12.16.1 ●●●●●●●@●●●●●●● node-app % node app.js internal/modules/cjs/loader.js:985 throw err; ^ Error: Cannot find module 'ejs'

Applicationsフォルダにejsをインストールしているのですから、
node-appにフォルダにejsがないのは当然でしょう、

投稿2020/03/05 10:25

technocore

総合スコア7200

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

11h45m

2020/03/05 10:39

「アプリケーション」と言う説明を「Applicationsフォルダ」と勘違いをしていたことを理解しました。 アホな質問に迅速な回答ありがとうございます。 node-appに直接インストールして解決しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問