I am trying to get the Winston logging library working with my Sapper app. That Sapper app uses Rollup as module bundler. To implement my app, I basically followed the following steps:
Start new Sapper project that uses Rollup:
npx degit "sveltejs/sapper-template#rollup" my-app
Install winston via npm install winston
and add custom logger factory in file src/_logging.js
:
import { createLogger, transports, format } from 'winston';
export function createLabeledLogger(label) {
return createLogger({
level: 'debug',
transports: [new transports.Console()],
format: format.combine(
format.label({ label: label, message: true }),
format.splat(),
format.padLevels(),
format.simple(),
)
});
}
Obtain the logger via the factory and use the logger in server route src/routes/blog/_posts.js
:
import { createLabeledLogger } from '../../_logging';
const logger = createLabeledLogger('posts-server');
const posts = [
// ...
];
posts.forEach(post => {
// ...
});
logger.debug(`Created ${posts.length} posts for client.`)
This works like a charm:
✔ service worker (16ms)
debug: [posts-server] Created 5 posts for client.
> Listening on http://localhost:5001
However, I've been struggling with getting that logger working in the browser.
For that purpose, I introduced the logger in client route src/routes/blog/index.svelte
:
<script>
import { onMount } from 'svelte';
import { createLabeledLogger } from '../../_logging';
const logger = createLabeledLogger('posts-page');
export let posts;
onMount(() => {
// console.log(`Obtained ${posts.length} posts from server.`)
logger.debug(`Obtained ${posts.length} posts from server.`)
});
</script>
This caused the following error:
✗ client
Unexpected token (Note that you need @rollup/plugin-json to import JSON files)
1: {
2: "_from": "winston",
^
3: "_id": "[email protected]",
4: "_inBundle": false,
So, I installed that suggested plugin via
npm install --save-dev @rollup/plugin-json
and modified rollup.config.js
to import the plugin (import json from '@rollup/plugin-json';
) and to use it in the client bundle (json()
).
While this fixes above "JSON" issue, it causes new trouble:
'util' is imported by node_modules/winston/dist/winston/common.js, but could not be resolved – treating it as an external dependency
'os' is imported by node_modules/winston/dist/winston/exception-handler.js, but could not be resolved – treating it as an external dependency
'os' is imported by node_modules/winston/dist/winston/rejection-handler.js, but could not be resolved – treating it as an external dependency
'util' is imported by util?commonjs-external, but could not be resolved – treating it as an external dependency
'os' is imported by os?commonjs-external, but could not be resolved – treating it as an external dependency
'os' is imported by node_modules/winston/dist/winston/transports/file.js, but could not be resolved – treating it as an external dependency
'os' is imported by node_modules/winston/dist/winston/transports/console.js, but could not be resolved – treating it as an external dependency
'os' is imported by node_modules/winston/dist/winston/transports/stream.js, but could not be resolved – treating it as an external dependency
'util' is imported by node_modules/winston-transport/dist/index.js, but could not be resolved – treating it as an external dependency
'path' is imported by node_modules/winston/dist/winston/transports/file.js, but could not be resolved – treating it as an external dependency
'zlib' is imported by node_modules/winston/dist/winston/transports/file.js, but could not be resolved – treating it as an external dependency
'fs' is imported by node_modules/winston/dist/winston/transports/file.js, but could not be resolved – treating it as an external dependency
'https' is imported by node_modules/winston/dist/winston/transports/http.js, but could not be resolved – treating it as an external dependency
'http' is imported by node_modules/winston/dist/winston/transports/http.js, but could not be resolved – treating it as an external dependency
[...]
Based on this answer, I installed rollup-plugin-node-builtins
, and added it to rollup.config.js
analogously to the JSON plugin (import builtins from 'rollup-plugin-node-builtins';
and builtins()
).
This again solves above "could not be resolved" issues -- and again causes new trouble:
preferring built-in module 'string_decoder' over local alternative at '/private/tmp/mburger/my-app/node_modules/string_decoder/lib/string_decoder.js', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning
preferring built-in module 'string_decoder' over local alternative at '/private/tmp/mburger/my-app/node_modules/string_decoder/lib/string_decoder.js', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning
preferring built-in module 'string_decoder' over local alternative at '/private/tmp/mburger/my-app/node_modules/string_decoder/lib/string_decoder.js', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning
preferring built-in module 'string_decoder' over local alternative at '/private/tmp/mburger/my-app/node_modules/string_decoder/lib/string_decoder.js', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning
preferring built-in module 'string_decoder' over local alternative at '/private/tmp/mburger/my-app/node_modules/string_decoder/lib/string_decoder.js', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning
Okay, so I set preferBuiltins
to false
. New trouble arises:
Circular dependency: node_modules/winston-transport/node_modules/readable-stream/lib/_stream_duplex.js -> node_modules/winston-transport/node_modules/readable-stream/lib/_stream_readable.js -> node_modules/winston-transport/node_modules/readable-stream/lib/_stream_duplex.js
Circular dependency: node_modules/winston-transport/node_modules/readable-stream/lib/_stream_duplex.js -> node_modules/winston-transport/node_modules/readable-stream/lib/_stream_readable.js -> /private/tmp/mburger/my-app/node_modules/winston-transport/node_modules/readable-stream/lib/_stream_duplex.js?commonjs-proxy -> node_modules/winston-transport/node_modules/readable-stream/lib/_stream_duplex.js
Circular dependency: node_modules/winston-transport/node_modules/readable-stream/lib/_stream_writable.js -> node_modules/winston-transport/node_modules/readable-stream/lib/_stream_duplex.js -> node_modules/winston-transport/node_modules/readable-stream/lib/_stream_writable.js
Circular dependency: node_modules/winston-transport/node_modules/readable-stream/lib/_stream_writable.js -> node_modules/winston-transport/node_modules/readable-stream/lib/_stream_duplex.js -> /private/tmp/mburger/my-app/node_modules/winston-transport/node_modules/readable-stream/lib/_stream_writable.js?commonjs-proxy -> node_modules/winston-transport/node_modules/readable-stream/lib/_stream_writable.js
Circular dependency: node_modules/rollup-plugin-node-builtins/src/es6/readable-stream/duplex.js -> node_modules/rollup-plugin-node-builtins/src/es6/readable-stream/readable.js -> node_modules/rollup-plugin-node-builtins/src/es6/readable-stream/duplex.js
Circular dependency: node_modules/rollup-plugin-node-builtins/src/es6/readable-stream/duplex.js -> node_modules/rollup-plugin-node-builtins/src/es6/readable-stream/writable.js -> node_modules/rollup-plugin-node-builtins/src/es6/readable-stream/duplex.js
Circular dependency: node_modules/readable-stream/lib/_stream_readable.js -> node_modules/readable-stream/lib/_stream_duplex.js -> node_modules/readable-stream/lib/_stream_readable.js
Circular dependency: node_modules/readable-stream/lib/_stream_duplex.js -> node_modules/readable-stream/lib/_stream_writable.js -> node_modules/readable-stream/lib/_stream_duplex.js
Circular dependency: node_modules/readable-stream/lib/_stream_duplex.js -> node_modules/readable-stream/lib/_stream_writable.js -> /private/tmp/mburger/my-app/node_modules/readable-stream/lib/_stream_duplex.js?commonjs-proxy -> node_modules/readable-stream/lib/_stream_duplex.js
Circular dependency: node_modules/readable-stream/lib/_stream_readable.js -> node_modules/readable-stream/lib/_stream_duplex.js -> /private/tmp/mburger/my-app/node_modules/readable-stream/lib/_stream_readable.js?commonjs-proxy -> node_modules/readable-stream/lib/_stream_readable.js
Now, I followed the instructions at this comment and adapted my rollup.config.js
file accordingly.
Again, a new issue occurs:
Could not resolve './stream/duplex.js' from node_modules/rollup-plugin-node-builtins/src/es6/stream.js
Well, following the instruction at this README, I installed rollup-plugin-node-globals
and added it to my rollup.config.js
file (import globals from 'rollup-plugin-node-globals';
, globals()
).
This results in a similar issue:
Could not resolve './stream/writable.js' from node_modules/rollup-plugin-node-builtins/src/es6/stream.js
I have researched in various Stack Overflow entries, GitHub issue trackers and Google.
Is it possible to use Winston in a Sapper app that uses Rollup as bundler?
Incidentally, to successfully use Winston with the webpack version of the Sapper app, all it required to get it running was to add node: { fs: 'empty' },
to webpack.config.js
-- as this output in the Web Developer console shows:
debug: [posts-page] Obtained 5 posts from server.
Update on June 2
You can find the complete rollup.config.js file as GitHub Gist.
Please note that it neither worked with preferBuiltins
set to true
nor with set to false
.
Update on June 3
The slow descent into the inner circles of (dev) hell continues:
Following ThomasHennes' suggestion, I added the following symlink:
❯ la node_modules/rollup-plugin-node-builtins/src/es6/stream
lrwxr-xr-x 1 mburger wheel 15B Jun 3 12:45 node_modules/rollup-plugin-node-builtins/src/es6/stream -> readable-stream
And again, a different issue comes to light:
• client
'stream/writable' is imported by stream/writable?commonjs-external, but could not be resolved – treating it as an external dependency
'stream/writable' is imported by node_modules/winston-transport/dist/index.js, but could not be resolved – treating it as an external dependency
Circular dependency: node_modules/rollup-plugin-node-builtins/src/es6/readable-stream/duplex.js -> node_modules/rollup-plugin-node-builtins/src/es6/readable-stream/readable.js -> node_modules/rollup-plugin-node-builtins/src/es6/readable-stream/duplex.js
Circular dependency: node_modules/rollup-plugin-node-builtins/src/es6/readable-stream/duplex.js -> node_modules/rollup-plugin-node-builtins/src/es6/readable-stream/writable.js -> node_modules/rollup-plugin-node-builtins/src/es6/readable-stream/duplex.js
preferBuiltins: true
in that earlier step - and did it change anything? – Drypoint./stream/writable.js
after you managed to fix resolving./stream/duplex.js
when both originate in the same library. Looking inside github.com/calvinmetcalf/rollup-plugin-node-builtins/blob/…,writable.js
is imported from a local./readable-stream
directory. Could it be that your aliasingreadable-stream
intostream
interferes and causes the resolver to fail? – Drypointstream
symlink that points to thereadable-stream
directory insidenode_modules/rollup-plugin-node-builtins/src/es6
, and see if it fixes your error. – Drypoint