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

Cannot use JavaScript view from CLI when project has "type": "module" #732

Closed
eobrain opened this issue Dec 30, 2019 · 1 comment · Fixed by #733
Closed

Cannot use JavaScript view from CLI when project has "type": "module" #732

eobrain opened this issue Dec 30, 2019 · 1 comment · Fixed by #733

Comments

@eobrain
Copy link
Contributor

eobrain commented Dec 30, 2019

If the package.json for a project has enabled ES6 modules by including

  "type": "module",

then it is not possible to use a JavaScript view.

If you try to create a view with a .js extension then you get an error such as the following:

(node:14179) Warning: require() of ES modules is not supported.
require() of /home/eobrain/src/mergi/template/index_view.js from /home/eobrain/src/mergi/node_modules/mustache/bin/mustache is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index_view.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/eobrain/src/mergi/package.json.
internal/modules/cjs/loader.js:1163
      throw new ERR_REQUIRE_ESM(filename);

If you try to solve the problem by renaming the view to have a .cjs suffix you get an error such as the following

Shooot, could not parse view as JSON.
Tips: functions are not valid JSON and keys / values must be surround with double quotes.

SyntaxError: Unexpected token m in JSON at position 0

because it is not recognized as being JavaScript and it is being parsed as JSON.

@phillipj
Copy link
Collaborator

Hi @eobrain,

thanks for taking the time to write such a great bug report, much appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants