【问题标题】:tsc ignores my tsconfig.json filetsc 忽略我的 tsconfig.json 文件
【发布时间】:2016-06-02 06:31:08
【问题描述】:

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

这是我第一次尝试 typescript 和 nodejs。你可以放心地假设我是个初学者。

  • 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"
}

【问题讨论】:

  • tsconfig.json 是什么样的?
  • 用 tsconfig.json 和 package.json 内容编辑了问题
  • 是您运行 tsc 命令的路径 /data/code/tsrest,是您项目的根文件夹吗?也只是为了确认一下,tsconfig.json 位于您项目的根文件夹中,对吗?我刚刚在 ubuntu 15 64 位上进行了打字稿测试,一切运行良好......我的配置 [node - 5.6.0, npm 3.6.0, tsc @next]
  • 是的 /data/code/tsrest 是我项目的根文件夹,而 tsconfig.json 在我的项目的根文件夹中。

标签: node.js typescript


【解决方案1】:

我用 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 文件。我编辑了文件,看看是否有什么有趣的地方。唯一突出的是 cmets,所以我删除了它们并再次尝试 tsc。

这次成功了。

我将提交一张票,以便在 tsc 无法解析配置文件时给出更好的错误。

【讨论】:

    【解决方案2】:

    最近在 tsconfig.json 中添加了对 cme​​ts 的支持,这是 TS 1.8 的里程碑。 Here 是 GitHub 中问题的链接。我能够在带有 Typescript 1.7.5 的 Ubuntu 上重现此问题,但如果您安装 TypeScript@next(开发中的 1.9),则可以毫无问题地接受 cmets。您需要等到 TS 1.8 发布才能在 tsconfig.json 中包含 cmets。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-17
      • 2019-03-27
      • 1970-01-01
      • 2019-01-09
      • 2017-02-13
      • 2017-04-18
      • 2018-06-20
      • 2018-05-31
      相关资源
      最近更新 更多