question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JSX Loader for node esm experimental loaders

See original GitHub issue

Feature Request

I’ve been trying to use node.js new esm loader by adding "type": "module", in package.json and running script using node --es-module-specifier-resolution=node myScript.js

The script includes some import to React component :

import MyReactComponent from '../components/MyReactComponent';

Yet I get the following exception :

(node:11964) ExperimentalWarning: The ESM module loader is experimental. file:///D:/…/components/MyReactComponent.js:58 <div className="col-lg-6 col-md-12 ^ SyntaxError: Unexpected token ‘<’ at Loader.moduleStrategy (internal/modules/esm/translators.js:122:18)

Untill now I was able to use :

require('@babel/register')({
    presets: [
        ['@babel/env', { targets: { node: true } }],
        '@babel/react',
    ],
});

But now, with the new node esm loader, require does not exist and its irrelevant. How can I load JSX loader while using nodejs ES Modules ?

Node.js does say they support loaders : https://nodejs.org/api/esm.html#esm_experimental_loaders with --experimental-loader ./loader-name.mjs

But I could not find one for React.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
joeldenningcommented, Oct 12, 2020

I have created an ESM NodeJS loader for babel at https://github.com/node-loader/node-loader-babel.

I think the best approach would be to add a similar implementation to @babel/node. @nicolo-ribaudo would you be open to a pull request adding this to @babel/node?

1reaction
callumgarecommented, Mar 22, 2021

@JLHwung Ah, bummer. Looks like I misunderstood what ESM loaders meant. Back to waiting I guess.

Read more comments on GitHub >

github_iconTop Results From Across the Web

@mdx-js/node-loader | MDX
This package is a Node ESM loader to support MDX. ESM loaders are an experimental feature in Node, slated to change. They let...
Read more >
esbuild-esm-loader - npm
ESBuild ESM Loader is a custom resolver implementation for the experimental loader hooks feature of Node.js ECMAScript modules.
Read more >
How to transpile ES modules with webpack and Node.js
ESM comes with a transpiler loader by default that converts code from sources the Node runtime does not understand into plain JS using...
Read more >
ECMAScript modules | Node.js v19.3.0 Documentation
Add support for chaining loaders. ... Add support for import assertions. ... Consolidate loader hooks, removed getFormat , getSource , transformSource , and ......
Read more >
Custom ESM loaders: Who, what, when, where, why, how ...
If you want to write your own TypeScript loader, the Node.js Loaders team have put together a simple example: nodejs/loaders-test/typescript- ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found