I have a JavaScript project where the frontend uses ES Modules (built with babel / webpack) and the backend uses CommonJS modules. All files use the .js extension. Given this mix and the file extension convention, I can't set "type": "module" in my project's package.json.
I'm using AVA to run unit tests across the frontend and backend. Is there a way to run the frontend tests as ES Modules and the backend tests as CommonJS modules?
Related
I'm currently rewriting some internal packages as ECMAScript modules. Unfortunately, I depend on some external packages that use esm syntax but .js file endings. Of course, they have neither "exports" nor "type": "module" defined in their package.json. I cannot update these external dependencies to new versions.
How can I tell node.js to treat these .js files as esm modules?
I have a UI framework that is maintained by my company. and This library doesn't provide cjm module. instead only provide esm module.
And I aware that this library is not failed to be imported from the test condition running by jest.
through some gogooling, I found that the Jest doesn't support ESM module import, and it is only on experimental stage to support esm module on Jest(https://jestjs.io/docs/ecmascript-modules). So I tried to make my library as cjs module version, and test againg. This is perfectly works. So I convinced that the previous bug was caused because Jest doesn't support esm module import.
But at this point, I wonder something below.
If Jest don't support ESM, every library should have cjs module regardless of providing esm module??
Jest always find only cjs module from libraries?
How React test works on jest? even thought every react component and other modules are ESM? This is why we need react-test-renderer or babel-jest as dependencies for the react test?
Finally, There is some resources that I can find some information about this subject??
For me, Webpack is a bundler for "web" since browsers don't have good support for js module system. Thus using Webpack we could still use "module" way to develop and pack them for browsers.
Webpack has a config option target
module.exports = {
target: 'node'
};
using node webpack will compile for usage in a Node.js-like environment
But NodeJS already support CommonJS modules, why need Webpack to build something that runs in NodeJS environment?
Besides, if you want to compile your javascript, for example, using the ES module, you could just use babel to do so, using es module transform plugins or proper presets.
Why use Webpack, set the target to node and then use babel loader... instead of using babel directly?
I cannot think of the use case of using Webpack to bundle application running in node.
One reason I can think of when to use webpack for node is when you have a node_modules package where it's not compiled yet (Still in ES6).
babel-node won't be able to help you bundle the node_modules packages that need to be transpiled along with the rest of your code
I had to go through this process.. ): This scenario is helpful in Yarn Workspaces where you want your server to depend on another package in your workspace. Where your server will re-update whenever you make changes in the other package with the help of webpack
How to include a few node_modules package in babel-node
Having a single server.js output
instant deployable on a server
no worry about mismatching dependencies compared to your dev environment.
Since 1.1 Kotlin has support for compiling code to JS. I want to find example of gradle project how to share some code (for example data-models) between js and jvm platforms. Ideally project should have this structure:
common module (compiles to .js and .class)
backend module (jvm)
frontend module (js, managed by webpack)
i'm authoring some modules for npm in umd format -- they're for node and the browser
i'm using requirejs to load the modules clientside to work on them
but requirejs can't find modules within node_modules -- getting 404's for the module identifiers at the web root
what configuration do i need to give requirejs so it will consult node_modules by default?