Node.js - Unable to load from module - javascript

Using a JS sdk of Icon a blockchain. Trying to using the SDK API calls and I'm having issues import or require
When I use Import an error is thrown SyntaxError: Cannot use import statement outside a module
When I use require an error is thrown ReferenceError: HttpProvider is not defined
Below is the link to Icon Icx Github
Icon GitHub
const IconService = require('icon-sdk-js');
//import IconService from 'icon-sdk-js';
const provider = new HttpProvider('https://bicon.net.solidwallet.io/api/v3');
const iconService = new IconService(provider);
const totalSupply = iconService.getTotalSupply().execute();
console.log(totalSupply);

In NodeJS you use require to "import" node modules and dependencies to your project
const IconService = require('icon-sdk-js');
Regarding this line const provider = new HttpProvider('https://bicon.net.solidwallet.io/api/v3');
Where are you importing HttpProvider from? HttpProvider is not built in Javascript library.
If it is a node module, you have to do something similar
const HttpProvider = require('HttpProvider'); (in your question you don't specify what HttpProvider is)

I think you might use IconService.HttpProvider('the url'); to use the constructor

Related

Import nodejs module with version number into typescript

So I have a node module which seems to use a versioning system. Using nodejs I simply used
const Package = require('package-name').V1;
which worked without issues. Typescript however does not like the .V1 so I use
import { Package } from 'package-name';
which complies fine but the typescript output to javascript is
const package_name_1 = require("package-name");
Which means that any function or properties of package_name_1 are undefined since the module doesn't seem to load V1.js file (i assume that is how it works..)
So I tried
import * as Package from 'package-name';
But it outputs the same javascript as before..
The actual folder structure of the package I am using is
-node_modules
--package-name
---client
----v1
----v1.js
with the v1.js file looking like
var PackageV1 = {}
PackageV1.CONSTANTS = require('./v1/constants');
PackageV1.Request = require('./v1/request');
PackageV1.Session = require('./v1/session');
...
module.exports = PackageV1;
Of course things like
const Package = require('package-name.V1');
do not work
Error: Cannot find module 'package-name.V1'
How can I require this V1.js file / directory using the typescript method?
As was hinted at in the comments
import * as Package from 'package-name';
is fine but when using the package I had to use
Package.V1.Request()
rather than
Package.Request()

How to define export for a module in javascript?

Hey I am doing a small project using react-app and I
have been trying all day to create export for this module.
I have installed it with npm, and i want to edit it so i can import and use it in my app.js
I have tried to define "reddit" using class\function\let and use either:
export default
module.exports
And either
import reddit from 'reddit.js';
var reddit = require('reddit.js');
And trying to check with a simple function from the module:
console.log(reddit.hot('cats'));
But I am still getting:
Uncaught TypeError: reddit.hot is not a function
I am a bit lost, what am I doing wrong?
The module uses window.reddit global variable. Don't override it!
So if you are on client side, just use:
require('reddit.js')
//...
reddit.hot('cats')
For server side - you have to do some trick to make it work, because on server side you don't have 'window' global variable.
Upd:
Example of back end usage:
const window = {};
const r = require('./reddit.js') // You don't really use r
const reddit = window.reddit;
reddit.hot('cats')
Reddit does not export anything because it was mainly designed to be added as a script tag!
// So for CommonJS module:
require('reddit.js');
//And for ES6 modules
import 'reddit.js';
And you you will be able to access reddit and reddit.hot() method through the window object.

Load “Vanilla” Javascript Libraries into Node.js = 'Unexpected token export'

Building on related issue: Load "Vanilla" Javascript Libraries into Node.js
I'm trying to load a 'Vanilla' Javascript library 'rsa.js' into NodeJS, using the method described by Chris W. in the question above. In essence I've created a custom node_modules directory like this:
./node_modules/rsa/rsa-lib/rsa.js
./node_modules/rsa/rsa-lib/README
./node_modules/rsa/index.js
Only problem is, compiling the module fails on the last line of rsa.js:
undefined:836
export default rsa;
SyntaxError: Unexpected token export
rsa.js
var rsa = {};
(function(rsa) {
...
})(rsa);
console.log("rsa",rsa);
export default rsa;
index.js
var fs = require('fs');
// Read and eval library
filedata = fs.readFileSync('./node_modules/rsa/rsa-lib/rsa.js','utf8');
eval(filedata);
/* The rsa.js file defines a class 'rsa' which is all we want to export */
exports.rsa = rsa
Any suggestions how to fix this problem?
The error 'Unexpected token export' is caused because the library is using
export default thingToExport;
instead of
module.exports = thingToExport
This is an ES6 feature not supported by Node (yet), so when Node tries to run the code, it throws an error.
How to solve: try modifying the last line of the library so it says module.exports = rsa;.
I should add, though, that eval is not a good way to load a library into your own code. That is what require is for. If you have installed this library with npm i and it is in your node_modules, you should be able to load it into your code with var rsa = require('rsa');.
Again though, if you're not transpiling the code, it may have problems with export default, so you will probably want to change that to module.exports either way.

ES6 syntax import Electron (require..)

To learn the new ES6 syntax, I've been trying to refactor some JS code.
I'm absolutely confused though by the whole import / export methods.
How do I change this require statement into ES6?
var remote = require('electron').remote
I've seen this answer but:
It doesn't work
It doesn't really seems to be much ES6-sque
Any thoughts?
It seems imports are not implemented in either Node 6 or Chrome 51 so Electron also does not support them, according to this post: https://discuss.atom.io/t/does-electron-support-es6/19366/18
And also the last electron doc doesn't use imports, they use destructuring syntax:
const { BrowserWindow } = require('electron').remote
// or
const { remote } = require('electron')
const { BrowserWindow } = remote
http://electron.atom.io/docs/api/remote/
But you can use babel with the require hook:
http://babeljs.io/docs/usage/require/
To be auto compile each required modules so you will be able to use imports.
Of course the script given to electron (the one that require babel) is not compiled so you need to make a bootstrap:
// bootwithbabel.js
require("babel-register");
require( process.argv.splice(2) );
In shell (sh):
electron bootwithbabel.js app.es
alias electrones="electron bootwithbabel.js "
electrones coron.es // ^^
Then in your app you can then write:
import electron from 'electron';
import { remote } from 'electron';
You can also import only the remote module:
import { remote } from 'electron';
But you can only import both in one statement:
import electron, { remote } from 'electron'
electron.ipcRenderer.on();
let win = new remote.BrowserWindow({width: 800, height: 600});
remote.getGlobal(name)
playground
I'm absolutely confused though by the whole import / export methods.
Mixing different module systems can indeed be confusing.
It doesn't work
const electron = require('electron');
const remote = electron.remote;
is exactly the same as what you have
var remote = require('electron').remote
If yours work, the other will as well. However, I would simply stick with yours.
It doesn't really seems to be much ES6-sque
Who cares? Node doesn't support ES6 imports and exports natively and it's not super clear how CommonJS modules should map to ES6 modules. I recommend to stick with require if you are only writing for Node anyway.
You could try to do
import electron from 'electron';
const {remote} = electron;
These days every version of Electron comes with basic typescript support. So if you use a TS or TSX file in your project -then you can use ES Import statements inside that file. Whether you use an ES module or not.
https://www.electronjs.org/blog/typescript

Nodejs import fails with syntax error

I am making use of react-router node module for routing in a react app. I am importing required modules as follows.
var React = require('react');
var Router = require('react-router');
var { Route, RouteHandler, Link } = Router;
// ... remaining code ...
But I am getting syntax error on line no. 3 i.e. var { Route, RouteHandler, Link } = Router;
Error:
Uncaught SyntaxError: Unexpected token {
Doing:
var {x,y} = {x:3,y:5};
Is called a destructuring assignment and is a new feature in JavaScript, it requires a new JavaScript runtime. This feature is not supported in NodeJS yet and not even in v8 yet (the JS engine JavaScript runs on). You can either assign it in 3 lines manually or use a tool like Traceur or Babel to compile your ES6 (new specification of EcmaScript) to ES5 (what node runs) code.

Categories