웹사이트 검색

node.js - node_option --loader ts-node/esm를 활성화하면 예외 스택 트레이스에서 잘못된 줄 번호가 표시됩니다


노드 옵션 --loader ts-node/esm를 활성화하면 예외 스택 트레이스에서 잘못된 줄 번호가 표시됩니다

문제를 재현하는 최소한의 GitHub 저장소는 여기 있습니다 https://github.com/deltaboukensha/bug-ts-loader

npm run test 스크립트에서 ts-mocha --loader ts-node/esm를 활성화했습니다

스택트레이스가 이제 잘못된 줄을 보여줍니다

옵션을 비활성화하면 선이 올바른 위치를 가리킵니다

이것은 제가 TypeScript를 사용하는 장점을 설명할 다른 회사 프로젝트를 위한 것입니다. 나는 정말로 이러한 신비로운 문제들을 해결하고 적어도 그것들을 설명하고 싶습니다.

테스트.ts

import chai, { expect } from 'chai';

it('throws error but shows the wrong line', async () => {
  const a = 2;
  const b = 4;
  
  const c = a+ b;
  expect(c).to.be.eq(7);
})

// 1) throws error but shows the wrong line:

//       AssertionError: expected 6 to equal 7
//       + expected - actual

//       -6
//       +7
      
//       at /home/delta/dev/bug-ts-mocha/test.ts:17:33 <--- should be line 9
//       at Generator.next (<anonymous>)
//       at /home/delta/dev/bug-ts-mocha/test.ts:8:71
//       at new Promise (<anonymous>)
//       at __awaiter (test.ts:4:12)
//       at Context.<anonymous> (test.ts:13:51)
//       at processImmediate (node:internal/timers:466:21)

package.json

{
  "name": "bug-ts-loader",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "ts-mocha test.ts --loader=ts-node/esm"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/chai": "^4.3.4",
    "@types/mocha": "^10.0.1",
    "chai": "^4.3.7",
    "ts-mocha": "^10.0.0",
    "ts-node": "^10.9.1",
    "typescript": "^4.9.5"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "ES6",
    "module": "CommonJS",
    "moduleResolution": "node",
    "paths": {
      "*": ["./src/*", "./@types/*", "./test/*"]
    },
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": false,
    "noImplicitAny": false,
    "typeRoots": [
      "@types/*",
      "node_modules/@types/*"
    ],
    "types": ["node", "mocha"]
  },
  "exclude": ["dist"]
}

도와주셔서 감사합니다.

나는 스택 트레이스의 줄 번호가 파일의 올바른 줄을 가리키기를 기대한다

답변 #1

다음과 같이 node에 --enable-source-maps를 추가해 보세요:

node --loader=ts-node/esm --enable-source-maps test.ts

토론을 위해 이 ts-node 문제를 참조하세요.