Importing DocumentReference in Es6 - node.js

Classes like DocumentReference, CollectionReference, etc., don't seem to be exported. Is there a way to import these classes?

Install firebase if it's not in your package.json:
npm install --save firebase
In the .ts file just import whatever you need:
import * as firebase from 'firebase/app'
import DocumentReference = firebase.firestore.DocumentReference
Use DocumentReference:
posts.forEach((doc: DocumentReference) => {
doc.ref.delete()
})

using typescript, this works:
import * as firebase from "firebase"
then do
firebase.firestore().DocumentReference

Related

ts-node can't use dynamic imports or import statements while in repl or eval in the cli

I have been trying to run eval or import esm module from the repl and I just can't make it work. I have tried a few things I found on the internet.
ex: Is it possible to import a Typescript into a running instance of ts-node REPL?
Not sure I am missing something, errors I am getting are:
from REPL with dynamic imports:
var a = await import("./test").then(x=>x)
//or
var a = await import("./test").then(x=>x)
//gives
//TypeError [ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING]: A dynamic import callback was not specified.
using import statements on REPL
import * as test from './src/test';
//works but then when I try to use 'test' it gives
//SyntaxError: Cannot use import statement inside the Node.js REPL, alternatively use dynamic import
using on the cli
ts-node --esm -e "import * as z from './src/test'; console.log(z); export {}"
//gives
//Cannot use import statement outside a module
ts-node --esm -e "const z= import('./src/test').then(console.log);"
//gives
//TypeError [ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING]: A dynamic import callback was not specified.
Any ideas what is going on?
I am using ts-node 10.9.1 globally installed in node 18.2.0

My Vue NPM imports can only find the modules if they are inside the src folder and not project root

In my app root, I have /node_modules/ and /src/
I have ran npm install and installed packages using npm install axios --save etc for all my dependencies
I do see that they are being added to the node_modules directory however my vue project does not compile with the following error:
Failed to compile.
./src/main.js
Module not found: Error: Can't resolve 'axios' in '/app/src'
If I manually create a node_modules folder inside of my /src/ directory, and place axios, and my other dependencies it works.. but from what I've read I should not be handling my node modules this way. It seems I need to have my dependencies point to a different path?
What's weird is my import Vue from 'vue' and App from './App.vue' work fine, it's basically everything afterwards that fails to compile..
My main.js :
import Vue from 'vue'
import App from './App.vue'
import axios from 'axios'
import VueAxios from 'vue-axios'
import firebase from 'firebase'
import "firebase/auth";
import "firebase/analytics";
import "firebase/firestore";
import moment from 'moment'
Vue.use(VueAxios, axios)
Vue.config.productionTip = false
new Vue({
render: h => h(App),
firebase: firebase,
}).$mount('#app')
I have also tried removing all node modules, clearing NPM cache and reinstalling NPM dependencies to no avail.
instead of use the use function to add your axios instance globally can you please try to add it like this:
import Vue from 'vue'
import App from './App.vue'
import axios from 'axios'
import VueAxios from 'vue-axios'
import firebase from 'firebase'
import "firebase/auth";
import "firebase/analytics";
import "firebase/firestore";
import moment from 'moment'
Vue.config.productionTip = false
Vue.prototype.$axios = Axios; //<---- like that
new Vue({
render: h => h(App),
firebase: firebase,
}).$mount('#app')
after that try to compile again. you are then able to call your axios instance on every component with this.$axios
this error accrues when compiler cant find module or file you want to import within your node modules file
when module cant be found compiler goes to current directory to find that and if it cant find it through this error

Typescript Cannot find Module "fs" even though #types/node installed

I know the usual fix for this one is to install #types/node , and I have 10.12.23 of that installed.
This appears to be a strange error and I am a bit baffled by it. I have 2 other npm modules installed: config ( which requires #types/config ) and firebase-admin which must have it's own typescript types. Also using VS code version 1.31 . I even tried installing an older version of #types/node
The following works fine
import admin from "firebase-admin";
import fs from "fs";
The following fails: cannot find module 'fs'
import admin from "config";
import fs from "fs";
The following fails: cannot find module 'fs'
import fs from "fs";
I am not using any other packages / webpack or anything else. Any ideas are appreciated.
Besides installing #types/node, check that the tsconfig.json:
does not have a compilerOptions types, or make sure "node" is included there
noResolve does not exist or is set to false
esModuleInterop is set to true or you'll get error Module "fs" has no default export
Import fs in your ts/js file
import fs from 'fs'

TypeScript - how to import 'os' module

I'm learning Typescript. To do this, I'm building a basic utility app with Node for myself. For this app, I need to use Node's OS Module. My question is, how do I import this module?
In my Typescript file, I have the following:
import { os } from 'os';
This line generates the error: "Cannot find module 'os'". What am I missing?
This line generates the error: "Cannot find module 'os'". What am I missing?
The correct code is
import os from 'os';
Also make sure you have npm i #types/node
More
Some notes I wrote on NodeJS quickstart : https://basarat.gitbook.io/typescript/docs/quick/nodejs.html
Just to update this entry:
import * as os from 'os';
and later, you can use:
const hostname = os.hostname();

Import node package vue.js loader webpack

I am using the basic vue loader setup with webpack(https://vue-loader.vuejs.org/en/start/setup.html).
In my main.js I import the necessary:
import Vue from 'vue'
import App from './App.vue'
import firebase from 'firebase'
import docx2html from 'docx2html'
Both firebase and docx2html are in the node_modules folder under src and specified in the dependencies folder. For some reason firebase is imported, but docx2html can not be found. It returns the error:
Module not found: Error: Can't resolve 'docx2html' in
'C:\path\to\src'.
How should I import docx2html, and why doesn't it work this way?

Resources