Let's say I have a node package like so:-
var debug = require('debug');
debug('starting..');
module.exports = function(){
debug('printing..');
return 'Hello World';
};
And I include debug package as my devDependencies and publish my package in npm. Now when someone installs my package then obviously debug package won't be installed on their end as it's a devDependencies so when the user runs my program it's going to throw error as s/he doesn't have that package installed.
Now, technically I could do this:-
var debug = process.env.DEBUG ? require('debug') : '';
and it'd be fine since the user is specifically trying to debug the program and throwing him/her error is not a problem but what I'd like to know is how do you guys handle this? Just leave debug as dependency of your app? And is there any other solution to this? Any solution is appreciated. Thanks.
Related
I wrote an app with Electron js that encapsulate Ionic apps for editing them live.
So i use a child process to cd {ionic app path} && ionic serve/npm install/npm update for serving and updating packages of the live Ionic app in my Electron container.
No problems with this technique on my side. But when i package my app and use an installer to test it on a clean machine, npm cannot be executed because of nodejs that is not installed on it.
First I was thinking of including a nodejs installer into my main app installer but this does not seem to me that is the good way of doing it.
And after digging on stackoverflow I've found this thread: Install programmatically a NPM package providing its version
that explain how to use npm directly in my code with require("npm"); and that's worked but i was not able to tell npm.install() in which folder i want to run this command what was possible with child process.
I has tried to read the lib but this not seams to be possible: https://github.com/npm/npm/blob/latest/lib/install.js
Do you have any idea what I can do to solve this problem ?
So I've found the answer after digging into this code https://github.com/npm/npm/blob/latest/lib/install.js
Simply use npm like this :
npm.load({}, function (err) {
npm.commands.install(HERE_A_PATH, [], function(er, data){
//callback here
});
npm.on("log", function (msg) {
console.log(msg + '');
});
});
I am not exactly a node newbie, but this issue has me stumped. Basically, this is an issue for me, nobody else on my team. After the usual npm install etc, calling a function returns require(...) is not a function. I have exported it properly and am passing in the parameter properly and again, this seems to be limited to me. Any suggestions?
Function file
const logger = require('../util/logger')('someParam');
^Fails here
Logger file:
function logger(param) {
console.log('this is the param: ', param);
}
module.exports = logger;
I have tried this in node 4.4.2, 6.10.2 and 7.7.1
I forgot about this question. After some time, I decided to reclone the repo and npm install again, and the issue did not present itself. I have no idea what caused the issue as my code was the same as a fresh clone. One of those things.
If I try to use winston module I have sucess. Nevertheless, if I try to use bunyan my application doesn't start neither show any error (or I don't know how to find the error).
I am debugging using Visual Studio Code although I don't think it is relevant to this question.
If I just require winston, I get no error. The issue happens when add "bunyan.createLogger({name: "myapp"})" as showed bellow.
I am not interested to compare bunyan with winston. I am defenitely interested to use bunyan unless there is a reason like conflict with other modules (I mean other require showed bellow). I just mentioned winston here because I can use it successfully and I noted that winston doesn't depend on ".createLogger" (at least the examples I have read never use ".createLogger" for winston and always show it for bunyan).
Something called my attention although I don't know if it is related to this case neither I know the why: I always install the dependencies using npm install "the dependency" -s because I want to keep a track of all modules used in my project in package.json. A bit strange to me that I don't see it after npm installed (see my package.json bellow).
var express = require('express');
var bodyParser = require('body-parser');
var Client = require('node-rest-client').Client;
var bunyan = require('bunyan');
//var winston = require('winston');
var logBunyan = bunyan.createLogger({name: "myapp"});
//winston.log('info', 'Hello distributed log files!');
//winston.info('Hello again distributed logs');
...
package.json (Shouldn't winston and bunyan appears here since both were installed with "- save"?)
{
"name": "myapp",
"main": "server.js",
"dependencies": {
"body-parser": "^1.17.1",
"express": "^4.15.2",
"node-rest-client": "^3.0.3"
}
}
Add to your package.json dependencies for example:
"bunyan": "^1.8.8"
and do npm install and then run your app.
Example app.js:
var bunyan = require('bunyan');
var log = bunyan.createLogger({name: "myapp"});
log.info("Hello!");
The problem was neither with Bunyan nor with NodeJs at all. Today I have similar issue with util.inspect. Whenever, in Visual Studio Code, I reach a line with this code, my server crashed without any useful information. I tried to run straight via Terminal (node server.js) and I saw both util.inspect and bunyan.createLogger work as expected. I dived in such scenario (works via Terminal but doesn't work throw Visual Studio Code) and suddenly I realize that there is certain conflict between the TypeScript version in Visual Studio Code and the one installed in my Debian. I am not that expect to explain in better arguments but it may help someone in similar condition. I did:
1 - download the portable version of Visual Studio Code instead of using npm. I noted a small upgrade, from version 1.8 to 1.10
2 - I updated in terminal, npm, nodejs and typescript
PS.: If I had payed more attention I should easily get the point. Whenever I started the VS Code I got "Version mismatch! global tsc (1.7.5) != VS Code's language service (2.2.1). Inconsistent compile errors might occur" and now such message it is over.
This fixed the error.
Hope it helps some future readers.
I have a new project where I'm using browserify to convert node modules into an sdk that can run inside the browser.
I'm requiring a number of other npm packages like:
var log4js = require('log4js');
That run fine and give me no problems in the browser, however JsSip just will not cooperate. When I do
var JsSIP = require('jssip');
I get
plivowebsdk.js:2 Uncaught Error: Cannot find module '../../package.json'
Looking through the code, it's obvious when it makes this call
var pkg = require('../../package.json');
is where it bombs out. Clearly it cannot find the package.json file, which it uses to pull out version information. I know JsSip is actually built with browersify itself (or used to be) so that it can run in either node or a browser. Is this causing a conflict?
Still sort of new to browserify, is their a configuration option or transformation I can perform to get around this?
Turned out the be browserify errors, re did the build process using the gulp recipes for browersify and works as expected.
I have install karma-requirejs and requires,but when running Karma,The console prompt "No provider for "framework:requirejs"";
please help me,thank you!!
I know this issue is a bit older but I hit it while searching for a solution, so maybe this helps other to solve their problem quicker.
Even without code it might be due to missing the karma-requirejs plugin reference. (That's the issue I just had.)
Just add it to your karma.conf.js:
module.exports = function(config) {
config.set({
...
plugins: [
...
'karma-requirejs'
],
...
});
};
You also might need to install it via npm install karma-requirejs --save-dev if not yet done.