question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Compile TS to native JS modules

See original GitHub issue

RxJS 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:closed
  • Created 6 years ago
  • Comments:32 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
phauxcommented, Oct 27, 2017

Let’s sum up all the ES module quirks again:

  1. Imports must include .js extensions, because they are basically relative urls that the browser will use to download scripts.
  2. Imports of other libraries must use relative path (../node_modules/some-lib/dist/lib.js instead of just some-lib)
  3. Typings (.d.ts files) should be in the same directory that the js files are. "typings" property of package.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:

for (every Import statement) {

    if (Import points to other package) {
        
        path = find_relative_path_to('node_modules') + path
        path += read('package.json')['main'] // read main property from module's package.json

    }
    
    if (path points to directory) { path += '/index.js' }
    else { path += '.js' }

}
1reaction
phauxcommented, Oct 26, 2017

about extension

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.

typings

Example code:

import {Observable} from '../node_modules/@reactivex/rxjs/dist/esm2015/Observable.js'

The detected type of Observable is any. It should work if the declarations from /dist/typings were also copied to /dist/esm2015.

It seems that tsc will only look at "typings" in package.json when importing a module using CJS/Node syntax (import "@reactivex/rxjs" without mentioning node_modules or subpath)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found