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

mdfafbf1  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(376)

我有一个angular 13项目,当我尝试为项目提供服务时,我得到了这个错误:
发生未处理的异常:不支持C:\Users\username\Documents\project\builds\Production2\tbsis\UI\node_modules\ansi-regex\index.js中ES模块C:\Users\username\Documents\project\builds\Production2\tbs\UI\node_modules\strip-ansi\index.js的require()。
以下是C:\Users\username\Documents\project\builds\Production 2\tbsis\UI\node_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\username\AppData\Local\Temp\ng-Z69dHd\angular-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"
  }

}

t1rydlwq

t1rydlwq1#

这个包在尝试导入Common JS源代码时出错了,所以你需要自己实现它。
首先使用import在ESM中导入ansi-regexstrip-ansi正在尝试导入和使用的包):

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

字符串
你可以这样使用它:

const regex = ansiRegex();
const stringWithoutAnsi = tartgetString.replace(regex, "");

相关问题