Bootstrap loading in an NW.js application (node โ€‹โ€‹-webkit) with a clean npm build

I am trying to create a simple desktop application with NW.js. Inspired by some posts, including this one , I wanted to try a clean npm approach without any bower , grunt or gulp .

So, I am using jquery and bootstrap libraries installed as npm . When I insert modules into my javascript application as follows:

 global.jQuery = $ = require("jquery"); var bootstrap = require("bootstrap"); 

I get ReferenceError: document is not defined from bootstrap . I can load bootstrap through the html document as before:

 <script type="text/javascript" src="node_modules/bootstrap/dist/js/bootstrap.js"></script> 

I ask myself if the stupid idea is to load the require boot tray into a script file? In principle, I do not need him. It can be downloaded using the script tag in html. What are the best practices? I am embarrassed.

Refresh. Decision

After getting advice from @Kuf, I created a build.js script

 var copyfiles = require('copyfiles'); copyfiles(["node_modules/bootstrap/dist/js/bootstrap.js", "vendor/js"], true, function (err) { if (err) return console.error(err); }); copyfiles(["node_modules/bootstrap/dist/css/bootstrap.css", "vendor/css"], true, function (err) { if (err) return console.error(err); }); 

called prestart hook:

 "scripts": { "build": "node build.js", "prestart": "npm run build" } 

which allows me access to bootstrap resources using convenient paths:

 <link href="vendor/css/bootstrap.css" rel="stylesheet"> <script type="text/javascript" src="vendor/js/bootstrap.js"></script> 
+5
source share
1 answer

I think the error is due to the fact that you are executing a request without a window context. What are you working with? note that for code running from node-main :

Window

: ... is not available at script loading time because the script is executed before the DOM window is loaded. ( source )

Even when importing JS using require, you still need to include the css files. I do not think that there is another โ€œcleanโ€ or ready-made solution. Although what you do will definitely work, it will create a weird, rather than natural, script path.

I would add a build script :

 "scripts": { "build": "node build.js" } 

in package.json, copy the boot from node_modules to '/ dist /' to make the path cleaner.

+1
source

Source: https://habr.com/ru/post/1242574/


All Articles