Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

javascript - Error when importing library in Typescript for static website

I'm new to the Typescript world and I'm facing an issue. I'm trying to create a script to access firebase database functions, but when compiling the script I'm running into :

Uncaught ReferenceError: exports is not defined if I use "module": "commonjs" in my tsconfig.json

or

Uncaught SyntaxError: Cannot use import statement outside a module if I use "module": "es6"

In the second case, I can remove those three lines from the generated index.js file and everything is working properly :

import firebase from 'firebase/app';
import "firebase/database";
import "firebase/auth";

My question is, how can I use those external libraries in my typescript code, link them in my html without having those import in the compiled js file ?

Edit : Here's my ts file :

import firebase from 'firebase/app';
import "firebase/database"
import "firebase/auth"

class FbManager {
    private static instance: FbManager;
    
    firebaseConfig: object;

    private constructor() {
        this.firebaseConfig = {
            ...
          };
        
        firebase.initializeApp(this.firebaseConfig);

        firebase.auth().onAuthStateChanged(function(user) {
            console.log("AUTH STATE CHANGED");
            if (user) {      
              console.log("LOGGED IN USER " + user.uid);
              console.log(user);
            } else {
              console.log("SIGN OUT");
            }
          });
     }

    public static getInstance(): FbManager {
        if (!FbManager.instance) {
            FbManager.instance = new FbManager();
        }

        return FbManager.instance;
    }
}

Thank you in advance !


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Once you are in your repo type tsc --init after that TS config file will be generated.

Go to this file and uncomment outDir and after semicolon write e.g. "./build". Whole line will look like this "outDir": "./build"

Next thing you can do is to go to your package.json and in scripts write build: tsc. You'll be able to run npm command which will build your .ts files in build directory.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...