【问题标题】:Why i am getting typescript SyntaxError in every d.ts file?为什么我在每个 d.ts 文件中都收到 typescript SyntaxError?
【发布时间】:2020-10-12 20:38:19
【问题描述】:

所以在我运行“node main.js”之后,每个 d.ts 文件都会出现错误:

declare function addTask(db: any): (params: any) => void;
        ^^^^^^^^

SyntaxError: Unexpected token 'function'
    at Object.compileFunction (vm.js:344:18)
    at wrapSafe (internal/modules/cjs/loader.js:1106:15)
    at Module._compile (internal/modules/cjs/loader.js:1140:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1196:10)
    at Module.load (internal/modules/cjs/loader.js:1040:32)
    at Function.Module._load (internal/modules/cjs/loader.js:929:14)
    at Module.require (internal/modules/cjs/loader.js:1080:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at /home/vdm/dev/reminder/dist/actions/index.js:11:24
    at Array.forEach (<anonymous>)

addTask.ts:

function addTask(db: any) {
  return function (params: any): void {
    db.get('tasks').push(params).write();
  };
}

export = addTask;

addTask.d.ts:

declare function addTask(db: any): (params: any) => void;
export = addTask;

tsconfig.json:

{
  "compilerOptions": {
    "target": "ES6",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
    "declaration": true,                   /* Generates corresponding '.d.ts' file. */
    "outDir": "dist",                        /* Redirect output structure to the directory. */
    "rootDir": "src",                       /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    "strict": true,                           /* Enable all strict type-checking options. */
    "esModuleInterop": true,                  /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    "skipLibCheck": true,                     /* Skip type checking of declaration files. */
    "forceConsistentCasingInFileNames": true  /* Disallow inconsistently-cased references to the same file. */
  }
}

index.ts:

const fs = require('fs');
const path = require('path');

const loader = (db: any) => {
  const modules: {[index: string]:any} = {};
  const files = fs.readdirSync(__dirname);
  files.forEach((file: any) => {
    const modulePath = path.join(__dirname, file);
    const name = path.basename(file, '.js');
    if (name !== 'index') {
      const fn = require(modulePath)(db);
      modules[name] = fn;
    }
  });
  
  return modules;
};

export = loader;

index.js:

"use strict";
const fs = require('fs');
const path = require('path');
const loader = (db) => {
    const modules = {};
    const files = fs.readdirSync(__dirname);
    files.forEach((file) => {
        const modulePath = path.join(__dirname, file);
        const name = path.basename(file, '.js');
        if (name !== 'index') {
            const fn = require(modulePath)(db);
            modules[name] = fn;
        }
    });
    return modules;
};
module.exports = loader;

项目结构:

我不明白为什么我在运行时遇到打字稿错误?看起来节点试图运行那些 d.ts 文件或 idk。 如何解决? ps:如果我在tsconfig中设置声明:false,错误就消失了,但我需要d.ts文件

【问题讨论】:

  • 这不是打字稿错误,看起来您正在尝试运行打字稿文件而不​​是编译后的 javascript。
  • 我在 dist 文件夹中运行“node main.js”
  • 您尚未发布index.ts 或index.js 代码,但堆栈跟踪似乎表明当您需要addTask 模块时发生此错误。请参阅:/home/vdm/dev/reminder/dist/actions/index.js:11:24。
  • 添加了 Index.js 和 index.ts
  • 您必须过滤掉带有.ts 扩展名的文件。使用 path.basename(file, 'js') 对你没有好处。

标签: javascript node.js typescript


【解决方案1】:

您需要过滤掉.ts 文件。 NodeJS 不知道如何解析 TypeScript。

const fs = require('fs');
const path = require('path');

const loader = (db: any) => {
  const modules: {[index: string]:any} = {};
  const files = fs.readdirSync(__dirname);
  files.forEach((file: any) => {
    if (file.endsWith('.js')) {
      const modulePath = path.join(__dirname, file);
      const name = path.basename(file, '.js');
      if (name !== 'index') {
        const fn = require(modulePath)(db);
        modules[name] = fn;
      }
    }
  });
  
  return modules;
};

export = loader;

【讨论】:

  • 是的,当我查看 loader =) 时,我自己发现了这一点,无论如何都标记为答案,因为有时当您尝试向其他人解释问题时,它可能会帮助您了解导致问题的原因
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-27
  • 1970-01-01
  • 2020-02-16
相关资源
最近更新 更多