编译节点模块时出现打字稿错误
Posted
技术标签:
【中文标题】编译节点模块时出现打字稿错误【英文标题】:typescript error when compiling node module 【发布时间】:2018-04-20 14:09:52 【问题描述】:我下载了Typescript, Express, Sequelize, Postgres Example 并运行npm install
和npm start
。
在npm start
上会产生一些错误。
[11:27:11] Starting 'compile:typescript'...
~/node_modules/@types/geojson/index.d.ts(94,54): error TS1005: ',' expected.
~/node_modules/@types/geojson/index.d.ts(104,64): error TS1005: ',' expected.
~/node_modules/@types/handlebars/index.d.ts(22,31): error TS1005: ',' expected.
~/node_modules/@types/handlebars/index.d.ts(24,32): error TS1005: ',' expected.
~/node_modules/@types/handlebars/index.d.ts(99,40): error TS1005: ',' expected.
~/node_modules/@types/express-validator/index.d.ts(23,2): error TS2300: Duplicate identifier 'export='.
~/node_modules/@types/express-validator/index.d.ts(37,28): error TS2374: Duplicate string index signature.
~/node_modules/@types/geojson/index.d.ts(94,56): error TS2368: Type parameter name cannot be 'any'
~/node_modules/@types/geojson/index.d.ts(104,66): error TS2368: Type parameter name cannot be 'any'
~/node_modules/@types/geojson/index.d.ts(106,21): error TS2314: Generic type 'Feature<G, P, any>' requires 3 type argument(s).
~/node_modules/@types/handlebars/index.d.ts(22,33): error TS2368: Type parameter name cannot be 'any'
~/node_modules/@types/handlebars/index.d.ts(22,77): error TS2314: Generic type 'HandlebarsTemplateDelegate<T, any>' requires 2 type argument(s).
~/node_modules/@types/handlebars/index.d.ts(24,34): error TS2368: Type parameter name cannot be 'any'
~/node_modules/@types/handlebars/index.d.ts(24,79): error TS2314: Generic type 'HandlebarsTemplateDelegate<T, any>' requires 2 type argument(s).
~/node_modules/@types/handlebars/index.d.ts(96,15): error TS2314: Generic type 'HandlebarsTemplateDelegate<T, any>' requires 2 type argument(s).
~/node_modules/@types/handlebars/index.d.ts(99,42): error TS2368: Type parameter name cannot be 'any'
~/node_modules/@types/handlebars/index.d.ts(104,22): error TS2314: Generic type 'HandlebarsTemplateDelegate<T, any>' requires 2 type argument(s).
~/node_modules/@types/handlebars/index.d.ts(115,34): error TS2314: Generic type 'HandlebarsTemplateDelegate<T, any>' requires 2 type argument(s).
~/node_modules/express-validator/index.d.ts(24,3): error TS2300: Duplicate identifier 'export='.
src/server.ts(6,35): error TS2497: Module '"express-validator"' resolves to a non-module entity and cannot be imported using this construct.
[11:27:13] TypeScript: 5 syntax errors
[11:27:13] TypeScript: 15 semantic errors
package.json
"main": "build/src/server.js",
"scripts":
"build": "gulp build",
"doc": "gulp generate:doc",
"start": "cross-env NODE_ENV=development gulp watch",
"start:prod": "cross-env NODE_ENV=production gulp watch",
"run:test": "cross-env NODE_ENV=test gulp test",
"tslint": "gulp tslint",
"precommit": "lint-staged",
"prettify": "tsfmt -r --baseDir ./src",
"sqlz:migrate": "./node_modules/.bin/sequelize db:migrate",
"sqlz:undo": "./node_modules/.bin/sequelize db:migrate:undo",
"sqlz:new": "./node_modules/.bin/sequelize migration:create"
,
"lint-staged":
"src/**/*.ts": [
"prettify",
"git add"
]
,
"dependencies":
"body-parser": "~1.17.0",
"cors": "~2.8.1",
"cross-env": "~3.2.3",
"express": "~4.15.0",
"express-boom": "~2.0.0",
"express-validator": "^3.1.3",
"morgan": "~1.8.1",
"pg": "~6.1.2",
"pg-hstore": "~2.3.2",
"sequelize": "~3.30.2",
"uuid": "~3.0.1",
"winston": "~2.3.1"
,
"devDependencies":
"@types/body-parser": "~0.0.33",
"@types/chai": "~3.4.35",
"@types/express": "~4.0.35",
"@types/express-validator": "~2.20.33",
"@types/mocha": "~2.2.39",
"@types/morgan": "~1.7.32",
"@types/sequelize": "~3.4.40",
"@types/winston": "~2.2.0",
"chai": "~3.5.0",
"extendify": "~1.0.0",
"glob": "~7.1.1",
"gulp": "~3.9.1",
"gulp-istanbul": "~1.1.1",
"gulp-json-refs": "~0.1.1",
"gulp-mocha": "~3.0.1",
"gulp-nodemon": "~2.2.1",
"gulp-plumber": "~1.1.0",
"gulp-sourcemaps": "~2.4.0",
"gulp-tslint": "~7.0.1",
"gulp-typedoc": "~2.0.2",
"gulp-typescript": "~3.1.4",
"husky": "^0.13.2",
"lint-staged": "^3.4.0",
"remap-istanbul": "~0.8.4",
"rimraf": "~2.5.4",
"run-sequence": "~1.2.2",
"sequelize-cli": "^2.7.0",
"tslint": "~4.5.1",
"typedoc": "~0.5.7",
"typescript": "~2.2.1",
"typescript-formatter": "^5.1.1"
,
"engines":
"node": ">=4.0.0"
,
gulpfile.js(错误相关部分)
...
let gulp = require("gulp");
let sourcemaps = require("gulp-sourcemaps");
...
const COMPILE_TYPESCRIPT = "compile:typescript";
const JS_SRC_GLOB = "./build/**/*.js";
const TS_GLOB = [TS_SRC_GLOB];
const tsProject = typescript.createProject("tsconfig.json");
...
gulp.task(COMPILE_TYPESCRIPT, function()
return gulp.src(TS_GLOB)
.pipe(sourcemaps.init())
.pipe(tsProject())
.pipe(sourcemaps.write(".", sourceRoot: "../src" ))
.pipe(gulp.dest("build"));
);
tsconfig.json
"compilerOptions":
"target": "es6",
"module": "commonjs",
"declaration": false,
"noImplicitAny": false,
"removeComments": true,
"noLib": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true
,
"exclude": [
"node_modules"
]
我看到了这个This Post 来解决这个问题,但在 tsconfig.json 的 "files"
属性中包含什么没有意义。
我该如何解决这个错误?有没有人遇到过这种错误?
感谢您的任何帮助。
【问题讨论】:
【参考方案1】:试试这个。
"exclude": [
"node_modules/*"
]
【讨论】:
【参考方案2】:我已经删除了所有节点模块,并一一添加了运行npm start
时出错的地方。
这里是更新的节点模块。
"dependencies":
"body-parser": "^1.17.1",
"boom": "^7.1.1",
"cookie-parser": "^1.4.3",
"cors": "^2.8.4",
"cross-env": "^5.1.1",
"express": "^4.15.2",
"express-boom": "^2.0.0",
"express-validator": "^4.3.0",
"gulp": "^3.9.1",
"gulp-istanbul": "^1.1.2",
"gulp-mocha": "^4.3.1",
"gulp-nodemon": "^2.2.1",
"gulp-plumber": "^1.1.0",
"gulp-sourcemaps": "^2.6.1",
"gulp-tslint": "^8.1.2",
"gulp-typedoc": "^2.1.1",
"gulp-typescript": "^3.2.3",
"morgan": "^1.8.1",
"nodemon": "^1.12.1",
"pg": "^6.4.1",
"postgresql": "0.0.1",
"pug": "^2.0.0-beta11",
"reflect-metadata": "^0.1.10",
"remap-istanbul": "^0.9.5",
"rimraf": "^2.6.2",
"run-sequence": "^2.2.0",
"sequelize": "^3.21.0",
"sequelize-cli": "^3.0.0",
"sequelize-typescript": "^0.5.0",
"serve-favicon": "^2.4.1",
"tslint": "^5.8.0",
"typedoc": "^0.9.0",
"typescript": "^2.6.1",
"uuid": "^3.1.0",
"winston": "^2.4.0"
,
"devDependencies":
"@types/body-parser": "1.16.1",
"@types/cookie-parser": "^1.3.30",
"@types/ejs": "^2.3.33",
"@types/express": "^4.0.35",
"@types/morgan": "^1.7.32",
"@types/node": "^7.0.10",
"@types/serve-favicon": "^2.2.28"
【讨论】:
以上是关于编译节点模块时出现打字稿错误的主要内容,如果未能解决你的问题,请参考以下文章
使用带有 lit-html 的打字稿编写测试时出现“语法错误:无法在模块外使用 import 语句”
使用 React useContext 和 useReducer 时出现打字稿错误