Strip-Ansi 使用 require() 而不是 import,生成异常

问题描述 投票:0回答:1

我有一个 Angular 13 项目,当我尝试为该项目提供服务时,出现此错误:

发生未处理的异常:ES模块的require() C:\Users\用户名\Documents\project uilds\Production2 bsis\UI ode_modules nsi-regex\index.js 从 C:\Users\用户名\Documents\project uilds\Production2 bsis\UI ode_modules\strip-ansi\index.js 不支持。

这是C:\Users\用户名\Documents\project uilds\Production2 bsis\UI的内容 ode_modules\strip-ansi\index.js:

'use strict';
const ansiRegex = require('ansi-regex');

module.exports = string => typeof string === 'string' ? string.replace(ansiRegex(), '') : string;

看来这个 require 语句导致了错误。

我该如何解决这个问题?

我尝试将其更改为

import ansiRegex from 'ansi-regex';

但是在尝试服务时出现了这个错误

发生未处理的异常:无法在外部使用 import 语句 模块见 “C:\Users\用户名\AppData\Local\Temp g-Z69dHd ngular-errors.log”为 更多详情。

这是我的package.json

{
  "name": "client",
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "startl": "ng serve --base-href \"\\\"",
    "build": "ng build ",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "postinstall": "ngcc",
  "private": true,
  "dependencies": {
    "@angular-devkit/schematics": "^13.1.4",
    "@angular/animations": "^13.1.2",
    "@angular/cli": "^13.1.2",
    "@angular/common": "^13.1.2",
    "@angular/compiler": "^13.1.2",
    "@angular/core": "^13.1.2",
    "@angular/forms": "^13.1.2",
    "@angular/platform-browser": "^13.1.2",
    "@angular/platform-browser-dynamic": "^13.1.2",
    "@angular/router": "^13.1.2",
    "@fortawesome/fontawesome-free": "^5.3.1",
    "ag-grid-angular": "^22.1.1",
    "ag-grid-community": "^22.1.1",
    "bootstrap": "^5.1.3",
    "core-js": "^2.5.4",
    "jquery": "^3.3.1",
    "ngx-bootstrap": "8.0.0",
    "node-gyp": "^7.1.2",
    "npm": "^8.3.1",
    "rxjs": "7.4.0",
    "tslib": "^2.3.1",
    "zone.js": "0.11.4"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^13.1.3",
    "@angular/compiler-cli": "^13.1.2",
    "@angular/language-service": "^13.1.2",
    "@types/jasmine": "3.9.1",
    "@types/jasminewd2": "2.0.10",
    "@types/node": "12.12.6",
    "codelyzer": "^6.0.1",
    "grunt": "^1.4.1",
    "grunt-cli": "^1.4.3",
    "grunt-war": "^0.5.1",
    "jasmine-core": "^3.9.0",
    "jasmine-spec-reporter": "^7.0.0",
    "karma": "^6.3.4",
    "karma-chrome-launcher": "^3.1.0",
    "karma-coverage-istanbul-reporter": "^3.0.3",
    "karma-jasmine": "^4.0.1",
    "karma-jasmine-html-reporter": "^1.7.0",
    "protractor": "^7.0.0",
    "ts-node": "^5.0.1",
    "tslint": "^6.1.0",
    "typescript": "^4.4.4"
  },
  "overrides": {
    "postcss": "8.2.13",
    "ansi-regex": "6.0.1",
    "node-forge": "1.3.1",
    "qs" : "6.10.3",
    "@angular/core" : "^13.1.2"
  }

}
angular npm node-modules
1个回答
0
投票

该包在尝试导入

Common JS
源时出错,因此您需要自己实现它。

首先使用

ansi-regex
在 ESM 中导入
strip-ansi
(包
import
正在尝试在后台导入和使用):

const ansiRegex = (await import("ansi-regex")).default;

那么你可以这样使用它:

const regex = ansiRegex();
const stringWithoutAnsi = tartgetString.replace(regex, "");
© www.soinside.com 2019 - 2024. All rights reserved.