How to import an npm package into Polymer 3 - es6-modules

I often find packages on npm that I want to include in a Polymer3 element.
the instructions to install the package are often to use npm with something like npm install fingerprintjs2
Below I included the basic Polymer 3 elementas an example. but i get the error
Uncaught TypeError: Cannot read property 'exports' of undefined
I dont understand how I should import from https://github.com/Valve/fingerprintjs2
import {html, PolymerElement} from '#polymer/polymer/polymer-element.js';
import './node_modules/fingerprintjs2/dist/fingerprint2.min.js';
class FingerPrint extends PolymerElement {
static get template() {
return html`
<style>
:host {display: block;}
</style>
<h2>Finger Print</h2>
`;
}
ready() {
super.ready();
setTimeout(function () {
Fingerprint2.get(function (components) {
console.log(components)
})
}, 5000)
}
} window.customElements.define('finger-print', FingerPrint);
if I try to import it with
import Fingerprint2 from './node_modules/fingerprintjs2/dist/fingerprint2.min.js';
then I get the error Uncaught SyntaxError: The requested module '../fingerprintjs2/dist/fingerprint2.min.js' does not provide an export named 'default'
I also tried
import {Fingerprint2} from './node_modules/fingerprintjs2/dist/fingerprint2.min.js';
and get Uncaught SyntaxError: The requested module '../fingerprintjs2/dist/fingerprint2.min.js' does not provide an export named 'Fingerprint2'

very simple:
import Fingerprint2 from 'fingerprint2'

Related

hardhat 'pragma solidity' marked as unexpected identifier

I have been working on my ethereum project when face with issue new contracts stops compiling with error message:
blockchain/contracts/Utils.sol:2
pragma solidity ^0.8.9;
^^^^^^^^
SyntaxError: Unexpected identifier
I simply can not create a new contract anymore. It looks like there is some break in the environment.
Have you ever face with this issue? Do you have any thoughts what is wrong here?
Hardhat config is:
import { HardhatUserConfig } from "hardhat/config";
import "#nomicfoundation/hardhat-toolbox";
const config: HardhatUserConfig = {
solidity: "0.8.9",
};
export default config;
The issue was in import in .ts file.
My current import for test is shown below:
import { expect } from "chai";
import { ethers } from "hardhat";
import { utils, BigNumber } from "ethers";
import { time, loadFixture } from "#nomicfoundation/hardhat-network-helpers";
import "../contracts/Utils.sol";
Remove import "../contracts/Utils.sol"; from import solves the issue.
It is redundant import, hardhat is able to evaluate type of the contract just by its name in the factory. The root cause though is unclear.
In Hardhat, this error occurs while running .sol Hardhat this error occurs while running .sol contract instead of .js deployment script

Angular 6 with lowdb - not getting it to work

