search envelope-o feed check
Home Unanswered Active Tags New Question
user comment-o

NPM javascript throws error "Uncaught ReferenceError: module is not defined"

Asked by Danijel
1 year ago.

Hello,

We have an ASP.NET Core app (without angular, react or vue).
We are pulling the file https://npm.daypilot.org/daypilot-pro-javascript/***/2023.1.5488.tar.gz to be used in our local npm repo.

in our package.json we have the following (other dependencies were removed for this post):
{
  "version": "1.0.0",
  "name": "asp.net",
  "private": true,
  "dependencies": {
    "daypilot-pro-javascript": "^2023.1.5488"
  }
}

in our bundleconfig.json we have this configuration:

  {
    "outputFileName": "wwwroot/js/bundle-daypilot.js",
    "inputFiles": [
      "node_modules/daypilot-pro-javascript/daypilot-javascript.min.js"
    ]
  },

in our Index.xshtml view we are using this line to load the JavaScript:

<script src="~/js/bundle-daypilot.js" asp-append-version="true"></script>

but on loading the page we get a javascript error:

bundle-daypilot.js?v=GvIg2_avHtwOVk9WM8w3DdafmP7bc-buTMWg2sVbWq0:37 Uncaught ReferenceError: module is not defined
    at bundle-daypilot.js?v=GvIg2_avHtwOVk9WM8w3DdafmP7bc-buTMWg2sVbWq0:37:13
    at bundle-daypilot.js?v=GvIg2_avHtwOVk9WM8w3DdafmP7bc-buTMWg2sVbWq0:37:48

If we compare the file that we manually download and the one that we get by npm link,
the script from npm has this line extra:

!function(){module.exports={DayPilot:DayPilot}}();

Question: How do we use the npm package in our project if we pull the javascript from npm?

Comment posted by Danijel
1 year ago.

replacing the content from daypilot-all.min.js works, but still we need to know how to use it over npm.

Answer posted by Dan Letecky [DayPilot]
1 year ago.

The JavaScript NPM package (daypilot-pro-javascript) defines a CommonJS module which can't be used directly in the browser. The purpose is to allow using DayPilot in a Node project.

In the browser, you can load a CommonJS module using require.js (https://requirejs.org/).

You can also create your own package from daypilot-all.min.js from the zip download.

Comment posted by Danijel
1 year ago.

A check for whether module is undefined would allow us to run your code in a Node context, but also with pure browser-side JS.

Something like this:

if (typeof module === 'object') {
    module.exports = ...;
}

Is there an example how to load this with RequireJS?

Comment posted by Dan Letecky [DayPilot]
1 year ago.

In the latest sandbox build (2023.1.5506), the module registration is now conditional:
https://release.daypilot.org/changes/js/

You can also work around this issue (with the current version) by declaring a "module" object in the global context:

var module = {};

That would be easier than using RequireJS.

Comment posted by Danijel
1 year ago.

Thank you, we will wait for the 2023.1.5506 release as it requires no further changes to our current app.

This question is more than 1 months old and has been closed. Please create a new question if you have anything to add.