Compile TS to native JS modules
See original GitHub issueRxJS version: 5.4.3
Code to reproduce:
<script type="module">
import * as Rx from './node_modules/rxjs/Rx.js'
</script>
Expected behavior: There should be a way to load RxJS via native JS module
Actual behavior: Can’t access Rx when using JS modules (error: require
is not defined)
Issue Analytics
- State:
- Created 6 years ago
- Comments:32 (13 by maintainers)
Top Results From Across the Web
Documentation - Modules - TypeScript
How JavaScript Modules are Defined. In TypeScript, just as in ECMAScript 2015, any file containing a top-level import or export is considered a...
Read more >Compile Typescript to native browser ES modules
1 Answer 1 ... The only way to achieve this at the moment is to use .js in your TypeScript imports (even though...
Read more >How To Use Modules in TypeScript | DigitalOcean
Modules are a way to organize your code into smaller, more manageable pieces. TypeScript evolved along with ECMAScript specifications to ...
Read more >TypeScript Compiling with Visual Studio Code
TypeScript is a typed superset of JavaScript that transpiles to plain JavaScript. It offers classes, modules, and interfaces to help you build robust...
Read more >Compiling a TypeScript Module - TutorialsTeacher
Compilation of a module depends on the target environment you are aiming for. The TypeScript compiler generates the JavaScript code based on the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Let’s sum up all the ES module quirks again:
.js
extensions, because they are basically relative urls that the browser will use to download scripts.../node_modules/some-lib/dist/lib.js
instead of justsome-lib
).d.ts
files) should be in the same directory that the js files are."typings"
property ofpackage.json
is used only when importing a library using node syntax (using only the package name as the path, so it doesn’t work with ES modules)(3) is actually the default behavior of TS. (1) and (2) would need a post processing script if TS wont implement it.
Post processing script pseudo code:
Take a look at the file
@reactivex/rxjs/dist/esm2015/Observable.js
. All the imports there should have.js
extensions. Otherwise importing Observable into your app will fail with 404 error (e.g../util/root
should be./util/root.js
). I worked around that by setting up URL rewriting in apache/nginx.Example code:
The detected type of
Observable
isany
. It should work if the declarations from/dist/typings
were also copied to/dist/esm2015
.It seems that tsc will only look at
"typings"
inpackage.json
when importing a module using CJS/Node syntax (import "@reactivex/rxjs"
without mentioningnode_modules
or subpath)