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

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?

Related

Require module inside a WebWorker in React

i am trying to import an npm module inside my React Web Worker using const package = require('package-name').
But it gives me the following error :
Uncaught (in promise) ReferenceError: require is not defined
How can i use a module inside a worker, is there a native way or some way to bypass it?
I am using NodeJS v18.10.0, Webpack v5.73.0.
I think your question is related to
Client on Node.js: Uncaught ReferenceError: require is not defined
please look at it , you might solved your problem

How to import functions from "back-end" js file to "front-end" js file

I have a Pinata IPFS js file with a "PinFileToIPFS" function inside; I'm wanting to call this function from another js file. The problem is that the Pinata js file uses "require()" to import its libraries, but my other "front-end" js file doesn't support node.js functions, and thus throws an error when loaded in the browser.
I need a way to import the PinFileToIPFS function into my other js file so it can be called upon completion of a certain front-end event. As stated before, I can't use require() to bring in the function, and if I try to use "import nameOfFunction from module;" to import the function I get the error:
Uncaught SyntaxError: Cannot use import statement outside a module.
I've seen similar questions asked, but none pertaining to my specific problem. I'm also new to programming, so any and all feedback is appreciated.

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.

Node.js - require is not defined

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.

JavaScript error when importing a package

getting the following error when importing netscape.javascript
importClass(netscape.javascript)
org.mozilla.javascript.EcmaError: ReferenceError: "netscape" is not defined.
Trying to use javascript within applet to get access to cookies.
Using the following as an example to import required package
http://www.java2s.com/Code/Java/JDK-6/UsingJavaObjectsinJavaScript.htm
http://docs.oracle.com/javase/tutorial/deployment/applet/invokingJavaScriptFromApplet.html
Found out by trial and error.
It seems we need to prefix with Packages
importPackage(Packages.netscape.javascript);

Categories

Resources