Skip to content

Problem reading module.parent.filename when using commonjs #88

@vadymrybak

Description

@vadymrybak

While running the app locally (npm run dev) - everything is fine. Package.json below:

{
  "name": "workflow-mfe-server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "prebuild": "tslint -c tslint.json -p tsconfig.json --fix",
    "build:prod": "webpack --env prod",
    "build:dev": "webpack --env dev",
    "prestart": "npm run prebuild",
    "start": "ts-node -r ./src/tracer.ts ./src/index.ts",
    "dev": "nodemon",
    "test": "jest --verbose=true --coverage --forceExit"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@opentelemetry/api": "^1.0.3",
    "@opentelemetry/exporter-collector-grpc": "^0.24.0",
    "@opentelemetry/instrumentation": "^0.24.0",
    "@opentelemetry/instrumentation-express": "^0.25.0",
    "@opentelemetry/instrumentation-http": "^0.24.0",
    "@opentelemetry/sdk-node": "^0.24.0",
    "axios": "^0.27.2",
    "chalk": "^4.1.0",
    "cookie-parser": "^1.4.6",
    "crypto": "^1.0.1",
    "dotenv-flow": "^3.1.0",
    "express": "^4.17.1",
    "express-http-context": "^1.2.4",
    "jwt-decode": "^3.1.2",
    "licensing-lib-js": "^1.0.14",
    "logform": "^2.2.0",
    "prom-client": "^14.0.1",
    "prometheus-api-metrics": "^3.2.2",
    "uuid": "^8.3.2",
    "winston": "^3.3.3"
  },
  "devDependencies": {
    "@types/cookie-parser": "^1.4.2",
    "@types/dotenv-flow": "^3.2.0",
    "@types/express": "^4.17.11",
    "@types/jest": "^26.0.21",
    "@types/logform": "^1.10.1",
    "@types/supertest": "^2.0.11",
    "@types/uuid": "^8.3.1",
    "@types/winston": "^2.4.4",
    "dotenv": "^8.2.0",
    "jest": "^26.6.3",
    "supertest": "^6.1.6",
    "ts-jest": "^26.5.6",
    "ts-loader": "^8.0.17",
    "ts-node": "^10.8.2",
    "typescript": "^4.2.3",
    "webpack": "^5.24.2",
    "webpack-cli": "^4.5.0"
  },
  "nodemonConfig": {
    "ignore": [
      "**/*.test.ts",
      "**/*.spec.ts",
      ".git",
      "node_modules",
      "dist"
    ],
    "watch": [
      "src"
    ],
    "exec": "npm start",
    "ext": "ts"
  }
}

Tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "esModuleInterop": true,
        "target": "es5",
        "noImplicitAny": true,
        "moduleResolution": "node",
        "sourceMap": true,
        "outDir": "dist",
        "baseUrl": ".",
        "paths": {
            "*": [
                "node_modules/*"
            ]
        }
    },
    "include": [
        "src/**/*"
    ]
}

The problem occurs when I build production app. When trying to launch the server by calling "node index.js" - I'm getting error:

Error: Could not find package.json up from 53263

By digging some source code I found this line of code in index.js of prometheus-api-metrics:

require('pkginfo')(metricsMiddleware, { dir: Path.dirname(module.parent.filename), include: ['name', 'version'] });

Apparently, when using "commonjs" - module.parent does not exist and it is causing a problem.

Does anyone have any solution for this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions