前提
UdemyでNode.jsを使用したメールアドレス登録のアプリケーションを作っています。mailchimp (https://mailchimp.com/) をurl先として設定しています。
Udemyの中ではherokuを使用していましたが、今は優良なのでRender.comを使いホストしようとしています。この動画の5:50頃までは順調に行っていました。
https://www.youtube.com/watch?v=bnCOyGaSe84&ab_channel=CodeBrah
実現したいこと
Render.comに登録する
発生している問題・エラーメッセージ
==> Starting service with 'node app.js' internal/modules/cjs/loader.js:888 throw err; ^ Error: Cannot find module 'node:https' Require stack: - /opt/render/project/src/app.js at Function.Module._resolveFilename (internal/modules/cjs/loader.js:885:15) at Function.Module._load (internal/modules/cjs/loader.js:730:27) at Module.require (internal/modules/cjs/loader.js:957:19) at require (internal/modules/cjs/helpers.js:88:18) at Object.<anonymous> (/opt/render/project/src/app.js:3:15) at Module._compile (internal/modules/cjs/loader.js:1068:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10) at Module.load (internal/modules/cjs/loader.js:933:32) at Function.Module._load (internal/modules/cjs/loader.js:774:14)
該当のソースコード
Hyper(terminal)
1rm -rf node_modules / package-lock.json 2git init 3git branch -M main 4git add . 5git commit -m "First Commit" 6git remote add origin https://github.com/yoriss67/Newsletter-Signup.git 7git push -u origin main 8node app.js
app.js
1const express = require('express'); 2const mailchimp = require("@mailchimp/mailchimp_marketing"); 3const https = require('node:https'); 4const { url } = require('node:inspector'); 5const app = express(); 6 7 8app.use(express.static('public')) 9 10// old 「body-parser」 11app.use(express.urlencoded({ 12 extended: true 13})); 14 15 16app.get('/', function (req, res) { 17 res.sendFile(__dirname + '/signup.html') 18}) 19 20// ------------- 21mailchimp.setConfig({ 22 apiKey: "eeb85b047aa8497f6038c75a82978ab2-us◯◯", 23 server: "us◯◯" 24}); 25// -------------- 26 27 28app.post('/', function (req, res) { 29 const firstName = req.body.fName; 30 const lastName = req.body.lName; 31 const email = req.body.email; 32 33 console.log(firstName, lastName, email) 34 35 36 //*****************************ENTER YOU LIST ID HERE****************************** 37 const listId = "◯◯"; 38 //Creating an object with the users data 39const subscribingUser = { 40 firstName: firstName, 41 lastName: lastName, 42 email: email 43 }; 44 45 46 // *** Construct Requesting data *** 47 // members's key-value pair 48 const data = { 49 members: [{ 50 email_address: email, 51 status: 'subscribed', 52 merge_fields: { 53 FNAME: firstName, 54 LNAME: lastName 55 } 56 }] 57 } 58 59 // 👩🎓Now we have our data object completed, but this is JavaScript and what we need is actually to turn this into a flatpack JSON. 60const jsonData = JSON.stringify(data); 61 62// 👩🎓make our request 63// const url = "https://us{list server number}.api.mailchimp.com/3.0/lists/{List ID}" 64const url = "https://us◯◯.api.mailchimp.com/3.0/lists/◯◯" 65 66 67const options = { 68 method: 'POST', 69 // how we use API key for auth? > A. HTTP Basic Authentication (a pair of ①string as username ②API key) 70 auth: 'yoriss67:eeb85b047aa8497f6038c75a82978ab2-us◯◯' 71} 72 73// 🤔constにしないとその場で終わっちゃってdataをserverに持っていって活用できなくなるから? 74const request = https.request(url, options, function(response) { 75 76if(response.statusCode === 200) { 77 // res.send('Successfully subscribed!!!!!!') 78 res.sendFile(__dirname + '/success.html') 79 80} else { 81 // res.send('There was an error') 82 res.sendFile(__dirname + '/failure.html') 83 84 85} 86 87 response.on('data', function(data) { 88 console.log(JSON.parse(data)) 89 }) 90}) 91 92request.write(jsonData); 93request.end(); 94 95}) 96 97// 250 98app.post('/again', function(req, res) { 99 console.log('Again') 100 res.sendFile(__dirname + '/signup.html') 101 102}) 103 104app.listen(2480, function () { 105 console.log('server is running on port 2480') 106});
package.json
1{ 2 "name": "newsletter-signup", 3 "version": "1.0.0", 4 "description": "Newsletter-Signup project!", 5 "main": "app.js", 6 "scripts": { 7 "start": "node app.js", 8 "test": "echo \"Error: no test specified\" && exit 1", 9 "build": "npm install; nodemon app.js" 10 }, 11 "author": "yoriss67", 12 "license": "ISC", 13 "dependencies": { 14 "@mailchimp/mailchimp_marketing": "^3.0.80", 15 "async": "^3.2.4", 16 "express": "^4.18.2", 17 "https": "^1.0.0", 18 "request": "^2.88.2", 19 "yarn": "^1.22.19" 20 } 21} 22
この
package.json
1 "build": "npm install; nodemon app.js"
とあるのはRender.comのコミュニティで似ている質問の回答にこう書けと書いてあったからです。
https://community.render.com/t/error-cannot-find-module/6781/6
Render.com内
試したこと
実は最初は「cannot find module "express"」と表示されていました。
そこで「Node express render」で調べたら、このサイトが最初に出てきました。https://render.com/docs/deploy-node-express-app。
Build Commandを "yarn "にしないといけないらしいので、"npm i yarn "と打ち込んで手動でデプロイ。
その後、上記のエラーが起きました。
モジュールを1つずつインストールしなければならないのでしょうか。。。

あなたの回答
tips
プレビュー