繁体   English   中英

tsc忽略了​​我的tsconfig.json文件

[英]tsc ignores my tsconfig.json file

当我在项目目录中运行tsc ,它会输出错误(稍后复制)

这是我第一次尝试打字稿和Node.js。 您可以放心,我是个初学者。

  • ubuntu 15.10 64位
  • npm版本2.4.12
  • 节点版本v4.3.1
  • tsc版本1.7.5
  • 系统区域设置fr_FR.UTF-8

编译器输出:

/data/code/tsrest$ tsc
/usr/lib/node_modules/typescript/lib/tsc.js:31084
            var jsonOptions = json["compilerOptions"];
                                  ^

TypeError: Cannot read property 'compilerOptions' of undefined
    at getCompilerOptions (/usr/lib/node_modules/typescript/lib/tsc.js:31084:35)
    at Object.parseJsonConfigFileContent (/usr/lib/node_modules/typescript/lib/tsc.js:31074:22)
    at parseConfigFile (/usr/lib/node_modules/typescript/lib/tsc.js:31351:40)
    at performCompilation (/usr/lib/node_modules/typescript/lib/tsc.js:31362:45)
    at Object.executeCommandLine (/usr/lib/node_modules/typescript/lib/tsc.js:31336:9)
    at Object.<anonymous> (/usr/lib/node_modules/typescript/lib/tsc.js:31635:4)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)

项目布局:

-rw-rw-r-- 1  220 févr. 20 17:15 package.json
-rw-rw-r-- 1  375 févr. 20 17:39 tsconfig.json
-rw-rw-r-- 1   70 févr. 20 17:23 typings.json

build:
total 8
drwxrwxr-x 2 4096 févr. 20 17:16 ./
drwxrwxr-x 6 4096 févr. 20 17:37 ../

client:
total 8
drwxrwxr-x 2 4096 févr. 20 17:16 ./
drwxrwxr-x 6 4096 févr. 20 17:37 ../

server:
total 12
drwxrwxr-x 2 4096 févr. 20 17:18 ./
drwxrwxr-x 6 4096 févr. 20 17:37 ../
-rw-rw-r-- 1  298 févr. 20 16:59 tsrest.ts

typings:
total 8
drwxrwxr-x 2 4096 févr. 20 17:23 ./
drwxrwxr-x 6 4096 févr. 20 17:37 ../
-rw-rw-r-- 1    0 févr. 20 17:23 browser.d.ts
-rw-rw-r-- 1    0 févr. 20 17:23 main.d.ts

我尝试过的一些方法:

cd tsrest
npm init -y
mkdir server build client

(从教程中编辑tsconfig.json和server / tsrest.ts)

tsc

输出错误

sudo npm install -g typings
typings install
tsc

嗯,那也没有帮助。

cd server/
tsc
cd ..

现在我以为我的tsconfig.json不好。

rm tsconfig.json 
tsc --init
ll
tsc

同样的错误...

tsconfig.json的内容:

{
    "version": "1.7.5",
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "noImplicitAny": false,
        "outDir": "built",
        "rootDir": ".",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true
        // "sourceMap": false
    },
    "exclude": [
        "node_modules",
        "client" // 4
    ]
}

package.json的内容:

{
  "name": "tsrest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

我用strace运行了tsc,看看它是否访问tsconfig :(开始省略)

1531  getcwd("/data/code/tsrest", 4096) = 18
1531  stat("tsconfig.json", {st_mode=S_IFREG|0664, st_size=375, ...}) = 0
1531  stat("tsconfig.json", {st_mode=S_IFREG|0664, st_size=375, ...}) = 0
1531  open("tsconfig.json", O_RDONLY|O_CLOEXEC) = 9
1531  fstat(9</data/code/tsrest/tsconfig.json>, {st_mode=S_IFREG|0664, st_size=375, ...}) = 0
1531  read(9</data/code/tsrest/tsconfig.json>, "{\n    \"version\": \"1.7.5\",\n    \"c"..., 375) = 375
1531  close(9</data/code/tsrest/tsconfig.json>) = 0
1531  mmap(0x320402100000, 1048576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x320402100000
1531  mmap(0x320403100000, 1048576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x320403100000
1531  munmap(0x7f35b90bd000, 1568768)   = 0
1531  write(2</dev/pts/1>, "/usr/lib/node_modules/typescript"..., 892) = 892
1531  exit_group(1)                     = ?

请注意,tsconfig.json为375字节,因此read调用将读取所有字节。 因此,我认为tsc不会解析JSON文件。 我编辑了文件,看是否有什么有趣的地方。 唯一引人注目的是评论,因此我删除了它们,然后再次尝试了tsc。

这次它起作用了。

我将提交票证,以防万一tsc无法解析配置文件,并给出了更好的错误。

tsconfig.json中对注释的支持最近已添加,并且已成为TS 1.8的里程碑。 是GitHub中问题的链接。 我可以使用Typescript 1.7.5在Ubuntu上重现此问题,但是如果您安装的TypeScript @ next(开发中的版本为1.9),则注释会被接受,不会出现任何问题。 您需要等到TS 1.8发布后才能在tsconfig.json中发表评论。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM