Node.js: Loading module as if in a browser
See original GitHub issueWhen I try to require('@zxing/library')
in Node.js it fails with the following error:
/Users/mattmahn/code/ms-barcodes/node_modules/@zxing/library/umd/index.min.js:1
(function (exports, require, module, __filename, __dirname) { !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("ZXing",[],e):"object"==typeof exports?exports.ZXing=e():t.ZXing=e()}(window,function(){return function(t){var e={};function r(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"st
ReferenceError: window is not defined
at Object.<anonymous> (/Users/mattmahn/code/ms-barcodes/node_modules/@zxing/library/umd/index.min.js:1:262)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (/Users/mattmahn/code/ms-barcodes/f.js:1:63)
at Module._compile (internal/modules/cjs/loader.js:689:30)
Node.js version: v10.15.0 zxing-js/library version: 0.11.0
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
JavaScript modules - MDN Web Docs
This guide gives you all you need to get started with JavaScript module syntax.
Read more >Load Node.js modules in browsers via lobrow - 2ality
Node.js calls its files modules. We want those modules to be universal, to work on browsers, too. The lobrow library lets you do...
Read more >load module in requirejs only in browser, not in nodejs
I wrote modules for both browser and nodejs using requirejs. Everything works fine, but I want to include a module just for the...
Read more >Loading (Requiring) a Module in Node.js - YouTube
Your browser can't play this video. ... Loading (Requiring) a Module in Node. js ... 29K views 4 years ago Node js Tutorials....
Read more >ES Modules in NodeJS and the Browser - YouTube
This tutorial explains how you can use ES Modules in either NodeJS or in the Browser along with some of the gotchas and...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
That new import path works great, thanks! I guess the example in the README is out-of-date.
You’re right, the other dependencies are from others in listed in my package.json.
I used
rm -r node_modules/@zxing && sync npm install @zxing/library@0.10.0 && sync & node f.js
to test each version, wheref.js
is:In v0.9.0 & v0.6.0, Node.js couldn’t find the module:
v0.5.1 is the most recent version that works. I also noticed that v0.5.1 is the first to have many (76) other dependencies in
node_modules/
.Edit: Looking at
git diff v0.5.1..v0.10.0 -- *webpack*
, it seems that thelibraryTarget: 'umd'
did not change… 😕