Cannot resolve module 'react/lib/merge' - javascript

I have a React project, where I am using Babel and Webpack.
In one the files, I have the following require statement:
var merge = require('react/lib/merge');
However, I am getting this error:
ERROR in ./app/stores/CountryStore.js
Module not found: Error: Cannot resolve module 'react/lib/merge'
Is there another way of requiring the component?

'react/lib/merge' is deprecated.
try
npm install merge
and instead of
var merge = require('react/lib/merge');
use
var merge = require('merge');

Related

How do you use a natively compiled Node module in an ES6 module?

If you are writing an ES6 module to run in NodeJS, how do you import another module that includes natively compiled code?
For example if I set "type": "module" in package.json, do an npm install talib and then try to run this:
import talib from 'talib';
I get this error in Node v16.2.0:
node:internal/process/esm_loader:74
internalBinding('errors').triggerUncaughtException(
^
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".node" for node_modules/talib/build/Release/talib.node
at new NodeError (node:internal/errors:363:5)
at Loader.defaultGetFormat [as _getFormat] (node:internal/modules/esm/get_format:71:15)
at Loader.getFormat (node:internal/modules/esm/loader:105:42)
at Loader.getModuleJob (node:internal/modules/esm/loader:243:31)
at async ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:75:21)
at async Promise.all (index 0)
at async link (node:internal/modules/esm/module_job:80:9) {
code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
talib is one example but there are a number of others, all of which exhibit the same behaviour. They all seem to be using node-gyp to build.
How can you use these modules in your own ES6 module code? Do the modules like talib and friends need changes? If so, what should be changed and is it backwards compatible?
Surprisingly - this works for me:
/*eslint-disable */
const talib = require("talib")
/*eslint-enable */

VueJS Grpc-Web module not found

I've created a fresh VueJS application with TypeScript functionality.
When I generate using:
protoc -I=. service.proto --js_out=import_style=typescript:. --grpc web_out=import_style=typescript,mode=grpcwebtext:.
I get the following files:
When I move them to src/_protos in my VueJS project and try to import { PlatformClient } from '#/_protos/ServiceServiceClientPb'; it gives me the following error:
Failed to compile.
./src/_protos/ServiceServiceClientPb.ts
Module not found: Error: Can't resolve './service_pb' in '/Users/theobouwman/dev/woodyshousing/woody_web/src/_protos'
Why is this?
I believe this is resolved in https://github.com/grpc/grpc-web/issues/431.
In short, --js_out=import_style=typescript:. is not supposed to work. You need to do --js_out=import_style=commonjs:. --grpc-web_out=import_style=commonjs+dts,mode=grpcwebtext:.

Cannot put winston inside a nodejs module

I'm separating my application in modules, but each of these modules have functions that must me logged. So my idea was to create a file winstonConfig.js which would configure the winston, and then I'd require this file in each of the modules that need to log things. Here's winstonConfig.js:
var winston = require('winston');
winston.add(winston.transports.Riak, { level: 'warn' });
winston.add(winston.transports.File, { filename: 'mylogfile.log', level: 'silly' });
exports.log = winston.log;
exports.debug = winston.debug;
exports.error = winston.error;
(btw, is there a way to export everything once?)
but when I require('./winstonConfig.js') in my index.js, I get:
node_modules/winston/lib/winston/logger.js:481
var instance = created ? transport : (new (transport)(options));
^
TypeError: transport is not a constructor
but the exact same code (without exports) will work without any problem when put in index.js (the problem is that then I cannot import this to other modules)
I got the same error with a new project, but when I was trying to use "logger.transports.DailyRotateFile". Then I realized that my code (that was copied from another project) just works for winston 1 (same version used on the other project). So, I just downgraded the lib to version 1 and everything worked normally:
$ npm uninstall winston --save
$ npm install winston#1.x.x --save
But if you want to use winston 2, I found the solution below to use DailyRotateFile - and, probably, there is a similar solution to use other kind of transports.
var winston = require('winston'), expressWinston = require('express-winston');
winston.transports.DailyRotateFile = require('winston-daily-rotate-file');
It looks like you're missing:
require('winston-riak');
I get the same error with your code.
If I comment out the addition of the Riak transport I get no error.
If I require winston-riak:
var winston = require('winston');
require('winston-riak');
winston.add(winston.transports.Riak, { level: 'warn' });
winston.add(winston.transports.File, { filename: 'mylogfile.log', level: 'debug' });
exports.log = winston.log;
exports.debug = winston.debug;
exports.error = winston.error;
I get error: TypeError: riakjs.getClient is not a function. This appears to be because 'winston-riak' tries to execute riakjs.getClient(options) but, per https://github.com/mostlyserious/riak-js/issues/234, getClient is the exported function, rather than a method of the exported object. The winston-riak module hasn't been updated for 5 years. It seems it is not compatible with the current riak-js, which was updated 2 years ago.

export node module to local error

i am a newby on node.js. i want to parse a xml file into json .so i am trying to use bulebutton library from https://github.com/blue-button/bluebutton.js .
first i have installed the module by command npm install bluebutton and its created a node_modules folder with bluebutton module.
now i created a test.js file with following code
var bb = require('bluebutton');
var myRecord = bb.BlueButton('./asd.xml');
console.log(myRecord);
but its gave me an error that bluebutton is not define .please help me to figureout this problem thanks
REVISED ANSWER
From bluebuttonjs.com/docs, the require statement you use would return the BlueButton object, so bb represents said object, and it would be called like so
var myRecord = bb('someFile.xml');
However you might also note that they use fs to read the file before passing it. http://www.bluebuttonjs.com/docs/#parsing-node
PREVIOUS ANSWER (for wrong module)
According to their npm docs, you need to do
var bb = require('blue-button');
https://www.npmjs.com/package/blue-button

Meteor Npm.require() cannot find a file in parent directory

I'm trying to get an abandoned nodejs library to work under Meteor.
Why does Npm.require('./crypto-js/crypto') work fine, but Npm.require('../convert') is throwing the error Error: Cannot find module '../convert'?
/packages/myPackage/package.js
Package.on_use(function (api) {
var path = Npm.require('path');
api.add_files(path.join('convert.js'), 'server');
api.add_files(path.join('crypto-js', 'crypto.js'), 'server');
api.add_files(path.join('util.js'), 'server');
});
/packages/myPackage/convert.js
myFunc = function() {
return true
}
/packages/myPackage/util.js
Crypto = Npm.require('./crypto-js/crypto');
/packages/myPackage/crypto-js/crypto.js
var conv = Npm.require('../convert')
Already answered to this on IRC but posting here for the reference.
The author wanted to use a fork of npm module hosted on GitHub. To import the npm module from certain repo and certain commit, we can use GitHub's tarball url.
Create a smart package in /packages/package-name
In /packages/package-name/package.js describe the package, add files, export variables
add Npm.depends to package.js file looking like this:
Npm.depends({'NPM-MODULE-NAME': "https://github.com/REPOAUTHOR/REPONAME/tarball/COMMIT-SHA1"});
in one of the smart package's files do ExportSymbol = Npm.require('NPM-MODULE-NAME')
export the export symbol
example: https://github.com/Slava/meteor-npm-fork-example

Categories