svgdom icon indicating copy to clipboard operation
svgdom copied to clipboard

Vanilla node fails to import

Open telamon opened this issue 4 years ago • 12 comments

$ node
Welcome to Node.js v13.2.0.
Type ".help" for more information.
> require('svgdom')
Thrown:
Error: No valid exports main found for '.../node_modules/svgdom'
    at resolveExportsTarget (internal/modules/cjs/loader.js:611:9)
    at applyExports (internal/modules/cjs/loader.js:492:14)
    at resolveExports (internal/modules/cjs/loader.js:541:12)
    at Function.Module._findPath (internal/modules/cjs/loader.js:643:22)
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:941:27)
    at Function.Module._load (internal/modules/cjs/loader.js:847:27)
    at Module.require (internal/modules/cjs/loader.js:1016:19)
    at require (internal/modules/cjs/helpers.js:69:18) {
  code: 'MODULE_NOT_FOUND'
}

telamon avatar May 17 '20 18:05 telamon

Did you install it?

$ node
Welcome to Node.js v13.12.0.
Type ".help" for more information.
> require('svgdom')
{
  defaults: Object [Module] {
    fontSize: [Getter],
    fontFamily: [Getter],
    fontDir: [Getter],
    fontFamilyMappings: [Getter]
  },
  Attr: [Function: Attr],
  CharacterData: [Function: CharacterData],
  Comment: [Function: Comment],
  CustomEvent: [Function: CustomEvent],
  DOMImplementation: {
    hasFeature: [Function: hasFeature],
    createDocumentType: [Function: createDocumentType],
    createDocument: [Function: createDocument],
    createHTMLDocument: [Function: createHTMLDocument]
  },
  Document: [Function: Document],
  DocumentFragment: [Function: DocumentFragment],
  Element: [Function: Element],
  Event: [Function: Event],
  EventTarget: [Function: EventTarget],
  Node: [Function: Node] {
    ELEMENT_NODE: 1,
    ATTRIBUTE_NODE: 2,
    TEXT_NODE: 3,
    CDATA_SECTION_NODE: 4,
    ENTITY_REFERENCE_NODE: 5,
    ENTITY_NODE: 6,
    PROCESSING_INSTRUCTION_NODE: 7,
    COMMENT_NODE: 8,
    DOCUMENT_NODE: 9,
    DOCUMENT_TYPE_NODE: 10,
    DOCUMENT_FRAGMENT_NODE: 11,
    NOTATION_NODE: 12
  },
  NodeFilter: [Function: NodeFilter] {
    FILTER_ACCEPT: 1,
    FILTER_REJECT: 2,
    FILTER_IGNORE: 4,
    SHOW_ALL: -1,
    SHOW_ELEMENT: 1,
    SHOW_TEXT: 4,
    SHOW_ENTITY_REFERENCE: 16,
    SHOW_ENTITY: 32,
    SHOW_PROCESSING_INSTRUCTION: 64,
    SHOW_COMMENT: 128,
    SHOW_DOCUMENT: 256,
    SHOW_DOCUMENT_TYPE: 512,
    SHOW_DOCUMENT_FRAGMENT: 1024,
    SHOW_NOTATION: 2048
  },
  Text: [Function: Text],
  Window: [Function: Window],
  HTMLElement: [Function: HTMLElement],
  HTMLImageElement: [Function: HTMLImageElement],
  HTMLLinkElement: [Function: HTMLLinkElement],
  HTMLParser: [Function: HTMLParser],
  HTMLScriptElement: [Function: HTMLScriptElement],
  elementAccess: {
    getElementsByTagName: [Function: getElementsByTagName],
    getElementsByTagNameNS: [Function: getElementsByTagNameNS],
    getElementsByClassName: [Function: getElementsByClassName]
  },
  ParentNode: {
    matchWithScope: [Function: matchWithScope],
    query: [Function: query],
    querySelectorAll: [Function: querySelectorAll],
    querySelector: [Function: querySelector],
    prepend: [Function: prepend],
    append: [Function: append],
    replaceChildren: [Function: replaceChildren]
  },
  SVGElement: [Function: SVGElement],
  SVGGraphicsElement: [Function: SVGGraphicsElement],
  matrixFactory: [Function: matrixFactory],
  SVGMatrix: [Function: SVGMatrix],
  SVGPathElement: [Function: SVGPathElement],
  SVGPoint: [Function: SVGPoint],
  SVGSVGElement: [Function: SVGSVGElement],
  SVGTextContentElement: [Function: SVGTextContentElement],
  setFontDir: [Function: setFontDir],
  setFontFamilyMappings: [Function: setFontFamilyMappings],
  preloadFonts: [Function: preloadFonts],
  getConfig: [Function: getConfig],
  getFonts: [Function: getFonts],
  config: {
    setFontDir: [Function: setFontDir],
    setFontFamilyMappings: [Function: setFontFamilyMappings],
    preloadFonts: [Function: preloadFonts],
    getConfig: [Function: getConfig],
    getFonts: [Function: getFonts]
  },
  createDocument: [Function: createDocument],
  createHTMLDocument: [Function: createHTMLDocument],
  createSVGDocument: [Function: createSVGDocument],
  createWindow: [Function: createWindow],
  createHTMLWindow: [Function: createHTMLWindow],
  createSVGWindow: [Function: createSVGWindow]
}

