mljs / levenberg-marquardt

Curve fitting method in JavaScript
MIT License
70 stars 15 forks source link

import from dist directory #50

Open arpanda opened 2 years ago

arpanda commented 2 years ago

I have manually build it and trying to import it directly in the browser.

import {LM} from '/node_modules/ml-levenberg-marquardt/dist/ml-levenberg-marquardt.js'

and getting the below error.

Uncaught SyntaxError: The requested module '/node_modules/ml-levenberg-marquardt/dist/ml-levenberg-marquardt.js' does not provide an export named 'LM' 

Any suggestion to solve it?

jobo322 commented 2 years ago

Hello, could you try to import levenbergMarquardt like it is exports.

arpanda commented 2 years ago

I tried this

import {levenbergMarquardt} from '/node_modules/ml-levenberg-marquardt/dist/ml-levenberg-marquardt.js'

After that, I am calling via let fittedParams = levenbergMarquardt(data, Gaussian, options);

Still getting similar error message .i.e.,

Uncaught SyntaxError: The requested module '/node_modules/ml-levenberg-marquardt/dist/ml-levenberg-marquardt.js' does not provide an export named 'levenbergMarquardt'
jobo322 commented 2 years ago

I am not able to reproduce your error. I did

npm i, npm run tsc npm run build

with this, I am able to require levenbergMarquardt. Could you try like that?

are you calling this module from the browser using this path? do you serve your LM folder using live-server extension of VScode?

arpanda commented 2 years ago

The npm run tsc command, gave me this error.

npm run tsc

> ml-levenberg-marquardt@4.1.0 tsc
> npm run clean && npm run tsc-cjs && npm run tsc-esm

> ml-levenberg-marquardt@4.1.0 clean
> rimraf lib lib-esm

> ml-levenberg-marquardt@4.1.0 tsc-cjs
> tsc --project tsconfig.cjs.json

error TS5058: The specified path does not exist: 'tsconfig.cjs.json'.

the result of npm run build

npm run build

> ml-levenberg-marquardt@4.1.0 build
> cheminfo-build

Building bundle...
Building minified bundle...

I ran those commands and it created a dist directory (attached here) dist.zip.

Then started the server using npx http-server and here is the html file code.

<html>
    <body> 
        <script type="module">
            import {levenbergMarquardt} from '/node_modules/ml-levenberg-marquardt/dist/ml-levenberg-marquardt.js'
        </script>
    </body>

</html>

The possible reason probably the npm run tsc command didn't run successfully.

jobo322 commented 2 years ago

@arpanda do you have your repository updated, I was able to reproduce you error by deleting the file tsconfig.cjs.json.

the file inside of dist.zip has a levenbergMarquardt function exported, could you check the way you're importing the function works, for another packages

arpanda commented 2 years ago

Earlier, I installed via npm i ml-levenberg-marquardt. Probably, tsconfig.cjs.json is missing in npm repository.

I tried it again with git clone and gendered the dist build. The output files are some.

Now coming to the export issue, Yes, its working for the other packages. Some of those packages has export at the end of the file, something like export { function_name};

I see this export lines in dist/ml-levenberg-marquardt.js

    typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
    typeof define === 'function' && define.amd ? define(['exports'], factory) :
})(this, (function (exports) { 'use strict';
    exports.levenbergMarquardt = levenbergMarquardt;
    Object.defineProperty(exports, '__esModule', { value: true });
arpanda commented 2 years ago

@jobo322 Looks like the dist files are in UMD format and cheminfo-build command is creating those files.

is cheminfo-build command has some option to bundled it to browser only format? if not, is it possible to add another bundler tool?