Download / ulog

The universal logger
https://ulog.js.org
MIT License
88 stars 19 forks source link

Q: Ulog for mac command line? #31

Closed abhi7214 closed 4 years ago

abhi7214 commented 4 years ago

Have been using ulog in the browser and it works well.

I am trying to a script like this "node test.js" in the command line. This JS file includes a library that includes ulog.

webpack:////usr/local/lib/node_modules/ulog/browser.js?:3 var qs = location.search.substring(1), ^ ReferenceError: location is not defined

Is there any solution to this? I am writing a test harness for a library, and would like to test some functions without invoking the browser.

Download commented 4 years ago

Yes, ulog is a universal logger, meaning it works on Node JS as well as browsers. However, you will have to use two different Webpack configurations. Looking at the logs you provided, Webpack is creating a browser bundle for you, so it is including the browser version of ulog.

There are 2 ways you can resolve this:

  1. Use ulog directly in Node JS
  2. Build a Node JS bundle

Use ulog directly in Node JS

Ulog uses require by default. So if you write a script like this:

test.js

var log = require('ulog')('my-module')
log.info('Logging a message from ' + log.name)

You should be able to run that script using

node test.js

and it should print to the console just fine.

Build a Node JS bundle

This is more of an advanced use case that is not used often. You can actually build a bundle with Webpack where you target Node JS. Because you are targetting Node JS, Webpack will include the node version of ulog instead of the browser version.

This JS file includes a library that includes ulog.

Is that a public library (so I can look at it?) When you say 'includes ulog' do you actually mean 'depends on ulog'? Because usually in the Node JS/NPM ecosystem, libraries publish a list of other libraries that they depend on, but they do not actually include them. When you install such a library, npm will also install all the dependencies of that library automatically.

Or, are you maybe creating a bundle with Webpack for use in your web project and are you now trying to test that by including the bundle from your test script?

Maybe you can minimize this thing down to a small project and put it on Github somewhere? I can then check it out and apply a fix for you so you can look at my fix and implement it yourself in the real project.

Keep an eye out for v2!

Thanks for using ulog BTW and keep an eye out because I am working hard on making it much better than it is now and I expect to deliver a v2 version somewhere in the coming months. The most important changes will be:

abhi7214 commented 4 years ago

Thank you for the detailed message.

Let me try to build a node-specific webpack configuration (I was missing that step). I will follow your instructions later today, and report back.

On your other questions -

I am already on ulog 2.0.0 Beta7, I love it. Keep up the good work!

abhi7214 commented 4 years ago

Thanks, I got it running by building webpack for node. (simple inclusion of target: node in the configuration file). Your hint pointed me in the right direction.