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

Error when Importing Module #97

Closed
callum-atwal opened this issue Dec 12, 2019 · 15 comments
Closed

Error when Importing Module #97

callum-atwal opened this issue Dec 12, 2019 · 15 comments
Assignees

Comments

@callum-atwal
Copy link

I get this error when I import the latex.js module

image

All I've done is put this at the top of my javascript file
import { parse, HtmlGenerator } from 'latex.js'

I've installed the latex.js package using the --save-prod flag.

Any ideas?

@michael-brade
Copy link
Owner

Hm.... not yet. I need a little more information. I see rollup -c, so does this mean the error is from rollup? If so, I still don't get the error message, 500531 is in the middle of an identifier (toRoot). Are you using ES6 everywhere?

Ah, maybe you could try this, just in case:

import { parse, HtmlGenerator } from 'latex.js/dist/latex.esm.js'

@michael-brade michael-brade self-assigned this Dec 12, 2019
@allisonh
Copy link

allisonh commented Dec 29, 2019

I was able to get it working by doing the import like this:

const latexjs = require('latex.js');
const HtmlGenerator = latexjs.default.HtmlGenerator;
const parse = latexjs.default.parse;
...

I also needed to run the script by adding -r esm, i.e.:

node -r esm <my-js-file>

(also had to install esm module for this)

@michael-brade
Copy link
Owner

Yes, that is how it should be. But if you use import instead of require, you can avoid stating .default explicitely.

@allisonh
Copy link

When I replace the 3 lines with just what's recommended in the documentation (as well as the variant suggested above), i.e.:

import { parse,HtmlGenerator } from 'latex.js'
const latex = 'This is some text';
let generator = new HtmlGenerator({ hyphenate: false })
let doc = parse(latex, { generator: generator }).htmlDocument()
console.log(doc.outerHTML)

I get this error:

SyntaxError: The requested module 'file:///Users/allison/WebstormProjects/sandbox/node_modules/latex.js/dist/latex.esm.js' does not provide an export named 'parse'
    at startup (bootstrap_node.js:194:16)
    at bootstrap_node.js:618:3

I'm running Node 9.3, could that be the problem?

@michael-brade
Copy link
Owner

hm... let me check.

@michael-brade
Copy link
Owner

