AS won't let me import from an external module. This does not work:
import { Foo } from "bar"
But this relative import does work:
import { Foo } from "./node_modules/bar"
Yeah, there is no concept of external modules yet. When you import from "bar", that file is looked up within stdlib paths. You should be able to do this if you specify --lib someDir on compilation, with a bar.ts inside.
Thanks, makes sense. Is there any plan to add module resolution? I want to offer an ewasm module on npm that people can simply npm install into their project. I suppose I can work around this using --lib for now.
I fear that doing module resolution the same way as node will lead to issues with TS/AS dependencies becoming mixed up. One idea on Slack recently was to use a special asMain property in package.json, and if that's not present fall back to assembly/index.ts as the entry file. Wdyt?
I'd really love to see this working. I feel like this is the missing piece that would let folks run wild. It would also make it possible to move implementations in std/assembly all out to external modules, where it belongs.
It is not possible for now to import library that has import another AS library.
As an example, I had to add one of node_modules dir to github project:
https://github.com/fluencelabs/signature-connector
And only after this, I could import this project to another properly.
Why this is important: there is no ecosystem and simple ways to create libs around AS language.
So, on every project developers cannot reuse another code.
Already support
It's not yet supported, right?
It's already supported. Are you have any problem with that?
Umm... yeah. you can refer to this issue: https://github.com/as2d/as2d/issues/5
I think it's related to --lib or --path flag. Can you give me an example of how to use it?
did you try
import { .. } from "as2d";
instead
import { .. } from "as2d/assembly/index";
?
No, and I tried it just now, but it does not work, emitting a different error message.
import {
CanvasRenderingContext2D,
getContextById,
} from "as2d";
(base) ➜ as2d-example git:(master) ✗ npm run asbuild
> [email protected] asbuild /Users/jm/Documents/Code/as2d-example
> asc assembly/index.ts node_modules/as2d/assembly/glue.ts -b build/optimized.wasm -t build/optimized.wat --sourceMap --validate --optimize --use Math=JSMath
ERROR: Import file '~lib/src/shared/CanvasDirection.ts' not found.
at parseBacklog (/Users/jm/Documents/Code/as2d-example/node_modules/assemblyscript/cli/asc.js:397:34)
at Object.main (/Users/jm/Documents/Code/as2d-example/node_modules/assemblyscript/cli/asc.js:450:16)
at Object.<anonymous> (/Users/jm/Documents/Code/as2d-example/node_modules/assemblyscript/bin/asc:21:26)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
What version of AssemblyScript you installed?
package.json
"as2d": "github:as2d/as2d",
"assemblyscript": "github:assemblyscript/assemblyscript",
I think the latest?
currently better install via npm
I did rm -rf node_modules and tried again with
"assemblyscript": "0.8.1",
but unfortunately, it didn't work
maybe it's a problem related to as2d?
cc @jtenner
@9oelM Did you find a way to resolve this issue? I have the exact same error message when trying to import blakejs, so I don't think it's related to as2d.
These are the only dependencies I have, installed with Yarn.
"dependencies": {
"assemblyscript": "^0.8.1",
"bignum": "MaxGraey/bignum.wasm",
"blakejs": "^1.1.0"
}
When I'm trying to import import { blake } from 'blakejs'; I'm getting the same error you posted in https://github.com/AssemblyScript/assemblyscript/issues/186#issuecomment-557799653
^^ Solved... I tried to import JavaScript into AssemblyScript which obviously doesn't work... :|
Most helpful comment
I fear that doing module resolution the same way as node will lead to issues with TS/AS dependencies becoming mixed up. One idea on Slack recently was to use a special
asMainproperty inpackage.json, and if that's not present fall back toassembly/index.tsas the entry file. Wdyt?