I'm relatively new to modern Javascript dependency management. I'm writing a web app in Typescript and have started to use Jasmine as a unit test framework.
Once I started using Jasmine it became clear I needed to start exporting/importing the classes and functions in my source code files, since there is no HTML page loading all of them via script tags. This has been fine for my own files, but I am using a third-party library that is provided minified:
https://currency.js.org/
https://unpkg.com/currency.js#1.2.2/dist/currency.min.js
I can't seem to get my code running via Jasmine to recognize the existence of the currency function defined in this file. I'm guessing it's because the function seems to be dynamically created.
What would be a proper way to export the currency function above for use in my own modules?
That package actually is published on npm (with TypeScript typings included), and I was able to successfully import it as a module in a test application:
npm install currency.js
Then in your code:
import currency from "currency.js"
console.log(currency(1.23).add(.01).format());
https://www.npmjs.com/package/currency.js
Related
I have a simple javascript function in an example.js file placed inside the node js public directory.
I am using jest for unit testing.
The problem is if I write the following in the example.js javascript file in the public folder:
module.exports.myFunction = myFunction;
Jest test file is able to import it, using require() and perform tests, however when I run the web application, the browser complains when I service the page containing this javascript:
Uncaught ReferenceError: module is not defined
What is the correct way to test javascript files in the public directory of the node application?
Using export and/or import in the project is reported an being unrecognized and results in errors as well.
How is this done?
As mentioned in comments require(...) and module.exports relate to Common JS Modules, which are natively supported by NodeJS runtime, but not by browser. So basically you'll need to add extra build configuration to have your module work in both runtimes.
If you want to have outputs in both CommonJs and Browser friendly bundle - you can write all code in ES Modules and use build tools like webpack to provide outputs in different formats.
Also, starting from Node 13.2.0 - it supports ES modules natively. So I would stick to ES modules for ongoing development anyway.
Please also check this short article on main JS module format differences.
I'm developing a library in TypeScript.
This library uses the https://github.com/Simonwep/pickr library.
I would like to make my library available so users can use it, but question is: do I need to bundle pickr library or just put a reference in the package.json?
I tried to use the library in a sample project and in dev mode all works since it loads from node_modules, but when I build the project and try to load it fails to load it.
It works only by importing the library using
<script src="https://cdn.jsdelivr.net/npm/#simonwep/pickr/dist/pickr.min.js"></script>
If library will be used in a web browser I made it so the script tag is automatically added.
But what if someone will use the library in an ionic project for instance which will run on a tablet?
In this case Pickr library needs to bundled in the final build.
Is this an automatic process? What's the correct way of using a third-party library in this case?
If you want pickr to get bundled with your code you will have to
List it under dependencies
Import it in your code (see pickr docs)
If its not imported in your code it wont be bundled.
If you want to tell the user to manually add pickr when he uses your module you can list pickr as a peerDependency in your package.json
I am working on a project which has a lot of client side javascript files
none of these files have any module.export.XXX statements
I understand that jasmine has a stand-alone version which uses a html file to run the tests and a node.js version which uses the cli to run the tests.
When using the stand-alone version of jasmine i can import the js files i want to test with tags, no problem.
When using the node.js version i cannot use import statements because none of the files that i wish to test have exports.
I am very keen on testing on the command line so that running the tests with our continuous integration package is easier.
I have attempted to solve this using this guide
https://www.nfriedly.com/techblog/2013/02/automatically-unit-testing-client-side-javascript-with-jasmine-and-node-js/
however it is very out of date (the jasmine folder structure is different) and i could not fix the JDOM issue which is mentioned.
Is there any way to make this work other than adding export statements?
if no
will adding export statements cause issues in any browsers?
and
most of the existing files are a wrapper functions with all of the functions and other components (variables, event listeners ect) inside it. I intend to call specific individual functions found in the files (not the wrapper functions) from the tests. will this be possible without using prototypes?
will i need to change the architecture
It would be nice if one can run node.js code inside Excel user-defined functions. Something like using js code like VBA.
I googled for solutions but cannot find any. Is it possible to do this?
Yes, if you want to use packages from NPM. You could use webpack to combine all the stuff to one js file, it should work.
webpack as a static module bundler for modern JavaScript applications. When webpack processes your application, it internally builds a dependency graph which maps every module your project needs and generates one or more bundles. you could refers to this document.
You could also refer to a sample, Yeoman, the Yeoman generator creates a Node.js Office Add-in project. it use webpack combine all files into one js file.
I am trying to implement elasticsearch.js in my project and when I added:
var elasticsearch = require('elasticsearch');
It broke my project and said require is not defined. I did research and saw that I would have to use a library called require.js within my project but that is changing my whole project structure just for one script.
I wanted to see if anybody knows how to call an instance without using require:
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client();
You seem to be following the instructions for using elasticsearch in a node project or using a bundling system that supports CJS modules (like browserify or webpack). If you want a script that's for a browser-only project, see the Browser Builds page.
Note that at this time, they have this note:
These versions of the client are currently experimental.
You're using a version that should be used in a node project or through a module loader/bundler. The require keyword is node specific, the browser has no idea what to do with it. Require.js would help, you can also install Rollup or Webpack which would bundle the CJS (require) dependencies and your code into one file.
Or to be simple just go to https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/browser-builds.html as Jacob said