ok, there are a few things to mention:

  • I totally forgot, usually when you are using import ..., babel transforms it into es3, which then uses require and .default, like you did manually
  • however, I wanted to know more, I thought node should be able to deal with import. Turns out, you need to pass the --experimental-modules flag for node < 13 AND you need to name the file .mjs. So it is kind of incompatible...
  • I also found that I used the wrong export in src/index.js :-( I fixed that, the named exports were missing.

Now it still doesn't work, some requires are not translated by rollup, and I will probably have to rename latex.esm.js to latex.mjs and require node 13.x.... I'll think about it.

@michael-brade
Copy link
Owner

On the other hand, -r esm could be a solution, I have not tried that one yet.

@allisonh
Copy link

@michael-brade - thanks for looking into that. I'll just continue using the workaround above.

@Donsome
Copy link

Donsome commented Apr 23, 2020

when i use this package in Vuecli, i cant import too. that's the error:

Failed to compile.
./node_modules/latex.js/dist/latex.esm.js
Module parse failed: Unexpected token (1:500342)

@michael-brade
Copy link
Owner

hm... I have no idea yet. But this is the next thing I will get to. I saw that the current rollup now has a new hook for dynamic imports (rollup/rollup#3449) so I hope this will help, too.

@mfekadu
Copy link

mfekadu commented May 20, 2020

Updates on this with node v12.0.0.0

latex.js = 0.12.1

const latex = require("latex.js") && node

stacktrace

$ node
Welcome to Node.js v12.0.0.
Type ".help" for more information.
> 
> 
> const latex = require("latex.js")
Thrown:
/rbd/pnpm-volume/e815515d-cfec-4ce3-9257-d111fad4eb94/node_modules/.registry.npmjs.org/latex.js/0.12.1/node_modules/latex.js/dist/latex.esm.js:1
var commonjsGlobal="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function unwrapExports(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}function createCommonjsModule(t,e){return t(e={exports:{}},e.exports),e.exports}var he=createCommonjsModule(function(t,e){!function(r){var n=e,i=t&&t.exports==n&&t,a="object"==typeof commonjsGlobal&&commonjsGlobal;a.global!==a&&a.window!==a||(r=a);var o=/[\uD800-\uDBFF][\uDC00-\uDFFF]/g,s=/[\x01-\x7F]/g,l=/[\x01-\t\x0B\f\x0E-\x1F\x7F\x81\x8D\x8F\x90\x9D\xA0-\uFFFF]/g,u=/<\u20D2|=\u20E5|>\u20D2|\u205F\u200A|\u219D\u0338|\u2202\u0338|\u2220\u20D2|\u2229\uFE00|\u222A\uFE00|\u223C\u20D2|\u223D\u0331|\u223E\u0333|\u2242\u0338|\u224B\u0338|\u224D\u20D2|\u224E\u0338|\u224F\u0338|\u2250\u0338|\u2261\u20E

SyntaxError: Unexpected token export
> 

const latex = require("latex.js") && node -r esm

stacktrace

$ node -r esm
Welcome to Node.js v12.0.0.
Type ".help" for more information.
> const latex = require("latex.js")
Thrown:
/rbd/pnpm-volume/e815515d-cfec-4ce3-9257-d111fad4eb94/node_modules/.registry.npmjs.org/latex.js/0.12.1/node_modules/latex.js/dist/latex.esm.js:1
TypeError: Cannot read property 'createElement' of undefined
    at e (/rbd/pnpm-volume/e815515d-cfec-4ce3-9257-d111fad4eb94/node_modules/.registry.npmjs.org/latex.js/0.12.1/node_modules/latex.js/dist/latex.esm.js:1)
    at /rbd/pnpm-volume/e815515d-cfec-4ce3-9257-d111fad4eb94/node_modules/.registry.npmjs.org/latex.js/0.12.1/node_modules/latex.js/dist/latex.esm.js:1
    at /rbd/pnpm-volume/e815515d-cfec-4ce3-9257-d111fad4eb94/node_modules/.registry.npmjs.org/latex.js/0.12.1/node_modules/latex.js/dist/latex.esm.js:1
    at createCommonjsModule (/rbd/pnpm-volume/e815515d-cfec-4ce3-9257-d111fad4eb94/node_modules/.registry.npmjs.org/latex.js/0.12.1/node_modules/latex.js/dist/latex.esm.js:1)
    at Object.<anonymous> (/rbd/pnpm-volume/e815515d-cfec-4ce3-9257-d111fad4eb94/node_modules/.registry.npmjs.org/latex.js/0.12.1/node_modules/latex.js/dist/latex.esm.js:1)
    at Generator.next (<anonymous>)
> 

import { parse,HtmlGenerator } from 'latex.js' && node

stacktrace

$ node
Welcome to Node.js v12.0.0.
Type ".help" for more information.
> import { parse,HtmlGenerator } from 'latex.js'
Thrown:
import { parse,HtmlGenerator } from 'latex.js'
       ^

SyntaxError: Unexpected token {
> 

import { parse,HtmlGenerator } from 'latex.js' && node -r esm

stacktrace

app@question-eval:~ 21:59 
$ node -r esm
Welcome to Node.js v12.0.0.
Type ".help" for more information.
> import { parse,HtmlGenerator } from 'latex.js'
Thrown:
/rbd/pnpm-volume/e815515d-cfec-4ce3-9257-d111fad4eb94/node_modules/.registry.npmjs.org/latex.js/0.12.1/node_modules/latex.js/dist/latex.esm.js:1
TypeError: Cannot read property 'createElement' of undefined
    at e (/rbd/pnpm-volume/e815515d-cfec-4ce3-9257-d111fad4eb94/node_modules/.registry.npmjs.org/latex.js/0.12.1/node_modules/latex.js/dist/latex.esm.js:1)
    at /rbd/pnpm-volume/e815515d-cfec-4ce3-9257-d111fad4eb94/node_modules/.registry.npmjs.org/latex.js/0.12.1/node_modules/latex.js/dist/latex.esm.js:1
    at /rbd/pnpm-volume/e815515d-cfec-4ce3-9257-d111fad4eb94/node_modules/.registry.npmjs.org/latex.js/0.12.1/node_modules/latex.js/dist/latex.esm.js:1
    at createCommonjsModule (/rbd/pnpm-volume/e815515d-cfec-4ce3-9257-d111fad4eb94/node_modules/.registry.npmjs.org/latex.js/0.12.1/node_modules/latex.js/dist/latex.esm.js:1)
    at Object.<anonymous> (/rbd/pnpm-volume/e815515d-cfec-4ce3-9257-d111fad4eb94/node_modules/.registry.npmjs.org/latex.js/0.12.1/node_modules/latex.js/dist/latex.esm.js:1)
    at Generator.next (<anonymous>)
> 

@michael-brade
Copy link
Owner

Thank you! I think I have it all fixed by now 😁 I'll do another release soo!

igor-anferov added a commit to igor-anferov/smart-guide that referenced this issue May 21, 2020
@michael-brade
Copy link
Owner

Hi guys, I have just released LaTeX.js v0.12.3. Can you test this again please? I hope this fixes all of your issues.

With NodeJS 14, node now imports the correct format depending on if you import from an ECMAScript Module or if you require as a standard/legacy node module.

@michael-brade
Copy link
Owner

Ok, too early. I found two more problems 😕 why is this so hard...

@michael-brade
Copy link
Owner

Alright, finally those problems should be fixed. All test cases pass, including loading LaTeX.js as ES6 module and as CommonJS module.

However, webpack doesn't support import.meta.url yet, so with webpack you cannot load latex.mjs, you have to use latex.js (see here webpack/webpack#6719) or you have to use a loader like https://www.npmjs.com/package/@open-wc/webpack-import-meta-loader (@luanshuai: this is relevant for vuecli, for instance).

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

No branches or pull requests

5 participants