Importing variable from another file into route file of node.js - javascript

I'm attempting to pass a prompt box variable into my route file of a node.js application, but I'm having issues having the variable be recognized. The main prompt variable "test" is in a javascript file called mainclient.js, and I'm attempting to get it into my index.js file. I've attempted to export it as so:
In mainclient.js I have:
exports.Test = test;
and in my index.js, I've included:
var mainclient = require('../public/javascripts/mainclient');
and then call it by using:
var prompt = mainclient.test;
When I attempt to start the node, I'm getting an error stating:
ReferenceError: $ is not defined
Due to the first line in my mainclient.js file being:
$(document).ready(function(){
Anyone know a way to get this error corrected or another way that I can get the variable from mainclient.js into index.js? Any help would be greatly appreciated. Thanks!
Edit:
I should add, I've attempted to import jquery in both files by including the proper header link,however, I then get an error stating:
"SyntaxError: Unexpected token <" when attempting to start the node. jquery is imported in the main.js file for the node though.

Related

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

Post TypeScript Compilation: Uncaught SyntaxError: Unexpected token {

Scenario
Whenever I run my application from index.html, referencing the main.js file, which has been compiled from Typescript using es6 as the target and ESNext as the module, I get this error within the browser:
Error
main.js:3 Uncaught SyntaxError: Unexpected token {
Main.ts
/// IMPORTS
//////////////////////////////////////////////////////////////////////
import { Player } from './classes/Player' <= Error Here!
import { printTable } from './helpers/Helpers'
/// MAIN COMMANDS
//////////////////////////////////////////////////////////////////////
let p1 = new Player('Bob', 'Hunter', 1, 10);
printTable(p1);
Concept
I appear to be importing my code improperly, and (for whatever reason) don't know why or can't figure it out. Everywhere I look, this is indicated as the method of importing exported code, classes, etc... When all code exists within a single file, all classes, interfaces, helpers, etc..., everything converts and runs dandy, except when I want to compartmentalize my code, and handle it this way.
Any guidance would surely be appreciate.
Cheers and thanks in advance!
Source Code
...and ESNext as the module, I get this error within the browser:
main.js:3 Uncaught SyntaxError: Unexpected token {
Your error here is that you are trying to use ES6 import syntax in a traditional script (<script src="index.js"></script>). Using ES6 import and export only works with modules, used in the browser with <script type="module" src="whatever"></script>.
Have you looked into using Webpack? It can automatically compile your TypeScript files into one JavaScript bundle that you can load into your HTML file with a single script tag.
For you to import in this way you are using, you must export the class reference directly, without being inside an object.
Examples:
(correct) export Player;
(wrong) export { Player }

Import dynamic file with require React

Hello there I'm importing a dynamic file with require, but my file is outside of /src so I check a post that say if I comment two lines in ./node_modules/react-scripts/config/webpack-config.js
I can access to files outside of /src, the problem is that if I do this works fine:
Show PDF
but if i want to be dynamic receiving the path through props i get this error:
Error: Cannot find module '../../../../../files/asd.pdf'
Where my code is this and this.state.fullPath is the same of before.
Show PDF
I try concatenating a empty string but still not working
This is a problem that relates to the module bundler that you are using. In order for it to work, the require function has to get a path which the bundler will use to import the module at build time rather than runtime. You will need to use require using the full path and not a variable.
You can try something like:
const one = require('./path/to/one.pdf');
const two = require('./path/to/two.pdf');
const href = variable === 1 ? one : two;
Show PDF

Require is not working for node-opcua

I want to load a local version of node-opcua with 'require' inside a HTML file, but it does not really work. The code snippet is the following:
<script type="text/javascript" src="path_to_require.js"></script>
<script>
var opcua = require(["path_to_node-opcua"]); <!-- Yes, the path is correct >
var client = new opcua.OPCUAClient();
...
When I execute the script I get the following error in the console:
Uncaught TypeError: opcua.OPCUAClient is not a constructor
Hence, var opcua is loaded correctly, but OPCUACluent is not, although the class is declared in a file that is present in the node-opcua folder called opcua_client.js under node-opcua\lib\client\
Sources:
The 'require' script from http://requirejs.org/docs/download.html#requirejs.
The node-opcua folder with the console command
npm install node-opcua.
node-opcua is not intended to run inside a browser as it relies on nodejs specific features such as filesystem access, crypto and so on.
You need to use browserify if you want to use that module in client. You will also need to look at how to use browserify with file system access (it can be done if paths are known ahead of time).

Including Javascript Library in Node Express Routes

I'm trying to use the Sanford encryption library(sjcl) in my Express App.
I've tried to the following in my app.js file:
var sjcl = require('.lib/sjcl.js');
Next I try to call sjcl.encrypt in my routes/journal.js file, but get an error that it's not defined.
Next I tried requiring the the library in my journal.js file at the beginning, but get the module ./lib/sjcl.js cannot be found.
The sjcl.js library does export the sjcl object so that doesn't seem to be it.
Any ideas on how I can gain access to the sjcl library from within my routers file?
Next I try to call sjcl.encrypt in my routes/journal.js file, but get an error that it's not defined.
require() just returns an object representing that module.
var x = require(...) assigns that object to a local variable.
It doesn't affect other .js files.
Next I tried requiring the the library in my journal.js file at the beginning, but get the module ./lib/sjcl.js cannot be found.
That would happen if your relative path is wrong.

Categories

Resources