节点 Lambda:ts-node 的导入导致 TypeError

Posted

技术标签:

【中文标题】节点 Lambda:ts-node 的导入导致 TypeError【英文标题】:Node Lambda: Import of ts-node results in TypeError 【发布时间】:2017-02-20 11:44:40 【问题描述】:

我是节点新手并编写服务器端代码,所以请多多包涵。 目前,我正在使用 Typescript 编写一个节点 lambda。为了在我的项目中使用它,我需要 typescript 模块作为依赖项,所以我将它们列在我的package.json 文件中,并且它们与npm install 一起安装。接下来我运行 npm test 并收到此错误日志。

LT-2012011001:example-data-sync amohnacs$ npm test

> example-data-sync@1.0.0 test /Users/amohnacs/Developer/git/example-data-sync
> nyc ./node_modules/.bin/jasmine-ts ./spec/**/*.spec.ts


/Users/amohnacs/Developer/git/example-data-sync/node_modules/ts-node/src/index.ts:313
      throw new TypeError(
            ^
TypeError: Unable to require `.d.ts` file.
This is usually the result of a faulty configuration or import. Make sure there is a `.js`, `.json` or another executable extension and loader (attached before `ts-node`) available alongside     `/Users/amohnacs/Developer/git/example-data-sync/node_modules/any-promise/implementation.d.ts`.
at getOutput (/Users/amohnacs/Developer/git/example-data-sync/node_modules/ts-node/src/index.ts:313:17)
at /Users/amohnacs/Developer/git/example-data-sync/node_modules/ts-node/src/index.ts:334:18
at Object.compile (/Users/amohnacs/Developer/git/example-data-sync/node_modules/ts-node/src/index.ts:462:19)
at Object.m._compile (/Users/amohnacs/Developer/git/example-data-sync/node_modules/ts-node/src/index.ts:392:44)
at Module._extensions..js (module.js:550:10)
at Object.require.extensions.(anonymous function) [as .ts] (/Users/amohnacs/Developer/git/example-data-sync/node_modules/ts-node/src/index.ts:395:12)
at NYC._readTranspiledSource (/Users/amohnacs/Developer/git/example-data-sync/node_modules/nyc/index.js:150:26)
at NYC.addFile (/Users/amohnacs/Developer/git/example-data-sync/node_modules/nyc/index.js:134:21)
at /Users/amohnacs/Developer/git/example-data-sync/node_modules/nyc/index.js:166:11
at /Users/amohnacs/Developer/git/example-data-sync/node_modules/nyc/index.js:231:5
npm ERR! Test failed.  See above for more details.

这是我的package.json


  "name": "example-data-sync",
  "version": "1.0.0",
  "description": "An example project using the serverless framework for a lambda that subscribes to kinesis events.",
  "main": "index.js",
  "scripts": 
    "postinstall": "./node_modules/.bin/tsd install && ./node_modules/.bin/tsd link",
    "sls:init": "./node_modules/.bin/sls project init -c",
    "test": "./node_modules/.bin/nyc ./node_modules/.bin/jasmine-ts ./spec/**/*.spec.ts",
    "dp:dev:meta": "sls meta sync -s dev -r us-east-1",
    "dp:dev:cf": "sls resources deploy -s dev -r us-east-1"
  ,
  "repository": 
    "type": "git",
    "url": "git+https://gitlab.com/danteinc-sandbox/example-data-sync.git"
  ,
  "keywords": [
    "kinesis",
    "event",
    "stream"
  ],
  "author": "John Gilbert <john.gilbert@danteinc.com> (danteinc.com)",
  "private": true,
  "license": "SEE LICENSE IN LICENSE.md",
  "bugs": 
    "url": "https://gitlab.com/danteinc-sandbox/example-data-sync/issues"
  ,
  "homepage": "https://gitlab.com/danteinc-sandbox/example-data-sync#README",
  "devDependencies": 
    "aws-sdk": "^2.6.1",
    "jasmine-console-reporter": "^1.2.7",
    "jasmine-ts": "0.0.3",
    "nyc": "^8.1.0",
    "serverless": "^0.5.0",
    "serverless-meta-sync": "^0.1.0",
    "ts-node": "^1.3.0",
    "tsd": "^0.6.5",
    "typescript": "^1.8.10"
  ,
  "dependencies": 
    "bluebird": "^3.3.5",
    "dante-commons-lambda": "^0.X.0",
    "debug": "^2.2.0",
    "highland": "^2.5.1",
    "lodash": "^3.10.1",
    "moment": "^2.12.0"
  ,
  "nyc": 
    "lines": 95,
    "statements": 95,
    "functions": 95,
    "branches": 95,
    "include": [
      "src/**/*.ts"
    ],
    "exclude": [
      "spec/**/*.spec.ts",
      "typings"
    ],
    "extension": [
      ".ts"
    ],
    "require": [
      "ts-node/register"
    ],
    "reporter": [
      "json",
      "html",
      "text-summary"
    ],
    "cache": false,
    "check-coverage": true,
    "all": true
  

它告诉我我的项目配置不正确,但按照它看起来的路径在ts-node 中出现了问题。我在项目配置中是否有遗漏或不正确的内容,或者我可能使用了不正确版本的 ts-node 或 Typescript。

提前谢谢你。

【问题讨论】:

【参考方案1】:

package.jsonnyc.exclude 部分添加 node_modules

"nyc": 
  "exclude": [
    "spec/**/*.spec.ts",
    "typings",
    "node_modules"
  ],

【讨论】:

【参考方案2】:

对于我的情况,下面解决了这个问题,但不排除 .ts 文件:

我安装了ts-jest,见说明here 然后,我在jest.config.js 中添加了
globals: 
  'ts-jest': 
    isolatedModules: true
  ,
,

根据this fix

【讨论】:

以上是关于节点 Lambda:ts-node 的导入导致 TypeError的主要内容,如果未能解决你的问题,请参考以下文章

运行时未定义 ts-node 导入

使用ts-node执行时使用打字稿导入nodejs`fs`?

ts-node 执行带有模块导入和模块定义的打字稿

无法再获取节点或 ts-node/register 以使用 es2020(flatMap 不是函数)

如何在 Azure 应用服务上运行使用 ts-node 开发的节点应用程序

如何将 ts-node ESM 与节点模块一起使用?