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.
Answer from Federico on Stack OverflowImporting commonJS modules with es6 imports
ES module can import but can't export from a common js module?
It is very difficult to call the `import` function from a CommonJS module.
node.js - Why won’t TypeScript let me import a type from an ES module into a CommonJS module? - Stack Overflow
I wrote this line in my code
export { Credentials } from 'aws-sdk/lib/credentials.js'; // "aws-sdk": "^2.955.0",It can be built without any problem, but at run time I got:
Exception during run: file:///x/src/index.js:1
export { Credentials } from 'aws-sdk/lib/credentials.js';
^^^^^^^^^^^
SyntaxError: Named export 'Credentials' not found. The requested module 'aws-sdk/lib/credentials.js' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from 'aws-sdk/lib/credentials.js';
const { Credentials } = pkg;But I have no problem using it like this:
import { Credentials } from 'aws-sdk/lib/credentials.js';Since this is an npm package, I have to export Credentials. What's the best practice?
Yes, you can use it in a same manner that you would use it in Javascript. Typescript is superset of Javascript, all things possible in Javascript are also possible in Typescript.
Refer to this:
https://www.typescriptlang.org/docs/handbook/modules.html
And search (ctrl/cmd + f) for 'require'
Yes you can, but in new projects. TS use ES Module System.