Transcrypt icon indicating copy to clipboard operation
Transcrypt copied to clipboard

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

Open halloleo opened this issue 3 years ago • 3 comments

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

  • Transcrypt 3.9.0
  • Node 14.17.3

halloleo avatar Nov 25 '22 04:11 halloleo

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.

JennaSys avatar Nov 25 '22 05:11 JennaSys

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.

halloleo avatar Nov 28 '22 04:11 halloleo

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.

JennaSys avatar Nov 28 '22 05:11 JennaSys