Node.js - require is not defined - javascript

I'm trying to import the mysql driver in node.js but I keep getting this error:
Uncaught ReferenceError: require is not defined
This is the line that is causing the issue:
//database.js
const mysql = require("mysql");
The file that is causing the issue is called database.js which I am trying to import into script.js using
//script.js
import {myFunction} from "../database.js";
This is how I am importing script.js in HTML:
<script type="module" src="script.js"></script>
I'm using Node.js 12.18.0

You cant use CommonJs (require) in the browser, it is for Node.js.
You cant, or should not be able to use mysql stuff on the client side, this would be a big security issue.

Related

I'm getting errors trying to require/import with dotenv

I'm trying to utilise an API token that's currently residing in my .env file.
I've tried
require('dotenv').config()
console.log(process.env)
and am returned with this error
Uncaught ReferenceError: require is not defined
so I tried.
import * as dotenv from 'dotenv'
dotenv.config()
and get this error in return
Uncaught ReferenceError: require is not defined
I am using react and have used the require method in another js file on the same project and it works perfectly fine, but trying to use them in this jsx file doesn't seem to work I can't understand why,
Thank you.
ReactJs is a browser/client-side JavaScript thus,
require() does not exist in the browser/client-side JavaScript. read here
To answer your question on implementing dotenv in client-side.
You can read more about here: Is it possible to use dotenv in a react project?

Uncaught SyntaxError: Cannot use import statement outside a module - Cordova

I am simply trying to access my common application variable/function from main.js file. using ES6- Import Export.
export var APP_NAME= "MyPOC"; - File main.js
Importing now in other javascript file- home.js,
import * as mainparams from './main.js';
console.log("variable_name:"+ mainparams.APP_NAME);
OR
import {APP_NAME} from './main.js';
console.log("variable_name:"+ APP_NAME);
Error: Uncaught SyntaxError: Cannot use import statement outside a module
The same thing is working while I am importing in my HTML page - index.html
<script type="module">
import {APP_NAME} from './js/main.js';
console.log("variable_name: "+ APP_NAME);
</script>
Result - Print in console - variable_name: MyPOC
What I did to fix the issue:
I checked the js file path, both js are in the same folder
Cross-check the cordova-plugin-whitelist package installed already
I tried to add "type" : "module" in package.json - Show me issue on terminal can't support ES6
Added <script type="module" src="main.js"></script> - Result: "unexpected token <"
None of the above action items working for me. I just want to normalize my code in different Javascript files for better code readability.
Using: HTML, CSS. JS, JQuery, Cordova, Phonegap Platform: Android, Browser
ES6- Java Script Import-Export
Cordova WhileList Plugin
Add module in package.json file or use module directly
Please help me out and thanks for your time to understand my issue.

import module without nodejs - vanillaJS

I'm refreshing my old and low knowledge on VanillaJS using the latest best practices i found.
I recently did a tutorial on NodeJS doing API REST with ExpressJs and one with Socket IO.
Now I want to practice a little before going to REACTJS.
So I started a little project
I do one without NodeJs - just JS into HTML view - using Objects.
I want to use modules from Npm. I want to try Fakerjs but when i want to import it i have a 403.
The path is correct, no error.
So i'm wondering if it's possible without Nodejs to import modules when doing VanillaJs?
Am i doing it wrong ?
The structure of the project is :
js/
main.js
class/
node_modules/
index.html
Main.js:
'use strict'
//Importation of modules
import faker from '../node_modules/faker'
//Importation of Class Folder
import { Crypto } from "./class/crypto.class.js";
console.log(faker);
faker.locale = 'en_US';
I have this error in console:
GET http://crypto-market-js.local:8282/node_modules/faker/ net::ERR_ABORTED 403 (Forbidden)
If i write : import faker from 'faker' (like with node js but it's a require instead) i have this : Uncaught TypeError: Failed to resolve module specifier "faker". Relative references must start with either "/", "./", or "../".
If you could give me a hand on that plz :)
Trying to import with require is not supported without NodeJS, because require is a node-specific function. When trying to import modules into Vanilla JS, my recommendation is to first link to ths script with the <script> html tag, and then add another script with import faker from 'faker'
Hope that clarifies your issue.

Trying to use pouchDB, saying that it's not defined when i think it is

i'm trying to add pouchdb to my pwa to store some things when offline so i can post them later. I first register my Service Worker from app.js, and from it i ImportScript two other files with the logic of the sw. One of those files is where i put the logic for the db, sw_db.js. I installed pouchDB with npm and added the script tag for it in index.html but i when i declare:
const db = new PouchDB('acciones');
i get
sw_db.js:4 Uncaught ReferenceError: PouchDB is not defined
at sw_db.js:4
at sw.js:3
Basically, sw_db.js doesnt know about PouchDB even though the methods from pouchdb get autocompleted. i've tried adding the cdn or downloading the file but still doesnt work. Also the script tag for pouchdb is before app.js in index.html (which registers the sw).
I am not sure if you are using PouchDB in a browser or a NodeJS context. If it is in a browser, then the PouchDB module should be defined in the HTML file like this:
<script src="js/pouchdb.min.js"></script>
assuming you have the PouchDB module on your web server. If not you can use it from a CDN like this:
<script src="//cdn.jsdelivr.net/npm/pouchdb#7.1.1/dist/pouchdb.min.js"></script>
and your Javascript code should work:
const db = new PouchDB('my-db');
But if it is in a NodeJS script then you need to install PouchDB with npm as you mention and declare it in the code like this:
const PouchDB = require('pouchdb');
and then define a database the same as before:
const db = new PouchDB('my-db');
I hope this helps you resolve the error.

JavaScript/Node js import and export module(Using exported node Js module in JavaScript file)

I am trying to export node js modules to use in JavaScript. This is what I have done.
-Installed esm in node.
in my node file I did like this:
require = require("esm")(module/*, options*/);
export function Myjs(name, surname){console.log(name, surname)};
This does not give error.
In my js file I did:
1- import { Myjs } from '/javascripts/sockets/bases.js'; This gives error as "Uncaught SyntaxError: Cannot use import statement outside a module". I have been reading topics regarding this error, and it suggests that I should include "type: module" while including the file. But I do not include node js files as scripts on html.??
2- I try to import dynamically like this:
import('/javascripts/sockets/bases.js')
.then((module) => {
module.Myjs("bola", "carro");
});
This gives error if detects require() in the file and it gives error "require is not defined" or any node js module that js does not recognize.??
What I'm trying to achieve is: Based on an event on js file trigger the function (Myjs()) I'm trying to import. How could achieve this? I tried babel and I wasn't successful. Thanks

Categories

Resources