expressjs / morgan

HTTP request logger middleware for node.js
MIT License
7.93k stars 531 forks source link

Deprecation warning when using import #190

Open rigwild opened 5 years ago

rigwild commented 5 years ago

When using import with esm using morgan, I get the following warning message :

morgan deprecated default format: use combined format node_modules\esm\esm.js:1:277757

This does not occur when using require. Example : https://repl.it/repls/SeagreenGrippingHexagon

On Morgan v1.9.1.

dougwilson commented 5 years ago

Thanks for the report. This should only display if you're trying to get the deprecated morgan.default property, though. I'll need to dig in to see what is going on.

rigwild commented 5 years ago

import loads default by default. This default property needs to be renamed. There's probably a workaround for this but I can't help.

dougwilson commented 5 years ago

So it will be renamed eventually, which is why it is deprecated.

dhowe commented 5 years ago

Same problem here. Is there anyway to get rid of the distracting 'deprecated' warning without forking and changing the source code?

dougwilson commented 5 years ago

I haven't yet investigated what changes can be made to this module to fix the deprecation warning for the import syntax, but the warnings can always be suppressed from printing, though node command line switch, event listener, environment variable. You can read more here about how the deprecation system works if that helps at all for the time being: https://www.npmjs.com/package/depd

dhowe commented 5 years ago

perhaps remove the deprecation warning until you have time to investigate further?

dougwilson commented 5 years ago

The deprecation warning was added almost 5 years ago and there was no issue until this issue. Perhaps if this issue was opened closer to when it was added reverting would be considered, but whatever has changed since then isn't this module.

dhowe commented 5 years ago

My guess is that people are simply using the 'import' style more now. The fix is just a comment (see this PR), but of course its your call...

mbrowne commented 5 years ago

Here is a fix that leaves the deprecation message in place: #207

Note that module.exports.default is still pointing to the default format function. I'm not sure if that might confuse any other tools that parse ES6 exports, but this at least prevents the deprecation warning from being erroneously shown when using esm.

josemvcerqueira commented 5 years ago

Quick fix is to pass "tiny" instead of dev to morgan.

mbrowne commented 4 years ago

@dougwilson I tried updating my app to use native ES modules now that they have landed in node (since node 13.2.0), but ran into some issues, so need to stick with esm for now. (It seems it will be a while before the ecosystem catches up with native ES modules to make adopting them a little more seamless.) So if you were to release a new major version of morgan that drops the deprecated default format (as you previously mentioned as a possibility), that would still be very helpful in the meantime.

laurlas commented 4 years ago

This will do the trick import * as morgan from 'morgan'

ryhinchey commented 4 years ago

To be clear, this issue is specific to using the esm package and not the import syntax in general.

Testing this with a basic import of import morgan from 'morgan' in node 12 or 13 does not produce the deprecation notice.

chriscalo commented 4 years ago

This will do the trick import * as morgan from 'morgan'

I was hopeful, but turns out this doesn't work 😕

dougwilson commented 4 years ago

I did some additional research today with the esm module and it looks like the issue is only with the import * syntax, using import morgan from 'morgan' not only did not produce the warning for me, but looking at the import syntax, that seems like the correct method to important this module. Can anyone who is using esm confirm this?

mbrowne commented 4 years ago

This is enough to cause the warning for me:

test.js:

import morgan from 'morgan'
node -r esm test.js

As discussed here, the issue is that morgan has an export called default that isn't actually the default export.

ahmetbcakici commented 4 years ago

same problem still!

danieldare commented 4 years ago

same problem yeah

n2ptune commented 4 years ago

same problem so far..

hectorbus commented 3 years ago

Same thing...

craigcosmo commented 3 years ago

I got this problem. any solutions ?

n2ptune commented 3 years ago

@craigcosmo It was solved by changing the import statement to require.

craigcosmo commented 3 years ago

yeah I tried that, and its fixed

RMERCADOR98 commented 3 years ago

Hey guys!

When you call morgan just try to do this:

app.use(morgan("dev"));

It worked for me, the "dev" is cause i use "npm run dev" (you can change it on package.json, instead of using "start", to start the server, hope i could be helpfull :D

mbrowne commented 3 years ago

@RMERCADOR98 All that does is change to the formatting option named dev described in the readme, which is intended for development (so overly verbose and not ideal for production).

mbrowne commented 3 years ago

In other news, I am using Koa instead of Express in my latest server, so I used koa-morgan, so I'm not getting the warning anymore because the import is now indirect.

Obviously this won't help those who want to use Express or httpServer and so are using morgan directly - for that, the best workaround is still to just use require instead of import, until if/when the next version of morgan is eventually released.

techieshark commented 3 years ago

@mbrowne I can confirm that this prevents the deprecation warning:

const morgan = require('morgan');

... but in typescript, it seems I now lose type information on the file.

The following works, but only if tsconfig has compilerOptions.module = "CommonJS" rather than any of the 'ES...' module options (e.g. es6).

import morgan = require('morgan');
rigwild commented 3 years ago

@dougwilson Every new comment is repeating what has been said before. Maybe you should lock the conversation until resolved.

daniyaniazi commented 3 years ago

app.use(morgan("dev")); this does solve in my case

prajwalmachado commented 3 years ago

const morgan = require('morgan')
app.use(morgan('dev')) solves the issue

mbrowne commented 3 years ago

The require() workaround does the trick.

I've been a fan of the esm library, but there's now a totally different alternative that's worth considering: the new swc transpiler is so fast that it can easily replace esm (I haven't benchmarked it, but it seems to be just as fast if not faster.) And if you set your SWC target to a recent version of node like node 14 or 16, then it basically won't compile anything other than import/export statements, achieving the same goal (unless you're using very new JS features already supported by SWC but not by node...and that would actually be a good thing because it would allow you to use the latest features that you want without a lengthy compilation step, thanks to SWC's speed).

This is the config I used in my .swc file:

{
  "env": {
    "targets": {
      "node": "16"
    }
  },
  "module": {
    "type": "commonjs"
  },
  "jsc": {
    "externalHelpers": true
  },
  "sourceMaps": "inline"
}

Make sure to install @swc/helpers as a production dependency if you use "externalHelpers": true like I did.

SilvinPradhan commented 2 years ago

you could just use const morgan = require("morgan"); instead of using import