Sebastian
Sebastian

Reputation: 134

How to Import/require a Browserify compiled file with nodejs

I like import psd.js in my Electron/NodeJS Application.

The normal way, npm i psd and require('psd') does not work for me, because then psd.js runs in NodeMode. Not in the Browser-Mode. I have to require the dist/psd.js directly, to get the BrowserMode. (Details in the github issue)

The Browser Version of dist/psd.js is already compiled with Browserify. Because of that its not a module which I can require with Node.

Has someone a solution for that with which i don't have to manipulate the psd.js file?


psd.js issue: https://github.com/meltingice/psd.js/issues/59

The psd.js example to that, which won't work for me, because the require is from browserify and not from NodeJS: https://github.com/meltingice/psd.js/blob/master/examples/browser/image.html

<script type="text/javascript" src="../../dist/psd.min.js"></script>
var PSD = require('psd');

The browserified js file: https://github.com/meltingice/psd.js/blob/master/dist/psd.js

If I replace the require=...... with module.exports= i can require that as usual

Upvotes: 1

Views: 829

Answers (1)

filype
filype

Reputation: 8380

You'll need to install the module via npm.

The script tag is not necessary. Once the package has been installed using npm you can require it in your index.html file with:

var psd = require('psd');

I am new to electron, and that's what I did when I included a node module in my index.html file. I am not sure how electron does it, it seems strange to require a node module in the browser (index.html) but it works.

Upvotes: 1

Related Questions