Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File '/Path/hello.js' is a JavaScript file. Did you mean to enable the 'allowJs' option

Tags:

typescript

I created a new directory in which I created 2 files

hello.ts

function sayHello(name: String) : String {
  return "Hello " + name;
}
console.log(sayHello("Foo"))

tsconfig.json

{
  "compilerOptions": {
    "outDir": "out",
    "module": "commonjs",
    "target": "es6",
    "moduleResolution": "node",
    "allowJs": false,
    "alwaysStrict": true,
    "noImplicitAny": true,
    "removeComments": true,
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  },
  "exclude": [
    "node_modules", 
    "**/*.spec.ts", 
    "out/**"
  ],
  "include": ["**/*.ts"],
  "files": [
    "hello.js"
  ]
}

Now when I run tsc in my directory. it gives me an error

error TS6504: File '/Users/Foo/code/typescript/hello.js' is a 
JavaScript file. Did you mean to enable the 'allowJs' option?

Why? I told the compiler to put the output into the "out" directory and ignore anything js files in the out directory (not try to compile them after generating them). Then why is it throwing this error at me?

like image 840
Knows Not Much Avatar asked Sep 07 '25 18:09

Knows Not Much


1 Answers

files is a list of files to include (TypeScript documentation). It doesn't matter if the file doesn't exist - TypeScript sees that you want to compile it; but wait! That's not a TypeScript file. In this case, you don't even need files, because you have included hello.ts using a pattern in include. However, if you wanted to include a file that matched one of the the patterns in exclude, you should use files. If you want to keep files, just change hello.js to hello.ts, because that is the name of the file you want to compile:

{
  "compilerOptions": {
    "outDir": "out",
    "module": "commonjs",
    "target": "es6",
    "moduleResolution": "node",
    "allowJs": false,
    "alwaysStrict": true,
    "noImplicitAny": true,
    "removeComments": true,
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  },
  "exclude": [
    "node_modules", 
    "**/*.spec.ts", 
    "out/**"
  ],
  "include": ["**/*.ts"],
  "files": [
    "hello.ts"
  ]
}
like image 121
pigrammer Avatar answered Sep 10 '25 07:09

pigrammer