Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Class constructor File cannot be invoked without 'new' #7249

Closed
jdim opened this issue Nov 4, 2021 · 1 comment
Closed

TypeError: Class constructor File cannot be invoked without 'new' #7249

jdim opened this issue Nov 4, 2021 · 1 comment

Comments

@jdim
Copy link

jdim commented Nov 4, 2021

馃悰 bug report

Hello, I noticed that can't run some code built for Node.js target. I caught the following error:

/home/dmitriy/git/jdim/parcel-winston/src/index.ts:13
    new winston.transports.File({ filename: `./error.log`, level: 'error' }),
                           ^
TypeError: Class constructor File cannot be invoked without 'new'
    at Object.<anonymous> (/home/dmitriy/git/jdim/parcel-winston/src/index.ts:13:28)
    at Module._compile (internal/modules/cjs/loader.js:1072:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47

But if I compile it with tsc it works.

馃帥 Configuration (.babelrc, package.json, cli command)

package.json:

{
  "name": "parcel-winston",
  "version": "1.0.0",
  "description": "",
  "source": "src/index.ts",
  "main": "dist/main.js",
  "engines": {
    "node": ">= 14"
  },
  "scripts": {
    "build": "parcel build --no-cache",
    "app": "node ./dist/main.js",
    "build.tsc": "npx tsc --outDir ./dist",
    "app.tsc": "node ./dist/index.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "source-map-support": "^0.5.20",
    "winston": "^3.3.3"
  },
  "devDependencies": {
    "@types/node": "^16.10.9",
    "@typescript-eslint/eslint-plugin": "^5.1.0",
    "@typescript-eslint/parser": "^5.1.0",
    "eslint": "^8.0.1",
    "parcel": "latest",
    "typescript": "^4.4.4"
  }
}

I am using the following scripts:

  1. npm run build
  2. npm run app

And I see the error.

But I can run it after:

  1. npm run build.tsc
  2. npm run app.tsc

You'll see no errors, and two log files in the project folder will be created.

馃 Expected Behavior

Build runs in Node.js without errors.

馃槸 Current Behavior

馃拋 Possible Solution

https://stackoverflow.com/questions/50203369/class-constructor-cannot-be-invoked-without-new-typescript-with-commonjs

According to this answer, you need transpile typescript for modern Node.js target to ES6 and newer.
Modern Node.js versions support ES6 classes, there is no need to transpile them.

Maybe you can read target from tsconfig.json.

馃敠 Context

馃捇 Code Sample

Code fragment:

import "source-map-support/register";
import winston from 'winston';

const logger = winston.createLogger({
  level: 'info',
  format: winston.format.json(),
  defaultMeta: { service: 'user-service' },
  transports: [
    //
    // - Write all logs with level `error` and below to `error.log`
    // - Write all logs with level `info` and below to `combined.log`
    // More about levels: https://github.com/winstonjs/winston#logging
    new winston.transports.File({ filename: `./error.log`, level: 'error' }),
    new winston.transports.File({ filename: `./combined.log` }),
    new winston.transports.Console({
      format: winston.format.simple(),
    }),
  ],
});


export default logger;

Also, I've created a tiny repository, so you can reproduce this on your local machine

https://github.com/jdim/parcel-winston

馃實 Your Environment

Software Version(s)
Parcel 2.0.0
Node v14.17.6
npm/Yarn npm 7.24.0
Operating System Ubuntu 20.04.3 LTS (WSL2)
@mischnic
Copy link
Member

mischnic commented Nov 7, 2021

The fix was already merged: #7113

@mischnic mischnic closed this as completed Nov 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants