Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

535
Views
Typescript compiler can't skip imported js file

I've just started typescript with angular2 and I've run into the following problem. I started with the angular2 quickstart project and it worked fine, until I tried to import a plain javascript file. I am using the import statement as follows

import * as myModule from './mymodule.js';

Also I added the allowJs: true to the tsconfig.json file to accept the javascript file as an import. The following error is given by the compiler:

error TS5055: Cannot write file '.../mymodule.js' because it would overwrite input file.

As far as I understand the typescript compiler should not compile this file, because it is already in javascript, but for some reason I cannot exclude this from compilation.

I tried adding the file to the exclude: [] array in tsconfig.json, but because it is imported in one of my .ts files this is not taken into consideration (at least that's what I understood from the typescript docs).

My question is how can I compile my project with that file included? Is there a setting that I'm missing? Or something wrong with my approach?

Any help or advice is appreciated as this is starting to drive me crazy

Additional info

I'm using the following tsconfig file:

{
   "compilerOptions": {
      "target": "es5",
      "module": "commonjs",
      "moduleResolution": "node",
      "sourceMap": true,
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "lib": [ "es2015", "dom" ],
      "noImplicitAny": true,
      "suppressImplicitAnyIndexErrors": true
   }
}

I'm building my project running the

npm run build

from a console. Which is a script in the package.json file that looks like this:

"scripts": {
   "build": "tsc -p src/",
...

After the modifications suggested by @Seamus below, the same error happens.

But

if I just run in the console

tsc -p src/

The error I get is different:

error TS7016: Could not find a declaration file for module 'mymodule'

This looks like it find the module, if I do it manually, but then the question is, why does it not work with the npm run build? Is it doing something differently?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

The compiler option allowJs will:

Allow JavaScript files to be compiled.

For commonjs you will need a module declaration like this:

mymodule.d.ts

declare module "mymodule";

And in your source where you import:

/// <reference path="mymodule.d.ts"/>
import * as myModule from 'mymodule';

See "Working with Other JavaScript Libraries" in the documentation.

about 4 years ago · Santiago Trujillo Report

0

Okay, I know this is old, but I had this problem and I think I figured out a way around it (possibly partly due to an Angular update).

To start:

  1. Upgrade your Angular to the latest version (5.1).
  2. Upgrade your TypeScript to the latest supported version (4.*, as of the time of the writing -- not 5.*)
  3. You probably need allowJs: true in your config file.

Run ng serve --aot or ng build --watch=auto --aot You should see the above error and be unable to access your site.

Open one of the .ts file that's part of your app and make a small change (such as adding a single space) and then save the file.

Your app should compile!

(The --watch=auto and --aot flags are critical for this workaround, as far as I can tell)

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!