How to fix unexpected token in ESLint? - npm

I install ESLint globally using the command npm install -g eslint on my Mac. It was successful installing, but when I run eslint -v this is the issue I encounter:
$ npm install -g eslint
path/to/bin/eslint -> path/to/lib/node_modules/eslint/bin/eslint.js
+ eslint#7.3.1
added 107 packages from 63 contributors in 4.823s
$ eslint -v
path/to/lib/node_modules/eslint/bin/eslint.js:93
} catch {
^
SyntaxError: Unexpected token {
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:617:28)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Function.Module.runMain (module.js:694:10)
at startup (bootstrap_node.js:204:16)
at bootstrap_node.js:625:3
I would like to know what are the missing steps that cause this issue? I'm using Node.js v8.16.2 and NPM v6.4.1.

The error happens because } catch { is a relatively recent (ES2019) language feature called "optional catch binding"; prior to its introduction, binding the caught error (e.g. } catch (err) {) was required syntactically. Per node.green, you need at least Node 10 to have that language feature.
So why does this happen in ESLint? Per e.g. the release blog, version 7 has dropped support for Node 8; they're no longer testing against that version and more modern language features will be assumed to be supported.
To fix it, either:
Upgrade Node (Node 8 is out of LTS, which is why ESLint dropped support); or
npm install eslint#6 (with -g if you want to install globally) to use the older version of ESLint with Node 8 support.

In case it's helpful for anyone, I had a slightly different twist on the other answer. In my case, the error was happening during the Travis CI build process and causing it to fail. The solution in my case was to update my .travis.yml file to node_js: "16"

Related

npm v9.4.0 is known not to run on Node.js v19.5.0

I somehow broke npm on my Windows system. Any (literally any) npm command I use gives me the following illogical error:
ERROR: npm v9.4.0 is known not to run on Node.js v19.5.0. You'll need to upgrade to a newer Node.js version in order to use this version of npm. This version of npm supports the following node versions: ^14.17.0 || ^16.13.0 || >=18.0.0. You can find the latest version at https://nodejs.org/.
ERROR: C:\Users\PCName\AppData\Roaming\npm\node_modules\npm\node_modules\#npmcli\arborist:1 ../../workspaces/arborist SyntaxError: Unexpected token '.' at internalCompileFunction (node:internal/vm:73:18) at wrapSafe (node:internal/modules/cjs/loader:1166:20) at Module._compile (node:internal/modules/cjs/loader:1210:27) at Module._extensions..js (node:internal/modules/cjs/loader:1300:10) at Module.load (node:internal/modules/cjs/loader:1103:32) at Module._load (node:internal/modules/cjs/loader:942:12) at Module.require (node:internal/modules/cjs/loader:1127:19) at require (node:internal/modules/helpers:112:18) at Object.<anonymous> (C:\Users\PCName\AppData\Roaming\npm\node_modules\npm\lib\npm.js:1:18) at Module._compile (node:internal/modules/cjs/loader:1246:14)
I tried uninstalling and re-install node.
I deleted the npm folder in C:\Users\PCName\AppData\Roaming\npm\node_modules, downloaded the latest version here: https://github.com/npm/cli/releases/tag/v9.4.0, extracted it, renamed it to 'npm', and copied it to the folder where I deleted it.
I fixed this on my machine by downloading the node installer from https://nodejs.org/en/download/ I did not even bother to uninstall my existing node.
Caveat: After doing this, nvm stopped working. When I do nvm use XXXX it says that it switched versions but it did not.

React install problems with npm

When I run npx create-react-app, I had these errors. Maybe someone knows whats it's wrong? P.S. also had tried npm-install -g create-react-app.
C:\Users\Deividas\Desktop\PAMEGINIMAS>create-react-app my-app
internal/modules/cjs/loader.js:621
throw e;
^
Error: No valid exports main found for 'C:\Users\Deividas\AppData\Roaming\npm\node_modules\create-react-app\node_modules\is-promise'
at resolveExportsTarget (internal/modules/cjs/loader.js:618:9)
at applyExports (internal/modules/cjs/loader.js:499:14)
at resolveExports (internal/modules/cjs/loader.js:548:12)
at Function.Module._findPath (internal/modules/cjs/loader.js:650:22)
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:948:27)
at Function.Module._load (internal/modules/cjs/loader.js:854:27)
at Module.require (internal/modules/cjs/loader.js:1023:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (C:\Users\Deividas\AppData\Roaming\npm\node_modules\create-react-app\node_modules\run-async\index.js:3:17)
at Module._compile (internal/modules/cjs/loader.js:1128:30) {
code: 'MODULE_NOT_FOUND'
}
This seems to be a recent problem with npm. There is an issue open as of the last few hours and it seems like people are working on it. I don't think it's anything to do with yourproject.
The issue
While the issue is being fixed: try installing node version 12.12.0 as shown below (if using nvm) and run create-react-app again.
nvm install 12.12.0
nvm use 12.12.0
I'd try to clear your cache npm cache clean --force. What version of node and npm are you using?

internal/modules/cjs/loader.js:584 issue

I got a problem when I input any npm cmd, it says
internal/modules/cjs/loader.js:584
Operating System: Windows 10 64bit
What shall I do to solve this issue?
internal/modules/cjs/loader.js:584
throw err;
^
Error: Cannot find module 'E:\FED\node_modules\npm\bin\npm-cli.js'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)
at Function.Module._load (internal/modules/cjs/loader.js:508:25)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
For many people on Mac (not sure about Windows/Linux), the solution is:
Delete node_modules and package-lock.json, then run $ npm install again.
You can also try updating your version of node.
Check version with $ node -v,
install with $ sudo n stable
I was having a similar error but it was because I hadn't had my node_modules(the ones you deleted ) file open in the file I was working on, even though I had run npm Install.
my Error was
Cannot find module _resolveFilename
If you build and deploy another place at a later time for example in an docker image, it may help to bundle the dependencies that the loader cannot find in package.json. Then the packaged are bundled and the loader can find them.
"bundledDependencies": [ "express", "express-http-proxy", ],
"dependencies": { ...}
Before starting any project you have to write this command npm init --yes

