Skip to content

Cannot find module 'assemblyscript/asc' or its corresponding type declarations. #2241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bbrk24 opened this issue Mar 27, 2022 · 23 comments
Closed
Labels

Comments

@bbrk24
Copy link

bbrk24 commented Mar 27, 2022

The website says it's possible to call asc programmatically in JavaScript, and can be imported as follows:

import asc from "assemblyscript/asc";

Source: Programmatic usage

However, trying to do so in TypeScript fails with the error message from the title. There is no @types/assemblyscript package either, at least not that yarn can find.

TypeScript seems to think the module is called assemblyscript/dist/asc, as I can import that with no such error.

@bbrk24
Copy link
Author

bbrk24 commented Mar 27, 2022

If I ignore the errors and run it in JavaScript anyways, Node gives me the error Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './asc' is not defined by "exports" in C:\Users\[...]\node_modules\assemblyscript\package.json. I tried it in Node v16 and v17, but they both give me the same error. However, I definitely see it in the package.json, so I'm not really sure what's up.

@dcodeIO
Copy link
Member

dcodeIO commented Mar 27, 2022

This is a relatively recent change (in v0.20). What's the version of the compiler you are using?

@bbrk24
Copy link
Author

bbrk24 commented Mar 27, 2022

@dcodeIO I have version 0.20.2 installed.

@dcodeIO
Copy link
Member

dcodeIO commented Mar 27, 2022

Toying around with this a little, seems the import can be resolved when adding "moduleResolution": "NodeNext" to tsconfig.json. However, there's a new error then on

import asc from "assemblyscript/asc";

stating

Module 'assemblyscript/asc' cannot be imported using this construct. The specifier only resolves
to an ES module, which cannot be imported synchronously. Use dynamic import instead.ts(1471)

which seems just as odd, as it doesn't go away even when adding "target": "ESNext" and "module": "NodeNext".

There is another hint, however, stating

Compiler option 'module' of value 'nodenext' is unstable. Use nightly TypeScript to silence this error.
Try updating with 'npm install -D typescript@next'.ts

which I haven't tested. Perhaps it's simply not supported by stable TS yet?

@bbrk24
Copy link
Author

bbrk24 commented Mar 27, 2022

That's unfortunate. I didn't realize it was ESM-only; I'm using CommonJS for imports, so that's probably why it's broken.

@bbrk24
Copy link
Author

bbrk24 commented Mar 29, 2022

@dcodeIO I got it to work in TypeScript 4.5.5 by using --module ES2022 --moduleResolution node, but I have to import assemblyscript/dist/asc.js rather than just assemblyscript/asc.

@bbrk24
Copy link
Author

bbrk24 commented Mar 30, 2022

I am seeing very strange behavior when I do this. For example, the following code logs out several megabytes, mostly base-64-encoded data:

import asc from "assemblyscript/dist/asc.js";
throw new Error();

@dcodeIO
Copy link
Member

dcodeIO commented Mar 30, 2022

I believe that's because Node.js by default doesn't load source maps, and somehow believes that printing all minified code on the same line is of great use. Could also be connected to Binaryen's Emscripten output, not sure, so perhaps not Node. Nonetheless, providing --enable-source-maps to the Node.js invocation should yield a proper error, i.e.:

"test:browser": "node --enable-source-maps tests/browser",

@bbrk24
Copy link
Author

bbrk24 commented Mar 30, 2022

Yep, --enable-source-maps helped. Still, the original issue (that I have to use a different import path than documented) stands.

@dcodeIO
Copy link
Member

dcodeIO commented Mar 30, 2022

My intuition here is that, when it works in plain JS, this would be an incompatibility of TS with Node's new exports field. Would assume that the TS folks are already working on it, but hard to say for sure. Does it perhaps work with typescript@next, as the warning above recommends?

@bbrk24
Copy link
Author

bbrk24 commented Mar 31, 2022

@dcodeIO No, but I currently can't compile at all with typescript@next, due to a compilation error in node_modules/typescript/lib/typescript.d.ts. I've filed an issue at microsoft/TypeScript#48504.

@jerrygreen
Copy link

jerrygreen commented Apr 4, 2022

Before trying Typescript just try to import it from js for starters. I'm getting Cannot find module error with just pure js (when trying to import dist files).

At least I'm using Node v14.15.2, but you seem to stuck into the similar thing with v16-17 too, so I guess it's not the Node version.

@bbrk24
Copy link
Author

bbrk24 commented Apr 4, 2022

@jerrygreen I couldn't import it from the Node REPL or a commonjs script. I had to either save it as a .mjs file or put "type": "module" in package.json. Even then, I'm using Node 17.

@jerrygreen
Copy link

I have type: module in package json but this doesn't resolve the problem

@0xbe1
Copy link

0xbe1 commented Apr 17, 2022

This is a relatively recent change (in v0.20). What's the version of the compiler you are using?

I downgrade to 0.19.23 and it works

@piotr-oles
Copy link

I tried to update to the newest assemblyscript in the as-loader library (a webpack loader for assemblyscript). Unfortunately, ESM doesn't work well with webpack... Would it be possible to provide a CJS format too? I feel like the node environment is not fully ready for the switch to ESM.

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in one week if no further activity occurs. Thank you for your contributions!

@github-actions github-actions bot added the stale label May 19, 2022
@Upperfoot
Copy link

This is still an issue

@github-actions github-actions bot removed the stale label May 24, 2022
@ufReitter
Copy link

ufReitter commented Jun 1, 2022

After spending the whole day upgrading my nestjs backend to use ESM modules I had at first no luck.
Still got this error in the editor and tsc compiler.

Additionally just supressing the error with:

// @ts-ignore
import asc from 'assemblyscript/asc';

did the trick.

The latest version of the ASC compiler works fine now in my backend.

Besides
"type": "module",
in the package.json
I use these
"compilerOptions": {
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"skipLibCheck": true,
"noImplicitAny": false,
"strictNullChecks": false,
"strictPropertyInitialization": false,
"esModuleInterop": true,
"baseUrl": "./",
"outDir": "./dist",
"target": "es2020",
"lib": ["es2020", "DOM"],
"module": "ES2020",
"moduleResolution": "Node",
"strict": true,
"sourceMap": true,
"allowSyntheticDefaultImports": true,
"declaration": true
},

with nodejs 18.2.0

@bbrk24
Copy link
Author

bbrk24 commented Jun 1, 2022

I don't like to use @ts-ignore or @ts-expect-error, but I see why that may be necessary here.

@jerrygreen
Copy link

Actually the issue is not an issue if all those requirements are met:

  1. You use import asc from 'assemblyscript/dist/asc.js' instead of import asc from 'assemblyscript/asc.js'
  2. assemblyscript v0.20.3 (newer version might fit as well)
  3. Node v16 (I have Node v16.14.2, whereas Node v14 doesn't work)
  4. "type": "module" in package.json

Break any of it and you gonna have a bad time :(

@piotr-oles
Copy link

Yep - especially when you build a package for another tool that didn't migrate to ESM :/ I cannot use new bindings stuff for the as-loader for webpack, because webpack doesn't support ESM

@github-actions
Copy link

github-actions bot commented Jul 2, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in one week if no further activity occurs. Thank you for your contributions!

@github-actions github-actions bot added the stale label Jul 2, 2022
@github-actions github-actions bot closed this as completed Jul 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants