When I run tsc
in my project's directory it outputs an error (reproduced later)
当我在项目目录中运行tsc时,它会输出一个错误(稍后再复制)
This is the first time I'm trying typescript and nodejs. You can safely assume I'm a total beginner.
这是我第一次尝试打字稿和nodejs。你可以放心地认为我完全是个初学者。
- ubuntu 15.10 64bits
- ubuntu 15.10 64位
- npm version 2.4.12
- npm 2.4.12版本
- node version v4.3.1
- 节点版本v4.3.1
- tsc version 1.7.5
- 1.7.5 tsc版本
- system locale fr_FR.UTF-8
- 系统区域设置fr_FR.UTF-8
Compiler output :
编译器输出:
/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)
Project layout:
项目布局:
-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
Some of the things I've tried:
我试过的一些东西:
cd tsrest
npm init -y
mkdir server build client
(editing tsconfig.json and server/tsrest.ts from a tutorial)
(编辑tsconfig。json和服务器/临时避难所。ts教程)
tsc
Outputs the error
输出错误
sudo npm install -g typings
typings install
tsc
Well that didn't help either.
这也没用。
cd server/
tsc
cd ..
Now I thought my tsconfig.json was not good.
现在我想我的tsconfig。json是不好的。
rm tsconfig.json
tsc --init
ll
tsc
Same error ...
同样的错误……
Contents of tsconfig.json:
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
]
}
Contents of package.json:
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"
}
2 个解决方案
#1
2
I ran tsc with strace to see if it access tsconfig: (start omitted)
我使用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) = ?
Note that tsconfig.json is 375 bytes, so the read call reads all of it. So I figured that tsc doesn't parse the JSON file. I edited the file to see if there was anything funny. Only thing that standed out was the comments so I removed them and tried tsc again.
注意,tsconfig。json是375字节,所以read调用读取所有内容。所以我认为tsc不会解析JSON文件。我编辑了文件,看看是否有什么有趣的东西。只有一件事,那就是评论,所以我删除了他们,再次尝试tsc。
This time it worked.
这次工作。
I'll submit a ticket so that in case tsc can't parse the config file a better error is given.
我将提交一个票据,以便万一tsc不能解析配置文件时,给出一个更好的错误。
#2
1
Support for comments in tsconfig.json has been added recently and is on the milestone for TS 1.8. Here is the link to the issue in GitHub. I was able to reproduce this issue on Ubuntu with Typescript 1.7.5 but if you install TypeScript@next, which is 1.9 in development, comments are accepted with out issues. You would need to wait until TS 1.8 is released to be able to have comments in tsconfig.json.
支持tsconfig中的注释。json是最近添加的,是TS 1.8的里程碑。下面是GitHub上关于这个问题的链接。我用Typescript 1.7.5在Ubuntu上复制了这个问题,但是如果你安装了TypeScript@next,也就是1.9,那么注释就会被接受,不会有问题。您需要等到TS 1.8发布后才能在tsconfig.json中有注释。
#1
2
I ran tsc with strace to see if it access tsconfig: (start omitted)
我使用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) = ?
Note that tsconfig.json is 375 bytes, so the read call reads all of it. So I figured that tsc doesn't parse the JSON file. I edited the file to see if there was anything funny. Only thing that standed out was the comments so I removed them and tried tsc again.
注意,tsconfig。json是375字节,所以read调用读取所有内容。所以我认为tsc不会解析JSON文件。我编辑了文件,看看是否有什么有趣的东西。只有一件事,那就是评论,所以我删除了他们,再次尝试tsc。
This time it worked.
这次工作。
I'll submit a ticket so that in case tsc can't parse the config file a better error is given.
我将提交一个票据,以便万一tsc不能解析配置文件时,给出一个更好的错误。
#2
1
Support for comments in tsconfig.json has been added recently and is on the milestone for TS 1.8. Here is the link to the issue in GitHub. I was able to reproduce this issue on Ubuntu with Typescript 1.7.5 but if you install TypeScript@next, which is 1.9 in development, comments are accepted with out issues. You would need to wait until TS 1.8 is released to be able to have comments in tsconfig.json.
支持tsconfig中的注释。json是最近添加的,是TS 1.8的里程碑。下面是GitHub上关于这个问题的链接。我用Typescript 1.7.5在Ubuntu上复制了这个问题,但是如果你安装了TypeScript@next,也就是1.9,那么注释就会被接受,不会有问题。您需要等到TS 1.8发布后才能在tsconfig.json中有注释。