Node.js 和 requirejs 未定义

Node.js and requirejs undefined

我真的试图在Whosebug和google

上找到解决方案

但我找不到任何明确的解决方案。

我是一名学生,我尝试用

制作一个网络应用程序

js,node.js,bootstrap,firebase,css

实际上当我尝试使用 var firebase = require('firebase');

我总是收到消息 "require is undefined"

这些是我的 package.json

{
  "name": "recette",
  "version": "1.0.0",
  "description": "Recette BCIT GROUP 22 Project",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/lewisfang59/recette.git"
  },
  "author": "Group22",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/lewisfang59/recette/issues"
  },
  "homepage": "https://github.com/lewisfang59/recette#readme",
  "dependencies": {
    "@sendgrid/mail": "^6.2.1",
    "nodemailer": "^4.6.4"
  }
}

这是我想要使用的代码 require

// using SendGrid's v3 Node.js Library
// https://github.com/sendgrid/sendgrid-nodejs
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'xx@gmail.com',
    pass: 'xxx'
  }
});

var mailOptions = {
  from: 'recette.group22@gmail.com',
  to: 'hwan5263@gmail.com',
  subject: 'Sending Email using Node.js',
  text: 'That was easy!'
};

transporter.sendMail(mailOptions, function(error, info){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);
  }
});

这是 firebase 的代码,同样的问题。

var firebase = require('firebase');
// Initialize Firebase
var config = {
    apiKey: "0",
    authDomain: "recmoved",
    databaseURL: "removed!",
    projectId: "foodver",
    storageBucket: "foo",
    messagingSenderId: "22300"
};
firebase.initializeApp(config);

var db = firebase.firestore();

我对这个领域很陌生。有什么需要补充的请告诉我!

非常感谢!!

require 在浏览器中不可用。

正如其他人提到的,考虑引入像Webpack这样的构建系统。 RequireJS 也可能是您的选择。