Cannot serve React app in NX-jsx is not currently enabled - javascript

I created a react app in a NX workspace using nx g #nrwl/react:application --js --unitTestRunner=none --e2eTestRunner=none my-project
Then I developed my project. Finally when I want to run my project using
nx serve my-project I get following error:
Syntax error: Support for the experimental syntax 'jsx' isn't currently enabled
I realized that it is related to babel configuration of project so I
installed #babel/preset-react #babel/preset-env and replaced content of
.babelrc file
configuration before any change:
{
"presets": [
[
"#nrwl/react/babel",
{
"runtime": "automatic",
"importSource": "#emotion/react"
}
]
],
"plugins": ["#emotion/babel-plugin"]
}
.babelrc after installing packages
{
"presets": [
"#babel/preset-env",
"#babel/preset-react"
]
}
I also changed jsx property in compilerOptions of tsconfig.json from
"react-jsx" to "react"
but when I run nx serve my-project again, I get that error again.
What should I do? Is there any solution?

The issue is the lib generator, when you create a react lib ,I only tried publishable, it doesnt add the babelrc file so you have to add it yourself..just copy it from your app. Its a bug atm

Related

Yarn won't publish/add both ESM and CJS versions of package?

We have an application bundled with Parcel that consumes a UI library (react components). The UI library is bundled with Rollup and published as a private package on NPM.
I've been trying to migrate our application to use Parcel 2, but Parcel complains that it cannot locate the ESM version of the UI library within the dist folder. Sure enough, when I check my node_modules directory, the dist folder for the UI lib includes only one file: index.cjs.js.
The weird part is that the UI lib is set up to build in both CJS and ESM formats with sourcemaps. When I build the project locally, Rollup produces both CJS and ESM files and their sourcemaps: index.cjs.js, index.cjs.js.map, index.esm.js, and index.esm.js.map. So, somehow, it seems that either: (1) Yarn is only publishing the CJS version of the library to NPM or (2) When the UI lib is added to the application, only the CJS version is being built. Neither of those situations makes sense to me.
Here's the relevant sections of our package.json and rollup.config.js files:
{
"name": "#thecb/components",
"version": "4.0.23",
"description": "Common lib for CityBase react components",
"main": "dist/index.cjs.js",
"module": "dist/index.esm.js",
"source": "src/index.js",
"scripts": {
"storybook": "start-storybook",
"build": "rollup -cm"
},
import resolve from "rollup-plugin-node-resolve";
import commonjs from "rollup-plugin-commonjs";
import babel from "rollup-plugin-babel";
import json from "rollup-plugin-json";
import visualizer from "rollup-plugin-visualizer";
import pkg from "./package.json";
import * as formattedInput from "formatted-input";
const globals = {
react: "React",
"react-dom": "ReactDOM"
};
const plugins = [
resolve({ preferBuiltins: false }),
babel({
exclude: "node_modules/**",
presets: ["#babel/env", "#babel/preset-react"]
}),
json(),
commonjs({
include: "node_modules/**",
namedExports: {
"formatted-input": Object.keys(formattedInput)
}
}),
visualizer()
];
const external = [...Object.keys(pkg.peerDependencies || {})];
const output_data = [
{
file: pkg.main,
format: "cjs"
},
{
file: pkg.module,
format: "esm"
}
];
export default output_data.map(({ file, format }) => ({
input: "src/index.js",
output: {
file,
format,
globals
},
plugins,
external
}));
Anyone have any idea why the ESM version of this lib either wouldn't be published or installed?
Late reply but I ran into something very similar today. Not using rollup but rather tsc directly to output dist/cjs/* and dist/esm/*.
I found that my build process locally produced both outputs but the tarball produced by yarn publish only contained dist/cjs/*. TBH I'm not sure why; my current theory is that yarn is somehow using the "main": "dist/cjs/index.js" and inferring some defaults for package inclusion from that.
What I can tell you is that by adding "files": "dist" to my package.json I got yarn to behave itself and add both dist/cjs/* and dist/esm/* to the package tarball as I initially expected.
https://docs.npmjs.com/cli/v7/configuring-npm/package-json#files

ESlint and typescript integration issues

I have the following problem I am trying to integrate typescript with ESlint, I have done it by installing eslint package with some additional packages basing mainly on a tutorial (
tutorial if it turns out to be helpful I can paste a link.Then after using eslint --init command I generated a file .eslintrc, where had all my configurations included. My goal is to have underline in my VScode Editor using Eslint. As a extra info when I use eslint --fix command it returns errors/warnings so its only underlining
I have typescript installed locally also tried globally, all necessary plugins also, deleting node_modules/checking versions of packages - don't have an idea what's wrong here. Would be very grateful for even some insights. I am using Mac pro with Mojave 10.14.6
Regards!
.eslintrc.json:
{
"env": {
"es6": true,
"node": true
},
"extends": [
"airbnb-base",
"plugin:#typescript-eslint/recommended"
],
"parser": "#typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module",
"jsx": true
},
"plugins": [
"#typescript-eslint"
],
"rules": {}
}
Unfortunately when Eslint starts still gets this kind of error in terminal output tab:
[Error - 11:44:47 PM] Failed to load plugin '#typescript-eslint'
declared in 'project-name'/.eslintrc.json': Cannot find module
'typescript' Referenced from:
/Users/MyUser/Desktop/project-name/project-name-backend/.eslintrc.json
Have you tried installing the missing module alone? Try re-installing the packages giving the issues as they may have been corrupted. You could also run npm audit to repair corrupted files.

Specify node version for eslint

I'm using eslint for a Node.js application. I'm running node version v10.6.0, but when I run eslint in the root directory of my project, I get error messages like the following:
The 'URL' is not supported until Node.js 10.0.0. The configured version range is '>=6.0.0'
My .eslintrc.json looks like this:
{
"extends": [
"eslint:recommended",
"plugin:node/recommended",
],
"parserOptions": {
"sourceType": "module"
}
}
and my package.json contains:
"engine": {
"node": ">=10.6.0"
}
How can I tell eslint that I'm using a newer node version? I'm using eslint v5.2.0.
The engine property in the package.json file is incorrect and should be engines instead. This is the property that eslint uses to determine the node version.

Unknown plugin "static-fs" specified in .babelrc?

I am trying to run the exhibits in deck.gl project. I have cloned the repo, switched to the exhibits directory, and run:
npm run browserify
This installs the packages without errors, and runs the project, but when the browser opens I see the following error:
Unknown plugin "static-fs" specified in "/Users/me/Projects/deck.gl/.babelrc" at 0,
attempted to resolve relative to "/Users/me/Projects/deck.gl"
while parsing file: /Users/me/Projectss/deck.gl/exhibits/app.js
So the problem is the .babelrc file:
{
"presets": ["es2015", "stage-2", "react"],
"plugins": [
"static-fs",
"transform-decorators-legacy"
]
}
What should I do? Google isn't suggesting any answers, and static-fs doesn't appear to be a package.
In addition to the package.json in the exhibits/browserify directory,
the deck.gl project includes a package.json at the project's root.
Note that there are packages in the root file that are not in the exhibits/browserify file.
It's necessary to run npm install at the root, too, as the 'missing' dependencies will resolve back to the root node_modules directory.

Babel not compiling ES6 to ES5 Javascript [duplicate]

I have this code:
"use strict";
import browserSync from "browser-sync";
import httpProxy from "http-proxy";
let proxy = httpProxy.createProxyServer({});
and I have installed babel-core and babel-cli globally via npm. The point is when I try to compile with this on my terminal:
babel proxy.js --out-file proxified.js
The output file gets copied instead of compiled (I mean, it's the same as the source file).
What am I missing here?
Babel is a transformation framework. Pre-6.x, it enabled certain transformations by default, but with the increased usage of Node versions which natively support many ES6 features, it has become much more important that things be configurable. By default, Babel 6.x does not perform any transformations. You need to tell it what transformations to run:
npm install babel-preset-env
and run
babel --presets env proxy.js --out-file proxified.js
or create a .babelrc file containing
{
"presets": [
"env"
]
}
and run it just like you were before.
env in this case is a preset which basically says to compile all standard ES* behavior to ES5. If you are using Node versions that support some ES6, you may want to consider doing
{
"presets": [
["env", { "targets": { "node": "true" } }],
]
}
to tell the preset to only process things that are not supported by your Node version. You can also include browser versions in your targets if you need browser support.
Most of these answers are obsolete. #babel/preset-env and "#babel/preset-react are what you need (as of July 2019).
I had the same problem with a different cause:
The code I was trying to load was not under the package directory, and Babel does not default to transpiling outside the package directory.
I solved it by moving the imported code, but perhaps I could have also used some inclusion statement in the Babel configuration.
First ensure you have the following node modules:
npm i -D webpack babel-core babel-preset-es2015 babel-preset-stage-2 babel-loader
Next, add this to your Webpack config file (webpack.config.js) :
// webpack.config.js
...
module : {
loaders : [
{
test : /\.js$/,
loader : 'babel',
exclude : /node_modules/,
options : {
presets : [ 'es2015', 'stage-2' ] // stage-2 if required
}
}
]
}
...
References:
https://gist.github.com/Couto/6c6164c24ae031bff935
https://github.com/babel/babel-loader/issues/214
Good Luck!
As of 2020, Jan:
STEP 1: Install the Babel presets:
yarn add -D #babel/preset-env #babel/preset-react
STEP 2: Create a file: babelrc.js and add the presets:
module.exports = {
// ...
presets: ["#babel/preset-env", "#babel/preset-react"],
// ...
}
STEP 3:- Install the babel-loader:
yarn add -D babel-loader
STEP 4:- Add the loader config in your webpack.config.js:
{
//...
module: [
rules: [
test: /\.(js|mjs|jsx|ts|tsx)$/,
loader: require.resolve('babel-loader')
]
]
//...
}
Good Luck...
npm install --save-dev babel-preset-node5
npm install --save-dev babel-preset-react
...and then creating a .babelrc with the presets:
{
"presets": [
"node5",
"react"
]
}
...resolved a very similar issue for me, with babel 3.8.6, and node v5.10.1
https://www.npmjs.com/package/babel-preset-node5
https://www.npmjs.com/package/babel-preset-react
Same error, different cause:
Transpiling had worked before and then suddenly stopped working, with files simply being copied as is.
Turns out I opened the .babelrc at some point and Windows decided to append .txt to the filename. Now that .babelrc.txt wasn't recognized by babel. Removing the .txt suffix fixed that.
fix your .babelrc
{
"presets": [
"react",
"ES2015"
]
}
In year 2018:
Install following packages if you haven't yet:
npm install babel-loader babel-preset-react
webpack.config.js
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: {
presets: ['es2015','react'] // <--- !`react` must be part of presets!
}
}
],
}
Ultimate solution
I wasted 3 days on this
import react from 'react' unexpected identifier
I tried modifying webpack.config.js and package.json files, and adding .babelrc, installing & updating packages via npm, I've visited many, many pages but nothing has worked.
What worked? Two words: npm start. That's right.
run the
npm start
command in the terminal to launch a local server
...
(mind that it might not work straight away but perhaps only after you do some work on npm because before trying this out I had deleted all the changes in those files and it worked, so after you're really done, treat it as your last resort)
I found that info on this neat page. It's in Polish but feel free to use Google translate on it.

Categories