1

I'm trying to use AssemblyScript to build a WebAssembly inference engine for a TensorFlow.js model that I have.

I started with essentially the quickstart AssemblyScript app (which works great) and then simply added @tensorflow/tfjs to my dependencies:

$ npm install --save @tensorflow/tfjs

and added an import to assembly/index.ts:

import * as tf from "@tensorflow/tfjs";

Full code here on Github

This results in an error when I build it:

$ npm run asbuild

> test-assemblyscript@1.0.0 asbuild
> npm run asbuild:untouched && npm run asbuild:optimized


> test-assemblyscript@1.0.0 asbuild:untouched
> asc assembly/index.ts --target debug

ERROR TS6054: File '~lib/@tensorflow/tfjs.ts' not found.

 import * as tf from "@tensorflow/tfjs";
                     ~~~~~~~~~~~~~~~~~~
 in assembly/index.ts(1,21)

FAILURE 1 parse error(s)

Am I misunderstanding the import syntax? I am puzzled why it would be looking in ~lib for this versus node_modules.

outside2344
  • 2,075
  • 2
  • 29
  • 52
  • 2
    You can't import non-assemblyscript into AssemblyScript – Sebastian Speitel Nov 25 '21 at 07:44
  • Yes apparently AssemblyScript imports are done slightly different than the ESM standard. It's explained [here](https://www.assemblyscript.org/concepts.html#module-imports). However `tfjs` is not an AS file anyways so you can not import it before transpiling the whole thing into AS. – Redu Nov 13 '22 at 14:20

1 Answers1

0

If you are sure the module you are trying to import is assemblyscript files, you can import with something like this ./node_modules/@tenderflow/tfjs.

I am not sure if @tensorflow has assembly files built, but I did that on assemblyscript-json for assemblyscript@0.19.8 (0.25.2 doesn't have that problem).

For example

assemblyscript-json has exported assemblyscript files from its package, so I can do this (ref)

import { JSON } from './node_modules/assemblyscript-json/assembly';

export function formatJsonString(jsonString: string): string {
  const jsonObj: JSON.Obj = <JSON.Obj>JSON.parse(jsonString);
  return jsonObj.stringify();
}
Alan Shum
  • 1
  • 1
  • 1