Browserify cannot find installed module sha3 - node.js

I am working on ethereumjs-libpackage installed from npm which has a dependency on sha3 module. However browserify is not able to pick it up from the dependencies. I installed sha3 manually and wrote the simple code shown below, still browserify shows me the error Cannot find module 'sha3'
var unique = require('uniq');
var sha = require('sha3');
var data =[1,2,2,3,4,5,5,5,6];
var uniqueArray = unique(data);
for(var i = 0; i < uniqueArray.length ; i++){
console.log(uniqueArray[i]);
}

Related

Error: Cannot find module 'togeojson'

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');

Getting error while using Imagemagick npm

I am using Imagemagick npm in node to compress an image. I installed the npm module and it was working fine. But after some time, it throws the error shown below.
TypeError: currentLine.search is not a function
If I install the module again, it doesn't show the error. But again after some time, it throws the same error. I am using 0.1.3 version of Imagemagick with 8.9.1 version of node.
Can any one help me with this issue?
Even I got the same issue for 0.1.3 npm version. After some debugging, I found that if we add custom functions into Array.prototype like this
Array.prototype.asyncforEach = async function(cbk) {
for (let i = 0; i < this.length; i++) await cbk(this[i], i, this);
};
Which will leads to an error in file node_modules/imagemagick/imagemagick.js at line number 113 i will have asyncforEach as a value so we have to change our Array.prototype.methodName like below or reference this for better understanding.
Object.defineProperty(Array.prototype, 'asyncforEach', {
value: async function(cbk) {
for (let i = 0; i < this.length; i++) await cbk(this[i], i, this);
}
});

Webpack ContextReplacementPlugin get packages from node_modules

We are trying to make a webpack bundle, which will include all of our dependencies inside a single bundle.
There is a specific package, which has dynamic requires from node_modules, and webpack can't resolve the packages correctly at compile time.
The code that generates the error is:
// config.middlewares is a list of packages inside node_modules
Object.keys(config.middlewares).forEach(function (moduleName) {
var pkg = require(moduleName)
var alias = config.middlewares[moduleName]
helmet[alias] = pkg
})
Webpack compiles it into:
Object.keys(config.middlewares).forEach(function (moduleName) {
var pkg = !(function webpackMissingModule() { var e = new Error("Cannot find module \".\""); e.code = 'MODULE_NOT_FOUND'; throw e; }())
var alias = config.middlewares[moduleName]
helmet[alias] = pkg
})
So we tried using Webpack's ContextReplacementPlugin, but whatever we tried we couldn't configure it to change the context to fetch a file from node_modules.
Somethings we tried (we first tried to just get one specific package, called hsts):
new ContextReplacementPlugin(/helmet.*/, /\.\.\/hsts/)
new ContextReplacementPlugin(/helmet.*/, /\.\/\.\.\/hsts/)
new ContextReplacementPlugin(/helmet.*/, /\.\/\.\.\/hsts\/index\.js/)
```

Waiting on 1 test

I have the following test
// tests/CheckboxWithLabel-test.js
jest.dontMock('../test.js');
describe('CheckboxWithLabel', function() {
it('changes the text after click', function() {
var React = require('react/addons');
var CheckboxWithLabel = require('../test.js');
var TestUtils = React.addons.TestUtils;
// Render a checkbox with label in the document
var checkbox = TestUtils.renderIntoDocument(
<CheckboxWithLabel labelOn="On" labelOff="Off" />
);
// Verify that it's Off by default
var label = TestUtils.findRenderedDOMComponentWithTag(
checkbox, 'label');
expect(label.getDOMNode().textContent).toEqual('Off');
// Simulate a click and verify that it is now On
var input = TestUtils.findRenderedDOMComponentWithTag(
checkbox, 'input');
TestUtils.Simulate.change(input);
expect(label.getDOMNode().textContent).toEqual('On');
});
});
when I try to run it though using jest, I get the following error: Waiting on 1 test
I run npm test
What should I do?
You did not specify the version of Node you were using. Your problem could be because you are using a newer version of Node and Jest works with version 0.10.0. This is an open issue on Github (https://github.com/facebook/jest/issues/243)
To downgrade your version of node, use the n package as follows:
npm install -g n # Install n globally
n 0.10 # Installing the correct version of node
This can result in some weird problems with your current packages, so delete your node_modules folder and perform a clean install.

How to read a pom.xml and get 'artifactId' in Grunt

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).

Resources