TranscryptOrg / Transcrypt

Python 3.9 to JavaScript compiler - Lean, fast, open!
https://www.transcrypt.org
Apache License 2.0
2.86k stars 215 forks source link

Compiling for nodes aborts with "SyntaxError: Cannot use import statement outside a module" #832

Open halloleo opened 1 year ago

halloleo commented 1 year ago

I'm trying to use the sample nodejs_demo.py with node, so I compile it with

transcrypt -b -p .none  nodejs_demo.py  

However when I run this as

node ./__target__/nodejs_demo.js

I get the error "SyntaxError: Cannot use import statement outside a module".

How can I fix this?


Environment

JennaSys commented 1 year ago

I got it to work using the instructions in the readme.txt for the demo. After compiling I ran it with the following node command (note that the command in the readme isn't quite accurate):

node nodejs_demo.bundle.js 8080

But I run into the same issue as you using the -p .none Transcrypt compile option where the import statement can't be used with commonJS. But then setting "type": "module" in the package.json like the node error message suggests, results in the require() function not working in the compiled ES module.

halloleo commented 1 year ago

Thank you so much @JennaSys! The rollup bundler does the trick indeed! I didn't look at the README to the demo, sorry, I just had followed the instructions in the doco.

JennaSys commented 1 year ago

I'm not quite familiar enough with JS and Node off the top of my head to figure out how to fix what is shown in the docs for the demo, but I'm glad the other method works for you.

This issue should be left open since both the demo readme and the docs need to be fixed.