【发布时间】: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