right way to import npm-module

Post Reply
User avatar
foxpalace
Member
Posts: 37
Joined: Fri Jan 14, 2011 12:25 pm
Location: Germany

right way to import npm-module

Post by foxpalace »

Hi, what is the standard way to use an mpm module?
PowerSwitch 2024 Spring
npm install fs-extra
old script works with: import * as fs from "fs-extra";

newly created script won't import it with this command.

Greetings
User avatar
tdeschampsBluewest
Member
Posts: 127
Joined: Tue Jun 01, 2021 11:57 am

Re: right way to import npm-module

Post by tdeschampsBluewest »

Scripting within switch designer interface does not support NPM import.
Scripting within scripter (with a proper TS file) will support NPM.

On top of your main.ts file you should import your script as :

Code: Select all

import fse from "fs-extra"
Destructuring syntax with a star (as below) tend to be less readable. Keep it only when you need partial functions of the module.

Code: Select all

import { tmpdir } from "tmp" 

Once it's set, you still have to open a shell at the same level of your main.ts file

Code: Select all

cd "whatsoever/is/your/path/to/maints"
npm i fs-extra
Note that in VScode, you could also right click on the main.ts file in the workspace and use "Open in integrated terminal".

Hope that will help you :)
Do you like the Enfocus Apps developed by Bluewest?
Feel free to leave a comment on the Appstore!
User avatar
foxpalace
Member
Posts: 37
Joined: Fri Jan 14, 2011 12:25 pm
Location: Germany

Re: right way to import npm-module

Post by foxpalace »

Hi, yes I use Visual Studio Code ... 95% of my Scripts are now TypeScript ... and I have a lot. I don't know the Version of PowerSwitch (there is somewhere a Webinar) we should use require - now back to import. Sometimes works require sometimes import :)
Post Reply