2

LWR -> Lightning Web Runtime developer.salesforce.com/docs/platform/lwr/guide/lwr-intro.html

I'll try to provide a clear project structure and questions. While learning LWR, I've started a new LWR project and chose a Single Page App for the project type

This gives a starting project structure like the following:

src
- modules / example
- - app
- - - app.css
- - - app.js
- - - app.html
lwr.config.json
package.json

My question is what is the correct way to include other npm packages or modules for consumption in my project? Do I need to add new module references to my lwr.config.json?

I've tried including some directly in my app.js but learned that is not the way with project type of module.

Thanks in advance for taking a look, I know LWR is still new so looking to learn where I can. I appreciate it!

2

1 Answer 1

1

There's a demo of this in lwc-recipes-oss.

First, you just install d3:

npm install d3 --save

Then you can import it in your JS file:

import { LightningElement } from 'lwc';
import * as d3 from 'd3';
...

You can clone the repo and follow the instructions in the README.md file to see d3 in action, as well as seeing it in action in the LWC OSS documentation.

3
  • Thanks for the response! Can this still be accomplished when the module is an inbuilt node module that needs a "requires" statement instead of the ES6 "import"?
    – nickforce
    Commented May 26, 2022 at 16:37
  • @nickforce In LWC OSS, import { LightningElement } from 'lwc' is actually importing from an installed node module called lwc. This is done via webpack to build the components and babel to provide backwards compatibility for older browsers. As such, the example should work fine if you've installed a library that does something you need to do.
    – sfdcfox
    Commented May 26, 2022 at 17:24
  • If I need to use a CommonJS method like 'requires' for example, should I be adding an installed node module to my LWR.Config.JSON?
    – nickforce
    Commented May 26, 2022 at 19:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .