Installing using the NPM method in a NodeJS environment.

Installing using the NPM method in a NodeJS environment.

MarkAndersonUKMarkAndersonUK Posts: 44Questions: 17Answers: 0
edited January 2022 in DataTables

Hi,

I am trying to install the DataTables, using the NPM method described in the manual.

The NPM installation works fine, and I am able to install the software into the 'node_modules' without any issues. However, when I attempt to use the software in my code using the example given in the documentation :

var $       = require( 'jquery' );
var dt      = require( 'datatables.net' )();

I am getting an error :

ReferenceError: window is not defined
    at module.exports (C:\SiteRoots\compass_admin\node_modules\datatables.net\js\jquery.dataTables.js:42:5)
    at Object.<anonymous> (C:\SiteRoots\compass_admin\routes\contacts.js:6:42)
    at Module._compile (internal/modules/cjs/loader.js:959:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
    at Module.load (internal/modules/cjs/loader.js:815:32)
    at Function.Module._load (internal/modules/cjs/loader.js:727:14)
    at Module.require (internal/modules/cjs/loader.js:852:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (C:\SiteRoots\compass_admin\routes\index.js:13:23)
    at Module._compile (internal/modules/cjs/loader.js:959:30)

Process finished with exit code 1

I understand this may be caused by the window object not being available in NodeJS, but is there some kind of workaround so I can use the GitHub NPM version of the software? I want to use this version as it includes bug fixes that have not yet been deployed into the release version of the datatable software.

Any ideas or help would be most appreciated. THANKS

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    I'm not sure if this is relevant, but it's the same error with a solution so may be useful,

    Colin

  • MarkAndersonUKMarkAndersonUK Posts: 44Questions: 17Answers: 0

    Thanks @colin , it is the same error, and I expect for the same reasons, ie
    that the code is looking for a 'window' object that doesn't exist in NodeJS. The suggestion there is to block off any code that isn't running on the client machine, however, I can't see how that can be done in Datatables when using Serverside options and NodeJS. I will try and see it I can make that solution work and will report how I get on.. I just hope there is a more elegant solution, as I want to load the Editor the same way and I can see it getting overly complex.

    thanks

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Yes, DataTables itself (and its extensions such as Editor, etc) wouldn’t run in NodeJS - they are browser libraries (unless you are doing server-side rendering or something I suppose - for which would need a virtual DOM anyway).

    Are you building a client-side file of Javascript - perhaps using Webpack, ESBuild or similar? Normally you would have an entry point for the Node process (the back-end) and one for the browser (the front-end) and they would be separate (although possibly sharing libraries).

    Allan

  • MarkAndersonUKMarkAndersonUK Posts: 44Questions: 17Answers: 0

    hi @allan,

    Actually, Datatables runs very well in nodeJS both, serverside and non-serverside,
    when installed using the Non-NPM method using the NodeJS libraries that datatables provides. The question is how to install them using the NPM method. I recently found a bug in the NodeJS version of Editor, and a fix was supplied but it is only available in the GitHub, which appears to require that I install using the NPM method, so I can grab the fixes.

    In my case I am using serverside. I am 100% sure that it does have to do with the lack of browser libraries, as that is the error I get. I really want to make use of the fixes that were provided in the GitHub. Perhaps I am mistaken and there is another way to load the fixes without using NPM. I haven't been able to locate any info on the matter. The fixes created changes to the .TS files, but the standard install method in NodeJS doesn't appear to use .TS files, it only has the .JS files. The NPM version does install the .TS files, which is why I have been trying to install via NPM.

    thanks for your help so far. I am going to continue to play around with it, and try and get it working..

    regards
    Mark

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    The npm install will use the latest release, unless you give it a specific git commit hash for the version you want to pull in. See this SO question on that topic.

    Beyond that, perhaps you could create a minimal repo showing the issue so I can clone that and debug the issue directly?

    Thanks,
    Allan

  • MarkAndersonUKMarkAndersonUK Posts: 44Questions: 17Answers: 0
    edited January 2022

    Hi @allan,

    The fixes to the code I am wanting to run have already been added to GitHub by @sandy, (ref DD-2424) . I am eager to use these, but was not able to figure out how to load the fixes into NodeJS environment. My initial install was performed using the downloadable NodeJS package (v205) I can wait for the next package release, which will include the fixes, but I was trying to learn how to use the GitHub fixes in the NodeJS environment.. Perhaps it is not possible and I just need to be patient. I am a novice compared to most people here, but doing is learning, so I was trying to do it.. Thanks for all your help.

    Mark

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    I'll hopefully drop the next release of Editor tomorrow or Wednesday :)

    Allan

Sign In or Register to comment.