Skip to content
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

ScriptEngineException: SyntaxError: Cannot use import statement outside a module #491

Closed
jnilsson89 opened this issue Feb 17, 2023 · 1 comment
Assignees
Labels

Comments

@jnilsson89
Copy link

I am working on a Worker Service project that includes the ClearScript NuGet package. ClearScript enables me to call JavaScript functions from my C# code without importing modules, which has been working great so far. However, I need to import a file in my JavaScript code using the "node-fetch" library in my .js file.

The line of code I have added is: import fetch from 'node-fetch'.

Unfortunately, when I run ExecuteDocument, the application crashes with the following error message: "ScriptEngineException: SyntaxError: Cannot use import statement outside a module."

Although I have read that using a JavaScript engine should be possible, I am still very new to this technology and have been unable to find a solution to get my code running. Thank you in advance for any assistance you can provide.

@ClearScriptLib ClearScriptLib self-assigned this Feb 17, 2023
@ClearScriptLib
Copy link
Collaborator

Hi @jnilsson89,

Unfortunately, when I run ExecuteDocument, the application crashes with the following error message: "ScriptEngineException: SyntaxError: Cannot use import statement outside a module."

The import declaration is valid only within a module. If you're using ClearScript's V8ScriptEngine, you can execute a document as a JavaScript module as follows:

// using Microsoft.ClearScript.JavaScript;
engine.ExecuteDocument(pathOrUrl, ModuleCategory.Standard);

Keep in mind however that node-fetch, like most npm packages, is likely to be dependent on the Node.js API, whereas ClearScript provides only the standard JavaScript built-ins, aiming to make it easy to use .NET to provide additional facilities.

Good luck!

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

2 participants