Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Typescript编译的js文件在未定义的默认属性上引发异常_Node.js_Typescript - Fatal编程技术网

Node.js Typescript编译的js文件在未定义的默认属性上引发异常

Node.js Typescript编译的js文件在未定义的默认属性上引发异常,node.js,typescript,Node.js,Typescript,无法读取未定义的属性“json” 反对。C:\web\learnTypescript\dist\index.js:7:40 dist/index.js:7 app_1.default.use(body_parser_1.default.json()); 我创建了一个简单的项目来学习打字脚本。我已经安装了依赖项和它们的@types对应项,但当我尝试启动node-node-dist/index.js时,仍然会出现上述错误 tsconfig.json { "compilerOpti

无法读取未定义的属性“json”

反对。C:\web\learnTypescript\dist\index.js:7:40

dist/index.js:7

app_1.default.use(body_parser_1.default.json());
我创建了一个简单的项目来学习打字脚本。我已经安装了依赖项和它们的@types对应项,但当我尝试启动node-node-dist/index.js时,仍然会出现上述错误

tsconfig.json

{
    "compilerOptions": {
        "allowSyntheticDefaultImports": true,
        "moduleResolution": "node",
        "module": "commonjs",
        "target": "es6",
        "outDir": "dist"
    },
    "include": [ "src/**/*" ],
    "exclude": [ "node_modules" ]
}
src/index.ts

import app from './app'
import bodyParser from 'body-parser'
/* more imports */

app.use(bodyParser.json());
/* more code follows */
dist/index.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const app_1 = require("./app");
const body_parser_1 = require("body-parser");
/* more requires */
app_1.default.use(body_parser_1.default.json());
/* more code follows */

如果您将导入更改为

import * as bodyParser from 'body-parser';
那就应该解决了

*因为语法被称为名称空间导入,它有效地创建了一个包含文件中所有内容的变量

根据从另一个文件导出的内容,有几种方法可以从文件导入

文件1.ts

export default class Foo {
}

export class Bar {
}

const a = 'Hello';

export { a as A }
文件2.ts

import A from 'file1'; // A is now the default export from file1
import * as File from 'file1'; // File is now a constant with Foo, Bar and A
import { Bar } from 'file1'; // Importing only Bar from file1

正如James也提到的,您也可以使用代码;从“主体解析器”导入{json}

如果将其从“body parser”更改为“作为body parser导入*”;会怎么样;?成功了。请加上这个作为答案,我会给你评分的。我有一些关于为什么需要以这种方式导入的问题,但我会将其保存下来,以供您的回答用于文档目的。谢谢。似乎没有必要,树是否应该抖动并从“主体解析器”导入{json}