简体   繁体   中英

How to load javascript library with require instead of import

I'm creating a node.js AWS Lambda serverless app and trying to include simmerjs https://github.com/gmmorris/simmerjs .

The example in the docs use import Simmer from 'simmerjs' which, of course throws a syntax error in ES5. I am using the aws SAM cli for my local dev environment. The sam invoke command starts a Docker container running Node.js v10.15.3.

This version does not support ES6 without the --experimental-mode flag. So far, I have not found a way to start the container with the --experimental-mode enabled.

Is there any way to load the simmerjs using the ES5 require('simmerjs') syntax? I have tried but it returns a "TypeError: Simmer is not a constructor" error when I try to create a new object.

Here's a code excerpt with my tried and failed results in the comments. I can load the file but cannot create an object.

// top of index.js 

// all statements below cause SyntaxError: Unexpected token
//import Simmer from 'simmerjs';
//import Simmer from './simmerjs';
//import { Simmer } from './simmerjs';

// Statement works but causes "TypeError: Simmer is not a constructor" error
// later when trying to instantiate an object
const Simmer = require('simmerjs');

const jsdom = require("jsdom");
const AWS = require("aws-sdk");
const sql = require("mssql");
const S3 = new AWS.S3();
var steps = new Array();

exports.handler = (event, context, callback) => {
    .
    .
    .
    omitted for clarity
        elements = await convertElements(elArray, prevDom);
}

const convertElements = async (fromArray, dom) => {
  // {dom} contains a jsdom window object - tried with document object as well   

  // all code below returns "TypeError: Simmer is not a constructor" error
    const mysimmer = new Simmer(dom,'{specificityThreshold: 100}',false);
  //const mysimmer = new Simmer(dom);
  //const simmer = new Simmer(dom, null, false);
  //let simmer = new Simmer(dom);
  //var simmer = new Simmer(dom,null, null);

  //Does not return "constructor error" but it gets the same results as 
  // new Simmer() can not use the object
  //const simmer = Simmer;  

  . 
  . omitted for clarity
  .
 }

According to the docs:

By the book:
    import Simmer from 'simmerjs'
    const simmer = new Simmer()
    const el =  document.getElementById('#SomeElement')

Docs Node example:
    import Simmer from 'simmerjs'
    const virtualWindow = new JSDom()
    const simmer = new Simmer(virtualWindow)
    const reconfiguredSimmer = simmer.configure({ /* some custom configuration */ })

I should be able to create a new simmer object from the Simmer library. Alternatively, if I could find a way to enable experiment-mode both locally and when deployed I should be able to use the ES6 syntax.

Update

To get this to work with commonJS, I have to specify the default constructor.

const Simmer = require('simmerjs').default;

Sure. You would just do

...
var Simmer = require('local/path/to/dist/simmerjs');
var whatever = new Simmer(virtualWindow)
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM