question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Node.js: Loading module as if in a browser

See original GitHub issue

When 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:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mattmahncommented, Jan 30, 2019

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.

1reaction
mattmahncommented, Jan 29, 2019

I used rm -r node_modules/@zxing && sync npm install @zxing/library@0.10.0 && sync & node f.js to test each version, where f.js is:

require('@zxing/library')

In v0.9.0 & v0.6.0, Node.js couldn’t find the module:

internal/modules/cjs/loader.js:583
    throw err;
    ^

Error: Cannot find module '@zxing/library'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
    at Function.Module._load (internal/modules/cjs/loader.js:507:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (/Users/mah7788/code/ms-barcodes/f.js:1:63)
    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)

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 the libraryTarget: 'umd' did not change… 😕

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found