tslog icon indicating copy to clipboard operation
tslog copied to clipboard

Error: Dynamic require of "path" is not supported

Open mikecann opened this issue 2 years ago • 2 comments

Describe the bug Im trying to use this awesome looking lib with esbuild targeting cloudflare workers and I am getting the following error:

[mf:err] Error: Dynamic require of "path" is not supported
    at __require (C:\dev\me\tinkering-with-durable-objects\packages\server\dist\index.mjs:28:9)
    at ../../node_modules/source-map-support/source-map-support.js (C:\dev\me\tinkering-with-durable-objects\packages\server\dist\index.mjs:
1812:16)
    at __require2 (C:\dev\me\tinkering-with-durable-objects\packages\server\dist\index.mjs:31:44)
    at ../../node_modules/tslog/dist/cjs/Logger.js (C:\dev\me\tinkering-with-durable-objects\packages\server\dist\index.mjs:2917:32)
    at __require2 (C:\dev\me\tinkering-with-durable-objects\packages\server\dist\index.mjs:31:44)
    at ../../node_modules/tslog/dist/cjs/index.js (C:\dev\me\tinkering-with-durable-objects\packages\server\dist\index.mjs:2935:20)
    at __require2 (C:\dev\me\tinkering-with-durable-objects\packages\server\dist\index.mjs:31:44)
    at C:\dev\me\tinkering-with-durable-objects\packages\server\dist\index.mjs:4604:31
    at SourceTextModule.evaluate (internal/vm/module.js:225:23)
    at ModuleScriptInstance.run (C:\dev\me\tinkering-with-durable-objects\node_modules\miniflare\src\scripts.ts:66:23)

Clicking the link in the stack trace takes me to:

// ../../node_modules/tslog/dist/cjs/Logger.js
var require_Logger = __commonJS({
  "../../node_modules/tslog/dist/cjs/Logger.js"(exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.Logger = void 0;
    var source_map_support_1 = require_source_map_support();
    var LoggerWithoutCallSite_1 = require_LoggerWithoutCallSite();
    var Logger2 = class extends LoggerWithoutCallSite_1.LoggerWithoutCallSite {
      constructor(settings, parentSettings) {
        super(settings, parentSettings);
        this._callSiteWrapper = source_map_support_1.wrapCallSite;
      }
    };
    exports.Logger = Logger2;
  }
});

It looks like the culprit is require_source_map_support(). Im not sure if cloudflare workers have some sort of special requirements or whatnot or if its esbuild that is stripping something out.

Any ideas?

mikecann avatar Aug 18 '21 12:08 mikecann

I ran into the same error but no cloudflare in my case. I found a workaround for local development that displays source information with esm+tslog+esbuild+typescript: npx esbuild src/index.ts --bundle --platform=node --outdir=dist --sourcemap=inline --sources-content=false --format=esm --external:source-map-support --out-extension:.js=.mjs --main-fields=module.

Although that worked, I then ran into https://github.com/fullstack-build/tslog/issues/99 and decided to abandon the esm output format, allowing me to remove the workaround: npx esbuild src/index.ts --bundle --platform=node --outdir=dist --sourcemap=inline --sources-content=false.

denosaurtrain avatar Dec 03 '21 20:12 denosaurtrain

Not quite. Looks like a problem with CJS. V4 is going to be a huge rewrite and ESM first. I hope it would fix that.

terehov avatar Aug 23 '22 19:08 terehov

There is a new next release of version 4.0. It is ESM only. Could you give it a go and see if it helps? I would appreciate your feedback.

  • npm i tslog@next

  • and run it with node --enable-source-maps or for TypeScript node --enable-source-maps --experimental-specifier-resolution=node --no-warnings --loader ts-node/esm

terehov avatar Sep 29 '22 10:09 terehov

@terehov, I tried a few configurations and couldn't get version 4.0.0-1 to work either.

I suspect the issue is that node_modules/tslog/package.json contains "exports": "./dist/index.js" which refers to a file that does not exist.

It's also possible that my configuration is incorrect. Here's what I have:

src/index.ts

import { Logger } from "tslog"

package.json

{
    "type": "module",
    "dependencies": {
        "tslog": "4.0.0-1",
        "typescript": "4.8.4"
    },
    "devDependencies": {
        "@types/node": "18.8.5",
        "ts-node": "10.9.1"
    }
}

tsconfig.json

{
    "compilerOptions": {
        "sourceMap": true,
        "target": "ESNext",
        "moduleResolution": "node",
        "module": "ESNext",
    },
}

Command node --enable-source-maps --experimental-specifier-resolution=node --no-warnings --loader ts-node/esm src/index.ts

Error

Could not find a declaration file for module 'tslog'. '/REDACTED/node_modules/tslog/dist/nodejs/index.js' implicitly has an 'any' type.

I tried a few variations on the tsconfig, including "moduleResolution": "NodeNext" which yields a very similar error:

Cannot find module 'tslog' or its corresponding type declarations

I'm not actively using tslog so if you want faster back-and-forth comms, I could meet you on Discord or the like.

denosaurtrain avatar Oct 13 '22 17:10 denosaurtrain

Well spotted, thank you. Can you please try this one:

npm i [email protected]

terehov avatar Oct 16 '22 18:10 terehov

@terehov Nice! I don't get an error when I import tslog. However, I'm also not quite there yet. With [email protected], I used the following code to configure my logging in one place, then import that log object from elsewhere in my project:

import { Logger } from "tslog"

export const log: Logger = new Logger({
    displayInstanceName: true,
    displayDateTime: false,
    minLevel: "silly",
})

In [email protected], that code yields the following two TS errors:

error TS2314: Generic type 'Logger<LogObj>' requires 1 type argument(s). error TS2345...'displayInstanceName' does not exist in type 'ISettingsParam'

I haven't (yet?) found how to resolve either of those. Admittedly, I only spent about 10 minutes looking through the source code so far. (I'm not actively working on this project, sadly.) I can maybe figure out the second error, assuming the field has been moved/renamed or something. However, I'm not sure whether this LogObj generic parameter fits with how I was exporting my logger config in 3.x. Is that no longer a good approach?

denosaurtrain avatar Oct 16 '22 23:10 denosaurtrain

That's because the API has changed. I have updated the documentation: tslog.js.org

V4 is released now, so I'm going to close this issue. If you shouldn't be able to find the solution, I would be happy to help.

terehov avatar Nov 15 '22 18:11 terehov