Fuzzyma avatar May 17 '20 21:05 Fuzzyma

Sorry for the stressed issue report, Yeah i installed svg-dom, I got my test-code working yesterday by directly including:

require('./node_modules/svgdom/main-require.js')

I suspect that there's a misconfiguration in package.json but it's a bummer that the bug doesn't reproduce on your end. Could also relate to our node-versions i guess.

telamon avatar May 18 '20 10:05 telamon

Yes it will be quite sure the node version. However, all the stuff is backwards compatible. The main entry in the package.json points to main-require.cjs and that key is resolved in old node versions too.

Fuzzyma avatar May 18 '20 10:05 Fuzzyma

Yeah i agree that it "should", the exports look syntactically correct comparing to the docs: https://nodejs.org/api/esm.html#esm_approach_2_isolate_state

But then again backwards-compatiblity "should" include the version i'm running as well - honestly I'm a bit intrigued by this issue, it shouldn't exist.

telamon avatar May 18 '20 11:05 telamon

Its strange. If you have a look at #60 you can see that the code seems to work with node 12 or even node 8. Maybe it is a bug specific to your version? Or something different is going on. You could try to use excatly my version to see if the error persists.

Fuzzyma avatar May 18 '20 11:05 Fuzzyma

(node: v13.2.0)

When I use :

import { createSVGWindow } from 'svgdom';

I get:

internal/modules/esm/default_resolve.js:94
  let url = moduleWrapResolve(specifier, parentURL);
            ^

Error: Cannot resolve package main '[object Object]' in /node_modules/svgdom/package.json, imported from 
src/image.js
    at Loader.resolve [as _resolve] (internal/modules/esm/default_resolve.js:94:13)
    at Loader.resolve (internal/modules/esm/loader.js:74:33)
    at Loader.getModuleJob (internal/modules/esm/loader.js:148:40)
    at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:41:40)
    at link (internal/modules/esm/module_job.js:40:36) {

henrypenny avatar Jul 30 '20 22:07 henrypenny

Same. I use typescript and while it compiles it gets an error: window is not defined when using the code in the README.md file. VSCode is also not able to resolve the package at all. It gives an error Cannot find module 'svgdom' or its corresponding type declarations.ts(2307). I added custom typings for this package just in case this is just a formal error. But it did not help. Strange

dasheck0 avatar Jan 02 '21 16:01 dasheck0

I am using svg.js with typescript right now and have no problems whatsoever. If window is not defined in your typescript you might want to run typescript with the dom lib enabled?

Fuzzyma avatar Jan 02 '21 19:01 Fuzzyma

as far as I can tell, svgdom does not include typescript type definitions?

larshp avatar Jan 11 '21 13:01 larshp

I obviously mixed up the repositories there...

No, svgdom has no typescript definitions but you could just use the dom lib which typescripts provide. Its a dom after all ^^

Fuzzyma avatar Jan 11 '21 15:01 Fuzzyma

the issue for me is the createSVGWindow function, I've worked around the issue by @ts-ignore'ing the import,

// @ts-ignore, https://github.com/svgdotjs/svgdom/issues/69
import {createSVGWindow} from "svgdom";
const window = createSVGWindow();

larshp avatar Jan 11 '21 15:01 larshp

@larshp please see https://github.com/svgdotjs/svgdom/issues/110

altano avatar Aug 28 '23 08:08 altano