how to import / require standard node modules in typescript? - import

What is the best way to use require with typescript and standard NPM modules?
I'm trying to use the debug package.
I have installed from npm
also tsd install debug
However, Identical syntax is OK in one file, but not in another.
I guess this is a load order thing, and TS thinks I am redeclaring a variable?
let debug = require("debug")("async-test");
# ReferenceError: debug is not defined
debug = require("debug")("async-test");
# ReferenceError: debug is not defined
identical code on left/right panels (different files) will show an error/not.

What is the best way to use require with typescript and standard NPM modules?
Give typings a go. It has great debug definitions https://github.com/typed-typings/npm-debug.
npm install typings -g
typings install debug
Then setup your tsconfig.json: https://github.com/typings/typings#maindts-and-browserdts
Now you can just do:
import debug = require('debug')
With complete type safety 🌹

Related

Source "#openzeppelin/contracts/token/ERC721/ERC721.sol" not found: File import callback not supported

I've imported the Open Zeppelin ERC721 token standard into my VS Code with the Solidity extension, but see the following warnings on all my OZ import statements:
Screenshot of error
Why is this happening and what is the workaround for this warning?
What I've tried:
change default workspace compiler to localNodeModule (began to throw other warnings like on the pragma solidity line)
Example of solution I've tried
Just install the Solidity+Hardhat Extension ,this will take care of the errror.
run below command
npm install #openzeppelin/contracts
Change the import line like this
import "./node_modules/#openzeppelin/contracts/token/ERC721/ERC721.sol";
You could try this solution here, the only one that helped me.
https://stackoverflow.com/a/72241149/7537543
When you compile programmatically using solc, new syntax was introduced, which you have to include in compile.js.
// New syntax (supported from 0.5.12, mandatory from 0.6.0)
var output = JSON.parse(
solc.compile(JSON.stringify(input), { import: findImports })
);
You should have a helper function for finding imports
function findImports(relativePath) {
//my imported sources are stored under the node_modules folder!
const absolutePath = path.resolve(__dirname, 'node_modules', relativePath);
const source = fs.readFileSync(absolutePath, 'utf8');
return { contents: source };
}
Unfortunately I ran into this error too & just gave the path manually:
import "/home/ev1lclow3n/node_modules/#openzeppelin/contracts/token/ERC721/ERC721.sol";
This solved my error.
(I'm a linux user so path may differ for you)
Thanks ;-)
you have to manually guide the open zepplin import to its source file if you have it downloaded in your node modules then all you have to do is to change its path like this " ../node_modules/" and also make sure to use the latest extension of juan blanco's solidity extension and solidity and hardhat extension and if you are following a tutorial your first lines of codes would probably be import "hardhat/console.sol"; all you have to do here is to manually direct only this file to its designated place and the others would do it by themselves.
What you have to do is:
If you have "Solidity by Juan Blanco" for Truffle and "Solidity by Nomic Foundation" for Hardhdat, and if you are using Hardhat, disable the one by Juan Blanco and just use the one by Nomic Foundation, it just worked for me.
Screenshot
Make sure to create a Hardhat project (npx hardhat) and install:
npm install --save-dev "hardhat#^2.12.7" "#nomicfoundation/hardhat-toolbox#^2.0.0"
npm i #openzeppelin/contracts
Ok. That was a dumb question. Two things you have to do:
(1) Install the OZ library via
npm install #openzeppelin/contracts
(2) If you see Error HH606 (i.e. project can't compile), it's likely because The Solidity version pragma statement in these files doesn't match any of the configured compilers in your config.. Ensure that your pragma version matches the version in your hardhat config.
Hope this helps.

Idris not properly installing/finding packages

I'm currently trying to use Blodwen, the prototype of Idris 2, as a backend for a language I'm writing. So I'm trying to import its ttimp package for my project.
I go into the Blodwen directory and do idris --install ttimp.ipkg
Then in my project's .ipkg I have:
opts = "--cg-opt -O2 -i ~/GitHub/Blodwen/"
pkgs = ttimp
However, when I run idris --build myproject.ipkg I get the error
Entering directory `./src'
The following packages were specified but cannot be found:
- ttimp
If I remove ttimp from pkgs, then I get Can't find import Core/Binary
i.e. it can't find the modules from the package.
If I try to build ttimp with TARGET=~/.idris-libs
idris --install ttimp.ipkg
Entering directory `./src'
Can't find import Builtins
Can't find import Prelude
Can't find import Data/List
My Question: How can I make sure ttimp is being installed properly? Where does Idris install packages by default? Why isn't Idris finding the library after I've installed it?
I am not sure what your current goal is, but when you install blodwen it is using idris to a) install ttimp and to compile blodwen, which is available as the executable blodwen afterwards. So if you want to work with blodwen, you should also use blodwen to compile your files and install additional libraries.

