1
votes

So after i run "node main.js" i'm getting errors in every d.ts file:

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;

project structure: enter image description here

i don't understand why i am getting typescript errors in runtime? looks like node trying to run those d.ts files or idk. How to fix that? ps: if i set declaration: false in tsconfig, error is gone, but i need d.ts files

1
That's not a typescript error, it looks like you are trying to run a typescript file instead of the compiled javascript.Jake Holzinger
i'm running "node main.js" in dist folderwealmostdone
You haven't posted the index.ts or index.js code, but the stack trace seems to indicate this error is happening when you are requiring the addTask module. See: /home/vdm/dev/reminder/dist/actions/index.js:11:24.Jake Holzinger
added Index.js and index.tswealmostdone
You must filter out the files with .ts extension. Using path.basename(file, 'js') does not do that for you.Jake Holzinger

1 Answers

2
votes

You need to filter out the .ts files. NodeJS does not know how to parse 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;