TypeError: Cannot read property 'combine' of undefined in rollup bundle [monorepo] [yarn workspaces] [nodejs]

TypeError: Cannot read property 'combine' of undefined in rollup bundle [monorepo] [yarn workspaces] [nodejs]

我正在尝试在 yarn monorepo 中创建一个 nodeJS 应用程序包。

将 Typescript 编译为 JS 工作正常(通过 tsc),然后 rollup 也完成了。但是,当 运行 节点中的编译包时,我收到以下异常,指出找不到外部模块:

/Users/benedikt/code/rollup-test/services/nodejs-service-1/dist/rollup/packages/service-utils/lib/logging.js:40
            format: winston_1.format.combine(winston_1.format.colorize(), winston_1.format.simple()),
                                     ^

TypeError: Cannot read property 'combine' of undefined

在我的 rollup.config.js 中,我将所有 node_modules 包都放在外部,这样它们就不会被汇总转译。

配置如下a project that reproduces the issue is available here

UPDATE 我创建了另一个范围更小的示例项目来重现问题 on replit.

rollup.config.js

import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';

const isProduction = process.env.NODE_ENV === 'production';

const defaultExternal = (id) => {
  return (
    !id.startsWith('@vestico') &&
    !id.startsWith(`[=11=]`) &&
    !id.startsWith(`~`) &&
    !id.startsWith(`.`) &&
    !id.startsWith(process.platform === `win32` ? process.cwd() : `/`)
  );
};

export default {
  input: './dist/server/index.js',
  output: {
    dir: './dist/rollup',
    format: 'cjs',
    preserveModules: true,
  },
  external: defaultExternal,
  plugins: [
    resolve({
      rootDir: '../../',
      transformMixedEsModules: true,
      extensions: ['.mjs', '.js', '.json', '.node', '.ts', '.jsx', '.tsx'],
    }),
    commonjs({ sourceMap: !isProduction }),
    json(),
  ],
};

Logging.ts

import { LoggingWinston } from '@google-cloud/logging-winston';
import { transports as wtransports, format, createLogger } from 'winston';

const isProduction = process.env.NODE_ENV === 'production' && process.env.COLD_START_TEST !== 'true';
const transports = isProduction
  ? [
      new LoggingWinston({
        resource: {
          type: 'cloud_run_revision',
          labels: {
            configuration_name: process.env.K_CONFIGURATION!,
            location: process.env.K_LOCATION!,
            revision_name: process.env.K_REVISION!,
            service_name: process.env.K_SERVICE!,
          },
        },
        serviceContext: {
          service: process.env.K_SERVICE!,
          version: process.env.K_REVISION!,
        },
      }),
    ]
  : [
      new wtransports.Console({
        level: 'debug',
        format: format.combine(format.colorize(), format.simple()),
      }),
    ];

export const logger = createLogger({
  level: 'debug',
  transports,
});

Logging.ts(由 tsc 编译 + 由 rollup

转译
'use strict';

var logging = require('../../../_virtual/logging.js_commonjs-exports');
var require$[=13=] = require('@google-cloud/logging-winston');
var require$ = require('winston');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var require$[=13=]__default = /*#__PURE__*/_interopDefaultLegacy(require$[=13=]);
var require$__default = /*#__PURE__*/_interopDefaultLegacy(require$);

(function (exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.initLogging = exports.logger = void 0;
/* eslint-disable prefer-spread */
const logging_winston_1 = require$[=13=]__default["default"];
const winston_1 = require$__default["default"];
const isProduction = process.env.NODE_ENV === 'production' && process.env.COLD_START_TEST !== 'true';
const transports = isProduction
    ? [
        new logging_winston_1.LoggingWinston({
            resource: {
                type: 'cloud_run_revision',
                labels: {
                    configuration_name: process.env.K_CONFIGURATION,
                    location: process.env.K_LOCATION,
                    revision_name: process.env.K_REVISION,
                    service_name: process.env.K_SERVICE,
                },
            },
            serviceContext: {
                service: process.env.K_SERVICE,
                version: process.env.K_REVISION,
            },
        }),
    ]
    : [
        new winston_1.transports.Console({
            level: 'debug',
            format: winston_1.format.combine(winston_1.format.colorize(), winston_1.format.simple()),
        }),
    ];
exports.logger = winston_1.createLogger({
    level: 'debug',
    transports,
});

}(logging.__exports));

package.json

{
  "name": "@vestico/node-service-1",
  "description": "vestico node service 1",
  "author": "Benedikt",
  "license": "UNLICENSED",
  "private": true,
  "version": "1.0.1",
  "main": "server/index",
  "scripts": {
    "dev:run": "tsc --build ./tsconfig.json && nodemon --watch ./ --watch ../../packages src/server/index.ts",
    "dev": "PORT=3011 yarn run dev:run",
    "dev:prod": "PORT=3311 yarn run dev:run",
    "clean": "rm -rf ./dist",
    "compile": "NODE_ENV=production tsc --build ./tsconfig.json && rollup -c",
    "build:prod": "yarn run clean && yarn run compile",
    "start": "NODE_ENV=production COLD_START_TEST=true node -r require-so-slow dist/services/widget-api/src/server/index.js",
    "test": "yarn run clean && NODE_ENV=production tsc --build ./tsconfig.jest.json && jest --env=node test --watch",
    "dev:cold-start": "yarn build:prod && NODE_ENV=production COLD_START_TEST=true node dist/rollup/services/nodejs-service-1/dist/server/index.js"
  },
  "dependencies": {
    "@google-cloud/error-reporting": "^2.0.1",
    "@vestico/service-utils": "^1.0.0",
    "body-parser": "^1.19.0",
    "compression": "^1.7.4",
    "express": "^4.17.1",
    "gaxios": "^4.3.0",
    "lodash": "^4.17.21"
  },
  "devDependencies": {
    "@nighttrax/eslint-config-tsx": "^6.3.0",
    "@rollup/plugin-commonjs": "^21.0.1",
    "@rollup/plugin-json": "^4.1.0",
    "@rollup/plugin-node-resolve": "^13.0.6",
    "@rollup/plugin-typescript": "^8.3.0",
    "@types/compression": "^1.7.0",
    "@types/express": "^4.17.11",
    "@types/jest": "^26.0.23",
    "@types/lodash": "^4.14.169",
    "@types/lru-cache": "^5.1.0",
    "@types/node": "^14.14.12",
    "cors": "^2.8.5",
    "nodemon": "^2.0.7",
    "rollup": "^2.60.0",
    "ts-node": "^9.1.1",
    "typescript": "^4.2.4"
  }
}

问题是 tsconfig.json 必须使用 "module": "esnext"。否则编译后的代码不兼容。