Error installing purescript-list

I'm new to Purescript and am following the tutorial for installation. Purescript itself is working and I can start the CLI using pulp psci, but installing purescript-list runs into trouble.
Having entered the command bower install purescript-lists --save, I get a long list of package names, but when it gets to purescript-eff and purescript-prelude I run into some version conflicts:
bower purescript-eff#^2.0.0 cached https://github.com/purescript/purescript-eff.git#2.0.0
bower purescript-eff#^2.0.0 validate 2.0.0 against https://github.com/purescript/purescript-eff.git#^2.0.0
Unable to find a suitable version for purescript-eff, please choose one by typing one of the numbers below:
1) purescript-eff#^1.0.0 which resolved to 1.0.0 and is required by purescript-console#1.0.0
2) purescript-eff#^2.0.0 which resolved to 2.0.0 and is required by purescript-st#2.0.0
Prefix the choice with ! to persist it to bower.json
? Answer
A similar message is shown for purescript-prelude. No matter which options I choose, both pulp build and pulp run fail with:
$ pulp build
* Building project in /Developer/purescript/training1
Error found:
in module PSCI.Support
at /Developer/purescript/training1/bower_components/purescript-psci-support/src/PSCI/Support.purs line 10, column 34 - line 10, column 53
Cannot import value unsafeInterleaveEff from module Control.Monad.Eff.Unsafe
It either does not exist or the module does not export it.
See https://github.com/purescript/purescript/wiki/Error-Code-UnknownImport for more information,
or to contribute content related to this error.
Compiling PSCI.Support
* ERROR: Subcommand terminated with exit code 1
What have I missed here?
Thanks
Chris W
If you are using psc version 0.10.* you should go with prelude, lists and eff v2*.
If you are using psc version 0.9.* you should go with prelude, lists and eff v1*.
If you are using psc 0.10.* you might want to update pulp to version 9.1.0
The problem occurs due to breaking changes between psc 0.9 and 0.10 and the relevant libraries. by writing bower install purescript-lists --save you are asking bower for the latest dependencies which conflict with the dependency versions specified in your bower.json.

How can I include a core.js polyfill in ember?

I was under the impression that I could do:
app.import('bower_components/core.js/library/fn/object/entries.js');
But it's not resolving the imported portions of it. If I start linking it up myself I end up with:
app.import('bower_components/core.js/modules/_global.js');
app.import('bower_components/core.js/modules/_export.js');
app.import('bower_components/core.js/library/fn/object/entries.js');
And
Module is not defined.
What's the proper way of doing this?
Right okay it seems like I need to install core.js then build it:
npm run grunt build:es7.object.entries
Then include the product of that.

Can I use CoffeeScript to write my Electron (Atom Shell) application?

Does anything special have to be done to get Electron to run my main.coffee file? I have a main.js file (that works) that I converted to CoffeeScript (hence main.coffee), but when I run Electron main.coffee I get an error like the following:
App threw an error when running [SyntaxError: /Users/foo/develop/electron/main.coffee:13
app.on('window-all-closed', ->
^
Unexpected token >]
I can only assume this is a CoffeeScript issue, since when I commented the offending code with CoffeeScript's block comment (###), I got the following:
App threw an error when running [SyntaxError: /Users/foo/develop/electron/main.coffee:13
###
^
Unexpected token ILLEGAL]
I added coffee-script to my packages.json as a dependency, and made sure it was installed to my local node_modules directory like my other application dependencies, but that didn't seem to help.
I think, the main file main.js has to be javascript. But you can require a coffee file, for example application.coffee, from there using coffee-script.
main.js
// main.js
require('coffee-script').register();
require('./application')
application.coffee
# application.coffee
app = require('app')
BrowserWindow = require('browser-window')
# ...
Installing coffee-script
Include it in your package.json:
{
...
"devDependencies": {
"electron-prebuilt": "^0.33.1",
"coffee-script": "~1.10.0"
}
}
And run:
npm install
I've recently discovered that instead of transpiling to Javascript, you can do something like:
<script>
require('coffee-script').register();
require('../src/app/boot');
and then in src/app/boot.coffee you can use regular CoffeeScript :)
I found it in the app https://github.com/postcasio/hearthdash so there are more examples there.
There is no way to do it (atom doesn't ship with a coffeescript compiler), but you can use the watch option of coffeescript,
-w, --watch watch scripts for changes and rerun commands
For example:
coffee -w main.coffee in your case.