Currently experiencing something that is absolutely baffling. I am requiring the 'qrcode' module which I installed via npm (npm --save install qrcode).
const {
QRCode
} = require('qrcode');
The module is listed in my package.json and does exist in node_modules. However, this is absolutely always 'undefined'. When hovering over the string in the require statement I see that it is some how referencing a node_modules directory in my local/AppData directory which I'm assuming is the global directory for modules. Using this path always results in 'undefined'. I then tried switching the path to reference the local node_modules qrcode and still 'undefined'. I've read everything here https://www.npmjs.com/package/qrcode but doesn't help with the issue I'm seeing and there appears to be not a single conversation on google about this.
TypeError: Cannot read property 'toDataURL' of undefined
When using the 'qrcode' string it is using something in the #types folder which I thought was for ES6 which I am not using. No idea how to proceed nothing adds up here, any help greatly appreciated!
Node.js example from npm package page(if only it were this easy)
var QRCode = require('qrcode')
QRCode.toDataURL('I am a pony!', function (err, url) {
console.log(url)
})
When you do this:
const {
QRCode
} = require('qrcode');
You are expecting a QRCode property on the object that the module exports. This is called object destructing assignment in ES6. It is shorthand for and equivalent to this:
const QRCode = require('qrcode').QRCode;
And, since the object the module exports does NOT have a .QRCode property, you get nothing but undefined.
Instead, the top level object it exports is the QRCode object so you need to do this:
const QRCode = require('qrcode');
If you wanted a specific property that it was exporting, you could then do something like this:
const { toDataURL } = require('qrcode');
Related
I'm trying to access the decode() method in the jsQR module.
I found an example that called decode() directly but that was from an HTML file, not nodejs.
In visual code I see this...
I know that the default export is defined in index.d.ts but is there anyway of importing the other classes/functions on the rest of the dist folder?
I've tried to import using require("jsqr/decoder") and require("jsqr/decoder/decode") to no avail.
EDIT
To be clear, I don't want jsQR, the default export. That deals with images. I'm trying to explicitly call the decode() method in the pic which accepts a BitMatrix
There are no limitations in what you can require from node_modules. So, with your case it should look like:
TypeScript\ESM Modules:
import { decode } from 'jsqr/dist/decoder/decoder';
CommonJS:
const { decode } = require('jsqr/dist/decoder/decoder');
UPD: If you take a look into dist folder for jsqr package, you can find that there are only d.ts files. Which means that you can not import it from there.
But, you can find an actual export of the module here:
Which means that you should able to import it from jsqr:
const { decode } = require('jsqr');
I know this problem has been submitted endless times but even after browsing through X questions and possible solutions this error still remains.
I have a file called tournament.js in the directory modules. tournament.js requires model.js which resides in the exact same directory. The structure is the following:
app/
modules/
model.js
tournament.js
tournament.js looks like:
const Model = require('./model');
class Tournament extends Model {
constructor() {
super();
}
static create() {
}
}
Even WebStorm says the path in require() is correct but my console still says: Uncaught Error: Cannot find module './model'
What am I missing?
Edit: I'm using Node v6.4.0 and Electron v1.4.15
Okay #adelphus gave me the correct hint. I outputted the path with console.log(__dirname) and it was not what I expected: Yes, I have been in the wrong path, because I thought the relative path require() takes is the one relative to the file I am currently working in (which was the tournament.js) and this is not the case.
Instead require() takes the path relative from the app root which in this case was ./app/models/model.js
I have a node module named redux-loop that I'm using, and I would like to modify one of its functions slightly.
It's not a very complicated file, so I've made a copy of it in my react-native app and made my changes. The original code requires a few exports from files inside the module, eg:
var { loop, isLoop, getEffect, getModel } = require('./loop');
var { batch, none } = require('./effects');
The problem is that my copy of this file cannot seem to get direct access to those files, so I can't import those symbols.
I've tried various combinations of things, such as:
var { loop, isLoop, getEffect, getModel } = require('redux-loop/loop');
var { batch, none } = require('redux-loop/effects');
…which conceptually would mean to me to require the loop.js file inside the redux-loop module, but apparently module loading doesn't work that way.
What's the best way for me to import theses symbols into this file?
If I understand your question and you're open to reapplying your changes to the source of the package, you could use npm edit <pkg> and modify the package directly. When your changes are done the package will be rebuilt with your modifications.
Let's say I have a node.js module
module.exports = function () {
console.log(__filename);
}
And in the main file I call it like
var x = require('path/to/module');
x();
That gives me the path to the module file. For example if I stored the module at ~/project-root/lib/mod.js and the main.js file lies at ~/project-root/main.js, that setup gives me the output:
~/project-root/lib/mod.js
I want to use something in place of __filename in the module that gives me to location of of the file from which it was called from. (e.g. the output would be ~/project-root/main.js instead, in this example).
The module can be located anywhere. So using path to adjust for only this example would fail in other scenarios (for example if module is stored in ~/project-root/node_modules/ or the global node.js modules directory.
I have a feeling it's something fairly trivial and I'm overlooking something. But I haven't found a solution from anything Google has yielded during an hour long search. Maybe I'm using the wrong keywords!
Module.parent called in your module lets you access to the exports of the file it was called from.
You can put the function below in your main.js and you can call module.parent.filename() in your module.
module.exports.filename = function () {
return __filename;
}
I ran into an issue with my Nodejs application.
I have two different apps that are using shared library, which is located so that it is found one level up in node_modules. So i have this structure ./app1/app.js, ./app2/app.js and ./node_modules/shared.libs/index.js.
shared.libs in its turn has some other modules installed, like mongoose, redis etc. Plus some mogoose models with additional functions in them. All are exported from index.js like this:
exports.async = require('async');
exports.config = require('./config');
exports.utils = require('./lib/utils');
And then in apps i import them like this:
var libs = require('shared.libs');
var config = libs.config;
So after this code i can use config which is coming from that shared library.
This part was and is working just fine. But now i need to put additional layer on top of this library (read: provide more unified interface for both apps).
What i tried to do is to add some functions into index.js of shared library and then export the whole object with these functions. But whenever i try to call previously imported (by var libs = require('shared.libs');) object it says that libs is not defined.
What am i doing wrong here?
I generally want to keep other code the same, so i won't need to go over replacing require part everywhere, but at the same time provide additional functionality from shared library which will be available from that imported libs object.
this should definitely work:
module.exports = {
async: require('async'),
config: require('./config'),
utils: require('./lib/utils'),
foo: function () {
return 'bar';
}
};
reference like:
var libs = require('shared.libs');
console.log(libs.async);
console.log(libs.config);
console.log(libs.utils);
console.log(libs.foo);
console.log(libs.foo());
What makes me wonder is one of your comments above, that you get an error libs is not defined. That looks like you should have other unnoticed errors before.. during the the initialization of your shared.libs module..