I'm trying to create an Electron app with Angular 6 that uses lowdb as a local database.
It's all very new to me and it's trial and error, but I don't seem to be able to figure out how to overcome the following error:
I've installed lowdb in my application by using the commands
npm install --save lowdb (edit: forgot to mention I did this already)
and
npm install --save #types/lowdb
I've created a service to communicate with this "local database".
import { Injectable } from '#angular/core';
import lowdb from 'lowdb';
import { default as FileAsync } from 'lowdb/adapters/FileAsync';
import { CurrentUserModel } from '../models/current-user';
#Injectable({
providedIn: 'root'
})
export class LowdbService {
private db: lowdb.LowdbAsync;
constructor() {
this.initDatabase();
}
set( field: string, value: any ) {
this.db.set( field, value ).write();
}
private async initDatabase() {
const adapter = new FileAsync( 'db.json' );
this.db = await lowdb( adapter );
this.db.defaults( { user: CurrentUserModel } );
}
}
But when I include the service in the constructor of a component I get errors.
ERROR in ./src/app/services/lowdb.service.ts
Module not found: Error: Can't resolve 'lowdb' in '/Users/paul/Projects/application-name/src/app/services'
ERROR in ./src/app/services/lowdb.service.ts
Module not found: Error: Can't resolve 'lowdb/adapters/FileAsync' in '/Users/paul/Projects/application-name/src/app/services'
ℹ 「wdm」: Failed to compile.
ERROR in src/app/services/lowdb.service.ts(2,12): error TS1192: Module '"/Users/paul/Projects/application-name/node_modules/#types/lowdb/index"' has no default export.
src/app/services/lowdb.service.ts(3,14): error TS2305: Module '"/Users/paul/Projects/application-name/node_modules/#types/lowdb/adapters/FileAsync"' has no exported member 'default'.
As far as I can see I'm doing the same as mentioned in this Github comment and this Stackoverflow comment. I can't find any more documentation though.
Can somebody help me out?
Update
Using import * as lowdb from 'lowdb' seemed to solve my initial errors. But it resulted in a few other errors. See below.
ERROR in ./node_modules/graceful-fs/polyfills.js
Module not found: Error: Can't resolve 'constants' in '/Users/paul/Projects/project-name/node_modules/graceful-fs'
ERROR in ./node_modules/graceful-fs/graceful-fs.js
Module not found: Error: Can't resolve 'fs' in '/Users/paul/Projects/project-name/node_modules/graceful-fs'
ERROR in ./node_modules/graceful-fs/fs.js
Module not found: Error: Can't resolve 'fs' in '/Users/paul/Projects/project-name/node_modules/graceful-fs'
ERROR in ./node_modules/graceful-fs/legacy-streams.js
Module not found: Error: Can't resolve 'stream' in '/Users/paul/Projects/project-name/node_modules/graceful-fs'
First of all, if you installed just #types/lowdb, that is only typings for that library, you need to install the library itself via:
npm install --save lowdb
Then, looks like there is no default export so import lowdb from 'lowdb'; also won't work. Try is like this:
import { LowdbAsync } from 'lowdb';
import * as lowdb from 'lowdb';
import * as FileAsync from 'lowdb/adapters/FileAsync';
private db: LowdbAsync<any>; // TODO provide correct schema instead of any
private async initDatabase() {
const adapter = new FileAsync('db.json');
this.db = await lowdb(adapter);
this.db.defaults({ user: any });
}
Or you can try removing those typings (as they might be outdated) and do what their docs says:
https://github.com/typicode/lowdb/tree/master/examples#browser
import low from 'lowdb'
import LocalStorage from 'lowdb/adapters/LocalStorage'
(but when you take a look at #types/lowdb, there is no such default export so again, the typings might be outdated)
As for your other issue, take a look here:
https://github.com/typicode/lowdb/issues/206#issuecomment-324884867
typicode commented on 25 Aug 2017
#DickyKwok I think it's impossible. Can you answer this, #typicode?
I confirm, to save to disk using FileSync or FileAsync adapters you need to have access to Node fs API.
Looks like you should use LocalStorage adapter instead.
Another similar issue here: How can I use node "fs" in electron within angular 5
You could also try the solution proposed in one of answers there:
https://github.com/ThorstenHans/ngx-electron
constructor(private _electronService: ElectronService) {
this.fs = this._electronService.remote.require('fs');
}
Looks like your lowdb is missing in node_modules.
try
npm install lowdb

How to import external JavaScript into Polymer 3.0

Let's say I have a traditional javascript library such as google-charts and I wanted to incorporate that into my Polymer 3.0 project. How would I actually do the import?
This is how I would traditionally do it: https://developers.google.com/chart/interactive/docs/quick_start
ES Modules on NPM
Normally, one would prefer an ES module version of such a library if available (e.g., on NPM), since ES modules allow tree shaking (by Polymer CLI in this case) to reduce the size of the production build output.
Modules are loaded in JavaScript with the import keyword. If available on NPM, the library could be installed in your Polymer project with the npm-install command. For instance, you'd install google-charts like this:
npm install --save google-charts
Then, import that package in your element code:
import { GoogleCharts } from 'google-charts';
Example:
import { PolymerElement } from '#polymer/polymer';
import { GoogleCharts } from 'google-charts';
class MyElement extends PolymerElement {
// ...
ready() {
super.ready();
GoogleCharts.load(/* ... */);
}
}
demo
Non-ES Modules (not on NPM)
On the other hand if ESM is unavailable, the script is likely an AMD/UMD module, which adds symbols to the global scope. In that case, you'd still import the file, and access the global as you normally would had you used the <script> tag.
If the script is not on NPM, you'd have to copy the file locally into your project (and include the library file in your release), and then import the file by path. For example, if you copied the library to <root>/src/libs/google-charts.js, the element code at <root>/src/components/Chart.html would import it like this:
import '../libs/google-charts'
Example:
import { PolymerElement } from '#polymer/polymer';
import '../libs/google-charts' // adds GoogleCharts to global
class MyElement extends PolymerElement {
// ...
ready() {
super.ready();
GoogleCharts.load(/* ... */);
}
}

Using import () in vue to import the component incorrectly

I'm using the babel-plugins-sync-dynamic -import package in the vue instance. What should I do?
I installed the babel-plugin-synch-dynamic -import package, and in the vue instance import the corresponding components, and he had the following error.
export default {
name: 'App',
created () {
console.log(111)
Promise.resolve(import('./Test.vue')).then(response => {
console.log(response.default) // //Shouldn't you load a component dynamically?
})
}
}
I wrote it like this.An error is as follows

Can't import any java classes

HelloWorld.ceylon
import java.util { HashMap } //Error:(1, 8) ceylon: package not found in imported modules: java.util (define a module and add module import to its module descriptor)
void run() {
print("test");
}
module.properties
module CeylonHelloWorld "1.0" {
import java.base "8";
}
I get an exception in HelloWord.ceylon file
When I try that code, I get:
Incorrect syntax: mismatched token CeylonHelloWorld expecting initial-lowercase identifier
In module.ceylon.
The name of a module is supposed to be of form foo.bar.baz (initial-lowercase identifiers separated by periods).
Like mentioned by Gavin you will have to use a legal module name, when I change your code to use the module name "java8test" I get the following output when compiling:
$ ceylon compile java8test
warning: It looks like you are using class files from a Java newer than 1.7.
Everything should work well, but if not, let us know at https://github.com/ceylon/ceylon-compiler/issues.
In the near future, the Ceylon compiler will be upgraded to handle Java 1.8.
./source/java8test/run.ceylon:1: warning: import is never used: 'HashMap'
import java.util { HashMap }
^
2 warnings
Note: Created module java8test/1.0.0
Which is all as expected.
module.ceylon
module holaCeylon "1.0.0"{
import java.base "7"; // versión 7 JDK
}
package.ceylon
shared package holaCeylon;
Now we go back to the run.ceylon file and import the java.util.HashMap Java library.
run.ceylon
import java.util { HashMap }
shared void run(){
print("Importando librerias de Java en Ceylon");
value romanos = HashMap<String,Integer>();
romanos.put("I", 1);
romanos.put("V", 5);
romanos.put("X", 10);
romanos.put("L", 50);
romanos.put("C", 100);
romanos.put("D", 500);
romanos.put("M", 1000);
print(romanos.values());
print(romanos.keySet());
}
Output:
salida
Code:
http://codemonkeyjunior.blogspot.mx/2015/03/ceylon-interoperabilidad-con-java.html