"react-native start" command gives error

react-native start command gives error.Genrated error is given below. Please help me.
react-native start
/usr/lib/node_modules/react-native/local-cli/server/checkNodeVersion.js:43
);
^
SyntaxError: Unexpected token )
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:549:28)
at Object.Module._extensions..js (module.js:586:10)
at Module.load (module.js:494:32)
at tryModuleLoad (module.js:453:12)
at Function.Module._load (module.js:445:3)
at Module.require (module.js:504:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/usr/lib/node_modules/react-native/local-cli/cli.js:17:1)
I found out that if you have installed nvm to manage different node versions, is showing one version but using another from behind, I also have problems with my global brew node installation, so I had reinstalled again from scratch. As a general tip, unload the nvm and work with the npm global installation.
Update your nodejs should fix
sudo npm cache clean -f (force) clear you npm cache
sudo npm install -g n install "n" (this might take a while)
sudo n stable upgrade to lastest version
I checked the file "/usr/lib/node_modules/react-native/local-cli/server/checkNodeVersion.js:43" and found that "React Native runs on Node 8.0 or newer."
But my node vesion was 6.0 ,so that the problem.
The error belongs with node js version.
node --v in cmd will show version less than 8.
Update your node version by installing nvm. This will help you update version.
https://stackoverflow.com/a/7718438/9090200
In-case you are using nvm to install a new version of node.
It's important that you will use nvm alias default node so it will be the default version while opening new shells.
React Native runs on Node 8.0 or newer
If you are using nvm then set "nvm alias default >=8.X.X" like "nvm alias default 8.0.0"
Re-open your terminal and try it again.

karma start Cannot find module 'jasmine-core'

I was getting the following error when I ran "karma start"
module.js:340
throw err;
^
Error: Cannot find module 'jasmine-core'
at Function.Module._resolveFilename (module.js:338:15)
at Function.require.resolve (module.js:384:19)
at initJasmine (/usr/lib/node_modules/karma-jasmine/lib/index.js:8:42)
at Array.invoke [as 0] (/usr/lib/node_modules/karma/node_modules/di/lib/injector.js:75:15)
at get (/usr/lib/node_modules/karma/node_modules/di/lib/injector.js:48:43)
at /usr/lib/node_modules/karma/lib/server.js:137:20
at Array.forEach (native)
at Server._start (/usr/lib/node_modules/karma/lib/server.js:136:21)
at invoke (/usr/lib/node_modules/karma/node_modules/di/lib/injector.js:75:15)
at Server.start (/usr/lib/node_modules/karma/lib/server.js:101:18)
at Object.exports.run (/usr/lib/node_modules/karma/lib/cli.js:231:26)
at Object. (/usr/lib/node_modules/karma/bin/karma:3:23)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
Does anyone know why this is happening? I was running karma in a VM. I tried several npm install commands and nothing worked. After I shutdown my machine for the night and came back the next day the problem went away and everything is working again. So, I'm not blocked anymore.. Anyone have some thoughts on what could be happening?
I solved using npm install jasmine-core --save-dev
try install jasmine-core globally (Jasmine on NPM).
use npm install -g jasmine-core
Even with karma and karma-jasmine installed in the local project's node_modules you must still npm install jasmine.
So for a fresh Karma project:
npm install karma karma-jasmine jasmine to install the required modules locally.
Optionally add --save-dev to write to your package.json.
Note these do not need to be -g globally installed here.
karma start
This step requires you to have Karma installed globally (sudo npm install -g karma).
Navigate to folder:
cd node_modules\karma-jasmine
Run:
npm install
This should download the required dependencies and fix the issue.
Also, it is important to know the installation path where do you have karma.conf.js file.
I have this problem, and have tried every suggestion on the page without it fixing the problem. Well, I haven't yet rebooted, so I'll try that next, but I have closed my shell (git-bash) and re-opened it in case it was an environment variable problem. Something is wrong here and I don't think the problem has been identified yet.