简体   繁体   English

如何使用require而不是import加载JavaScript库

[英]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 . 我正在创建一个node.js AWS Lambda无服务器应用程序,并尝试包含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. 文档中的示例使用了来自'simmerjs'的import Simmer,这当然会在ES5中引发语法错误。 I am using the aws SAM cli for my local dev environment. 我在本地开发环境中使用aws SAM cli。 The sam invoke command starts a Docker container running Node.js v10.15.3. sam invoke命令启动运行Node.js v10.15.3。的Docker容器。

This version does not support ES6 without the --experimental-mode flag. 没有--experimental-mode标志,此版本不支持ES6。 So far, I have not found a way to start the container with the --experimental-mode enabled. 到目前为止,我还没有找到启用--experimental-mode来启动容器的方法。

Is there any way to load the simmerjs using the ES5 require('simmerjs') syntax? 有什么方法可以使用ES5 require('simmerjs')语法加载Simmerjs? I have tried but it returns a "TypeError: Simmer is not a constructor" error when I try to create a new object. 我尝试过,但是在尝试创建新对象时返回“ TypeError:Simmer不是构造函数”错误。

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. 我应该能够从Simmer库中创建一个新的Simmer对象。 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. 或者,如果我能找到一种在本地和部署时都启用实验模式的方法,那么我应该能够使用ES6语法。

Update 更新资料

To get this to work with commonJS, I have to specify the default constructor. 为了使它与commonJS一起使用,我必须指定默认构造函数。

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

Sure. 当然。 You would just do 你会做

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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