TypeError [ERR_UNKNOWN_FILE_EXTENSION]:gulpfile.ts 的文件扩展名“.ts”未知

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

我运行时出错

yarn run package
。添加
NODE_OPTIONS="--loader ts-node/esm"
没有帮助。

我见过https://github.com/TypeStrong/ts-node/issues/1062,但我尝试了不同的

target
,但没有帮助。

package.json

...
"scripts": {
    "build": "tsc",
    "build:production": "npm run prestart && npm prune --production",
    "watch": "tsc --w",
    "prestart": "npm run build && func extensions install",
    "start:host": "func start",
    "start": "npm-run-all --parallel start:host watch",
    "test": "echo \"No tests yet...\"",
    "package": "gulp build && gulp package"
  },
  "description": "",
  "devDependencies": {
    "@azure/functions": "^1.0.1-beta1",
    "@types/gulp": "^4.0.9",
    "@types/gulp-zip": "^4.0.2",
    "@types/node": "^18.11.13",
    "del": "^7.0.0",
    "gulp-typescript": "^6.0.0-alpha.1",
    "gulp-zip": "^5.1.0",
    "npm-run-all": "^4.1.5",
    "ts-node": "^10.2.1",
    "typescript": "^3.3.3"
  },
  "dependencies": {
    "azure-devops-node-api": "^11.2.0",
    "gulp": "^4.0.2"
  },
  "resolutions": {
    "decode-uri-component": "0.2.2",
    "glob-parent": "6.0.2",
    "qs": "6.10.3",
    "string_decoder": "1.3.0"
  }

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "moduleResolution": "node",
    "target": "es6",
    "outDir": "dist",
    "rootDir": ".",
    "sourceMap": true,
    "noImplicitAny": true,
    "strict": false,
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "lib": [
      "es6"
    ]
  },
  "exclude": [
    "./gulpfile.ts"
  ],
  "ts-node": {
    "esm": true
  }
}

node
18.12.1、
npm
9.1.3 和
yarn
3.3.0.

typescript tsconfig ts-node yarnpkg-v2
1个回答
0
投票

这可能是一个迟到的答案,但几个小时后我可以修复它。

  1. 创建您的
    gulpfile.ts
  2. 创建第二个 gulpfile 并将其命名为
    gulpfile.cjs
    gulp-cli
    实际上应该首先使用
    cjs
    文件,但由于某种原因没有使用(查看此问题
  3. 因此,请向您的
    package.json
    添加一个脚本,告诉
    gulp-cli
    实际上使用
    gulpfile.cjs
    而不是
    gulpfile.ts
    :
{
  ...
  "scripts": {
    ...
    "gulp": "gulp -f gulpfile.cjs"
  }
}
  1. 安装
    ts-node
    https://www.npmjs.com/package/ts-node#installation
  2. 将此添加到您的
    gulpfile.cjs
// Workaround to load the gulpfile as a TypeScript file
require('ts-node').register({
  compilerOptions: {
    module: 'CommonJS',
  },
});

import('./gulpfile.ts');

现在应该可以了。我讨厌 JavaScript。

© www.soinside.com 2019 - 2024. All rights reserved.