简体   繁体   English

使用 ts-node 转译 mocha 测试时,我找不到模块“tsconfig/register”

[英]when transpiling mocha test using ts-node i cannot find module 'tsconfig/register'

I have run both npm i ts-node and npm i ts-node --save-dev with no changes.我已经运行npm i ts-nodenpm i ts-node --save-dev没有任何变化。 ts-node works fine when I run it from the command line, but for some reason I cannot run mocha tests through test explorer.当我从命令行运行 ts-node 时,它​​工作正常,但由于某种原因,我无法通过测试资源管理器运行 mocha 测试。 I get the error below:我收到以下错误:

error:错误:

{ Error: Cannot find module 'tsconfig/register'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
    at Function.Module._load (internal/modules/cjs/loader.js:507:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at exports.requireOrImport (c:\Users\king\.vscode\extensions\hbenl.vscode-mocha-test-adapter-2.8.1\node_modules\mocha\lib\esm-utils.js:20:12)
    at c:\Users\king\.vscode\extensions\hbenl.vscode-mocha-test-adapter-2.8.1\out\worker\bundle.js:5663:41
    at Generator.next (<anonymous>)
    at fulfilled (c:\Users\king\.vscode\extensions\hbenl.vscode-mocha-test-adapter-2.8.1\out\worker\bundle.js:90:58) code: 'MODULE_NOT_FOUND' }

package.json包.json

{
  "name": "appts",
  "version": "1.1.1",
  "description": "",
  "main": "handler.js",
  "scripts": {
    "build:launchlocal": "cd serverless-site/ && npm start"
  },
  "mocha": {
  "require": ["tsconfig/register"]
},
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/aws-lambda": "^8.10.40",
    "@types/expect": "^24.3.0",
    "@types/mocha": "^8.0.3",
    "@types/validator": "^13.1.0",
    "serverless-offline": "^5.12.1",
    "serverless-plugin-typescript": "^1.1.9",
    "serverless-s3-sync": "^1.10.4",
    "serverless-step-functions": "^2.17.1",
    "ts-mocha": "^7.0.0",
    "ts-node": "^8.10.2"
  },
  "dependencies": {
    .....omitted....
  }
}

Turns out none of the package.json config options were working for me.结果没有一个 package.json 配置选项对我有用。 I wound up implementing a .mocharc.json on the same level as my package.json which worked perfectly.我最终实现了与我的 package.json 相同级别的 .mocharc.json ,它运行良好。

Example from this github repo (with more examples) copied here for posterity. 此 github 存储库中的示例(包含更多示例)复制到此处以供后代使用。

{
  "extension": ["ts"],
  "spec": "src/**/*.spec.ts",
  "require": "ts-node/register"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM