node.js - tsc 忽略我的 tsconfig.json 文件

标签 node.js typescript

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

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

  • ubuntu 15.10 64位
  • npm 版本 2.4.12
  • Node 版本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 无法解析配置文件的情况下给出更好的错误。

关于node.js - tsc 忽略我的 tsconfig.json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35526404/

相关文章:

node.js - 我应该在整个服务器运行时保留 Sequelize 实例吗?

reactjs - 将 Typescript 接口(interface)属性传递给子组件返回不存在的属性

dojo - 当模块名称冲突时如何从 typescript.d.ts 文件访问接口(interface)?

javascript - Jasmine 规范超时。重置 WebDriver 控制流

javascript - typescript 多次进口

javascript - 将 Mongoose 返回的数组转换为常规 JSON

javascript - Discord.js 向特定 channel 发送消息

javascript - Node.js:http 请求在 1 分钟后超时

jquery - 带有 require.js 和 jquery 的 TypeScript 在生产服务器上不起作用(require.js 脚本错误)

javascript - Express js API 中的端点 URI