Monday, February 11, 2019

error TS5014: Failed to parse file

Working on rebuilding my js files from Typescript src files, I went to run tsc and found this issue.

$ tsc -p tsconfig.json

error TS5014: Failed to parse file 'tsconfig.json/tsconfig.json': Unexpected token u in JSON at position 0.

Turns out this was due to not having typescript installed properly on WSL (where I was running the compiler command).

$ npm i -g typescript
$ tsc -p tsconfig.json

By first installing typescript, I was able to properly compile my code.

Initially, I had thought this might be related to using WSL in conjunction with Windows to develop. However, this was not the case. You can read my previous post to see issues related to WSL and Windows cross development.

Nodejs Development - WSL and Windows: Unknown error lstat, errno 4094

I really like Linux and MacOS over Windows. The commands and syntax feel more comfortable, the tooling more reliable (IMO), probably because I've managed Linux servers for some time. So, you can imagine how excited I was to see WSL or Windows Subsystem Linux.

Switching between WSL Ubuntu terminal and Windows command line, I've found some various issues. More recently, however, I came across this one while trying to run a simple script:

{ Error: UNKNOWN: unknown error, lstat 'C:\project_path\node_modules\.bin\eslint'
  errno: -4094,
  code: 'UNKNOWN',
  syscall: 'lstat',
  path: 'C:\\project_path\\node_modules\\.bin\\eslint' }

In my case, the problem has to do with how npm is connecting dependency scripts to my project. In WSL, npm uses symbolic links to create an alias to something located somewhere else; in our case the scripts for these programs are actually nested deeper in the node_modules directory. To allow our program to use them easily, npm creates aliases in the node_modules/.bin directory. However, in Windows, npm creates a bunch of cmd files, which more or less do the same thing, by running the script where it actually resides; deeper in the node_modules/ directory.

To figure this out, I decided to let the operating systems each tell me what it saw in the .bin/ directory (I had run npm install on WSL prior):

On WSL (using ls -l)
eslint -> ../eslint/bin/eslint.js

On Windows (using attrib)
The target of the symbolic link C:\project_path\node_modules\.bin\eslint does not exist

If you want to develop node applications, then you either need to use npm on WSL or Windows, but not both. As my IDE is a Windows application and has built in script execution, I guess I will need to do all my npm management on Windows.

To fix this issue, the easiest/clean solution is to completely remove node_modules/ (verify it is removed on both Windows and WSL!) and then on your chosen operating system (Windows for me), run your npm install command.