Can Typescript import CommonJS Modules?

Federico

I had this file:

//foo.js
var foo = function () {
    return "foo";
}; 

module.exports = foo;

So, I wanted to import it to my Typescript file. I tried this

//typescript.ts
import * as foo from ("./foo");

Didn't work. I read about this 'ambient' modules, so I added this

//typescript.ts
/// <reference path="./foo.d.ts" />
import * as foo from ("./foo");

And I added a "foo.d.ts" file in the same folder, which had the purpose of letting typescript know about the types of my imported function:

declare module "foo" 
{
    function foo(): string
    export = foo;
}

No luck.

I thought that the problem was with the import syntax (you cannot have the same syntax for es6 and commonjs modules, right?). So I did this.

import foo = require("./foo");

As you might guess, that didn't work either.

I have been able to import d3 an use it successfully when I installed it as a node module with npm install d3 and referenced its d.ts file. I did it with this code:

import * as d3 from "d3";

I haven't been able to do the same with any other module (jquery, box2d, box2d-commonjs, among others), nor with my own libraries as demonstrated above. I am new to Typescript, so probably I'm missing something very obvious, but I haven't been able to figure it out by myself.

Federico

Turns out it was something really obvious: you had to use the --allowJs option. This worked for me:

tsc --moduleResolution "node" --module "commonjs"  --allowJs main.ts

Though, I still can't figure out why d3 worked while the others libraries didn't.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Isomorphic import of CommonJS modules

Can't import npm modules in commonjs with rollup : "require is not defined"

How can Include both commonJs require syntax and es modules import syntax in the same bundle - webpack

Import 'global' modules with TypeScript

How can I import CSS Modules in a TypeScript file?

How can typescript import external modules in a web worker

Can I dynamically import TypeScript modules in Node.js/Express?

Can't import TypeScript modules without providing the file extension

Import commonJS, AMD, and ES6 modules at runtime with synchronous syntax

es6 modules to commonjs with typescript under node harmony flag

Mix Node's CommonJS and Typescript's ES modules

Can't import modules

Typescript: Import from nested modules

Import Typescript modules in Svelte Component

Import Angular JavaScript Modules into TypeScript

Import node standard modules into TypeScript

How to import CommonJS module that uses module.exports= in Typescript

Can I define common dependencies in a file, and import that in commonJS?

Can no longer Import Any Modules

Can't import functions/modules

Backbone Views as CommonJS modules

Converting CommonJS to ES modules

I can't import node_modules from typescript in node.js

How can I import TypeScript AMD modules without making the dependent code a module

How to import js-modules into TypeScript file?

Import modules dynamically during Webpack compilation with Typescript

How to import all modules from a directory in TypeScript?

unable to import modules in node type using typescript

How to Properly Export and Import Modules in TypeScript