I am attempting to use this module in node.js and am running into an "Error: Cannot find module 'togeojson'" error when I attempt to use the documented example code:
// using togeojson in nodejs
var tj = require('togeojson'),
fs = require('fs'),
// node doesn't have xml parsing or a dom. use xmldom
DOMParser = require('xmldom').DOMParser;
var kml = new DOMParser().parseFromString(fs.readFileSync('foo.kml', 'utf8'));
var converted = tj.kml(kml);
var convertedWithStyles = tj.kml(kml, { styles: true });
I ran npm init in the same directory that my app.js file (where the above code resides) is stored and I used the --save flag when installing the #mapbox/togeojson package to my application.
I am running node version 8.11.2 and npm v 6.1.0.
How do I go about debugging an issue like this in node/npm?
It is #mapbox/togeojson package, not togeojson, so it should be required like:
var tj = require('#mapbox/togeojson');
Related
I'm building an Electron app (with Ionic) to digitally sign PDFs with p12 certificates using node-signpdf (https://www.npmjs.com/package/node-signpdf). I've had problems which I solved by using require ('electron').remote for example for fs.
I've also installed the same node version in my OS (MacOS Catalina) as the same one that electron is using (Node v12.4.0).
The problem is that one of the NPM dependencies uses crypto and it shows as undefined with the next error:
HomePage.html:39 ERROR TypeError: _crypto.randomBytes is not a function
at Object.ctx.seedFileSync (prng.js:340)
at _reseedSync (prng.js:210)
at Object.ctx.generateSync (prng.js:163)
at Object.ctx.generate (prng.js:80)
at Object.ctx.getBytes (random.js:92)
at _modPow (rsa.js:431)
at Object.push../node_modules/node-forge/lib/rsa.js.pki.rsa.encrypt (rsa.js:501)
at Object.key.sign (rsa.js:1245)
at addSignerInfos (pkcs7.js:534)
at Object.sign (pkcs7.js:377)
What I can see is that node-signpdf uses node-forge as a dependency, and node-forge loads crypto inside prng.js this way:
var _crypto = null;
if(forge.util.isNodejs && !forge.options.usePureJavaScript &&
!process.versions['node-webkit']) {
_crypto = require('crypto');
}
I've tried changing that part of the code to use crypto-js or browserfy-crypto (this last one doesn't even build and hasn't been updated in years), but I keep getting the error shown above.
EDIT 1:
This is how I'm implementing the signature in my service:
public signFile(pathToFile: string, pathToCert: string): void {
const fs = (<any>window).require('fs');
let certBuffer = fs.readFileSync(pathToCert);
let fileBuffer = fs.readFileSync(pathToFile);
fileBuffer = plainAddPlaceholder({
pdfBuffer: fileBuffer,
reason: 'I have reviewed it.',
signatureLength: 1612,
});
const signedPdf = signer.sign(fileBuffer, certBuffer, {passphrase: 'qwertyui'});
}
The code adds the placeholder to add the signature, the problem comes in signer.sign
EDIT 2:
When I run the app if I type in console: require ('crypto') I see the methods, so it looks like it's loaded in the global scope, the problem seems to be in the NPM dependency of node-forge.
EDIT 3:
I've changed require('crypto') to window.require('crypto') and started working. But I think I'll have to make postinstall script to modify it.
Is there a better way?
-
How can I make this crypto thing work? I ran out of ideas. Maybe you can spare some?
Thanks for your time!
I am trying to get Mozilla's BrowserQuest up and running and have run into a problem. It depends on the websocket-server node package, and unfortunately that has been deprecated and removed from the npm library.
I have added the ws and node-websocket-server packages, however neither of them seem to be an easy replacement for the existing calls to websocket-server.
One prior SO post I saw said to replace websocket-server with node-websocket-server in the package.json file, however there are existing direct calls to websocket-server in the BrowserQuest codebase.
I also tried updating the server/js/ws.js file directly as follows (added node- to the websocket-server fields):
var cls = require("./lib/class"),
url = require('url'),
wsserver = require("node-websocket-server"),
miksagoConnection = require('node-websocket-server/lib/ws/connection'),
worlizeRequest = require('websocket').request,
http = require('http'),
Utils = require('./utils'),
_ = require('underscore'),
BISON = require('bison'),
WS = {},
useBison = false;
which leads to the following error:
/homenode/browserquest/BrowserQuest/server/js/map.js:15
path.exists(filepath, function(exists) {
^
TypeError: undefined is not a function
at module.exports.cls.Class.extend.init (/home/node/browserquest/BrowserQuest/server/js/map.js:15:11)
at Class (/home/node/browserquest/BrowserQuest/server/js/lib/class.js:50:23)
at module.exports.cls.Class.extend.run (/home/node/browserquest/BrowserQuest/server/js/worldserver.js:151:20)
at /homenode/browserquest/BrowserQuest/server/js/main.js:79:15
at Function._.each._.forEach (/home/node/browserquest/BrowserQuest/node_modules/underscore/underscore.js:153:9)
at main (/home/node/browserquest/BrowserQuest/server/js/main.js:77:7)
at /home/node/browserquest/BrowserQuest/server/js/main.js:134:13
at /home/node/browserquest/BrowserQuest/server/js/main.js:117:13
at fs.js:334:14
at FSReqWrap.oncomplete (fs.js:95:15)
This is updated! I also got it to work today!!
https://github.com/nenuadrian/BrowserQuest
How we can read a pom.xml and get the 'artifactId' 'groupId' etc in the GruntFile.js.
'xml-parser'- I saw in the NPM but how can I use that in GruntFile.js , Anyone has any example . I am very new to Grunt .
Thanks for your advise.
Search npm registry for XML parser
var fs = require('fs');
var parse = require('xml-parser');
var xml = fs.readFileSync('example.xml', 'utf8');
var obj = parse(xml);
obj is your parsed xml
The best way is to use 'pom-parser' (https://github.com/intuit/node-pom-parser)
Install the package:
npm install --save node-pom-parser
then, in the grunt file
var ext = require('pom-parser');
var pom = ext.parsePom({ filePath: "pom.xml"});
var artifactId = pom.artifactId;
console.log(artifactId)
module.exports = function(grunt) {
//GRUNT ....
}
But it won't work in Windows. https://github.com/marcellodesales/node-pom-parser/issues/1
UPDATE: pom-parser has been refactored and is now compatible with Windows (see resolution on linked issue).
Is there any module to parse javascript using node.js . I mean we are able to add and remove html content dynamically using cheerio nodejs module.
Similarly, we want to add, remove and manipulate a javascript method/variable. Is there any module to do that. I searched but unable to get one.
Thanks in Advance !!!
I would recommend the recast module. Install it via npm install --save recast. Below is a sample program that will read in the source of a module named user.js in the current directory. It will parse the source into an AST then from the AST re-generate the original source. Modify the AST with help from estraverse before you call recast.print(ast).code.
The source (does not incorporate estraverse -- exercise for the reader):
'use strict';
var recast = require('recast');
var path = require('path');
var fs = require('fs');
var file = path.resolve(__dirname, 'user.js');
var code = fs.readFileSync(file).toString();
var ast = recast.parse(code);
var output = recast.print(ast).code;
console.log(output);
I want the equivalent of $PROGRAM_NAME in Ruby, or ARGV[0] in C-likes, for Node.js. When I have a Node script that looks something like this,
#!/usr/bin/env node
console.log(process.argv[0])
… I get “node” instead of the name of the file that code is saved in. How do I get around this?
You can either use this (if called from your main.js file):
var path = require('path');
var programName = path.basename(__filename);
Or this, anywhere:
var path = require('path');
var programName = path.basename(process.argv[1]);
Use __filename? It returns the currently executing script.
http://nodejs.org/docs/latest/api/globals.html#globals_filename
For working with command line option strings check out the optimist module by Substack. Optimist will make your life much easier.
var inspect = require('eyespect').inspector();
var optimist = require('optimist')
var path = require('path');
var argv = optimist.argv
inspect(argv, 'argv')
var programName = path.basename(__filename);
inspect(programName, 'programName')
To install the needed dependencies:
npm install -S eyespect optimist