Eslint with node.js - Parsing error: Assigning to rvalue - node.js

I'm trying to use a JS linter for the first time and have chosen eslint to test some AWS Lambda functions written in NodeJS. I get an immediate error that has me stumped.
module.exports.replyGet = ( event, context, callback ) => {
Generates the following error
14:28 error Parsing error: Assigning to rvalue
My .eslintrc.js reads as follows:
module.exports = {
"rules": {
"no-console":0
},
"extends": "eslint:recommended"
};
Can anybody advise a novice on how best to fix the error?
Many thanks

Add this to your eslint config file
"env": {
"es6": true,
"node": true
}

Related

getting Invalid semver string: "undefined"

Going through a Udemy tutorial on AWS CDK and Typescript. The video is about two years old and the instructor had just started using the new 2.0.0 release of the CDK. So, that may be part of my problem. I'm getting the below error when I run CDK synth --verbose
Invalid semver string: "undefined"
[21:30:22] Error: Invalid semver string: "undefined"
at parseVersion (C:\Users\DREWA\AppData\Roaming\npm\node_modules\aws-cdk\lib\index.js:31:8255000)
at Function.validate (C:\Users\DREWA\AppData\Roaming\npm\node_modules\aws-cdk\lib\index.js:31:8255125)
at Function.loadManifest (C:\Users\DREWA\AppData\Roaming\npm\node_modules\aws-cdk\lib\index.js:32:594)
at Function.loadAssemblyManifest (C:\Users\DREWA\AppData\Roaming\npm\node_modules\aws-cdk\lib\index.js:31:8254222)
at new CloudAssembly (C:\Users\DREWA\AppData\Roaming\npm\node_modules\aws-cdk\lib\index.js:32:14396)
at createAssembly (C:\Users\DREWA\AppData\Roaming\npm\node_modules\aws-cdk\lib\index.js:329:15973)
at execProgram (C:\Users\DREWA\AppData\Roaming\npm\node_modules\aws-cdk\lib\index.js:329:15438)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async Object.CloudExecutable.synthesizer (C:\Users\DREWA\AppData\Roaming\npm\node_modules\aws-cdk\lib\index.js:385:50431)
at async CloudExecutable.doSynthesize (C:\Users\DREWA\AppData\Roaming\npm\node_modules\aws-cdk\lib\index.js:329:7689)
Didn't know I needed a manifest.json file until I got the error beforehand that it wasn't found when I ran "cdk synth". No idea what should go in it, so I downloaded a sample from AWS. Here are the contents
{
"default": {
"version": "v0.0.1",
"telemetry": {
"deploy": true,
"env": {
"telemetryTopic": "smartproduct/telemetry"
}
},
"events": {
"deploy": true,
"env": {
"eventTopic": "smartproduct/event"
}
},
"jitr": {
"deploy": true
},
"api": {
"deploy": true
},
"ownerapp": {
"deploy": true
}
}
}
Here are the versions:
Node: v14.17.6
NPM: 6.14.15
aws-cdk: 2.63.2
Couldn't find any help in Stack Overflow, github or AWS regarding this particular error and scenario. Is there a way to run cdk synth without requiring a manifest.json?

Cypress build error in Azure pipeline: Cannot find module '#cypress/code-coverage/task'

Here is my config:
// cypress/plugins/index.js
module.exports = (on, config) => {
require('#cypress/code-coverage/task')(on, config);
//require('#bahmutov/cypress-extends')(on, config);
return config
}
I am getting an ERROR when trying to run cypress in a Azure pipeline script (within a cypress/included container). This error doesn't occur when I run on my local.
The function exported by the plugins file threw an error.
We invoked the function exported by `/root/e2e/cypress/plugins/index.js`, but it threw an error.
Error: Cannot find module '#cypress/code-coverage/task'
Require stack:
- /root/e2e/cypress/plugins/index.js
- /root/.cache/Cypress/9.1.1/Cypress/resources/app/packages/server/lib/plugins/child/run_plugins.js
The only unusual thing I am doing is this:
// cypress/config/cypress.local.json
{
"extends": "../../cypress.json",
"baseUrl": "https://localhost:4200"
}
And a normal cypress.json config:
// /cypress.json
{
"baseUrl": "http://localhost:4200",
"proxyUrl": "",
"defaultCommandTimeout": 10000,
"video" : false,
"screenshotOnRunFailure" : true,
"experimentalStudio": true,
"projectId": "seixri",
"trashAssetsBeforeRuns" : true,
"videoUploadOnPasses" : false,
"retries": {
"runMode": 0,
"openMode": 0
},
"viewportWidth": 1000,
"viewportHeight": 1200
}
The problem here might be that Cypress does not support extending the configuration file in the way you did, as also stated here: https://www.cypress.io/blog/2020/06/18/extending-the-cypress-config-file/
In my opinion there are two suitable solution approaches:
1. Approach: Use separate configuration files (my recommendation)
As extending an existing configuration file does not work, I would recommend having separate configuration files, e.g. one for local usage and one for the execution in Azure pipelines. You could then simple add two separate commands in your package.json like:
"scripts": {
"cy:ci": "cypress run --config-file cypress/cypress.json",
"cy:local": "cypress run --config-file cypress/cypress.local.json"
},
Docs: https://docs.cypress.io/guides/references/configuration
2. Approach: Set configuration options in your tests
Cypress gives you the option to overwrite configurations directly in your tests. For example, if you have configured the following in cypress.json:
{
"viewportWidth": 1280,
"viewportHeight": 720
}
You can change the viewportWidth in your test like:
Cypress.config('viewportWidth', 800)
Docs: https://docs.cypress.io/api/cypress-api/config#Syntax

Why is linting failing with "Unexpected token ." on "import.meta.url"?

I have the following lint config...
{
"extends": ["eslint:recommended", "google"],
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module"
},
"rules": {
"require-jsdoc": 1
},
"env": {
"es6": true
}
}
and the following code...
const __dirname = path.dirname(new URL(import.meta.url).pathname);
//^Error is...
But when it lints I get...
9:46 error Parsing error: Unexpected token .
This is a pretty common piece of code so I am confused.
Update
solved with...
"ignorePatterns": ["unclean.mjs", "node_modules"],
But I would like a solution where I don't have to ignore an entire file.
It is a syntax error because the default parser of ESLint only supports stage 4 proposals, but import.meta is currently stage 3. For now, you must change the parser to "babel-eslint" or "#typescript-eslint/parser" in order to parse import.meta.
That phrase is a syntax error because import is a keyword in EcmaScript. Therefore import.meta is as invalid as if.foo or switch.foo.

exports is not defined when running compiled typescript

I am trying to take my first steps into working with typescript and I've run into an issue when trying to run my application.
I get the error ReferenceError: exports is not defined
the code I have is quite simple:
// --src/changeset.ts
export enum ChangeAction {
ADD,
DELETE,
MODIFY
}
export class Changeset {
constructor(
public version: Number,
public content: String,
public path: String,
public action: ChangeAction
) {}
}
// --src/index.ts
import { Changeset, ChangeAction } from "./changeset";
const set = new Changeset(0, "Hello world", "/dev/null", ChangeAction.ADD);
set.version = 0;
console.log("Hello World! " + set.version);
// --tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"outDir": "build"
},
"include": ["src/**/*"]
}
running tsc, it compiles and seems to work without any real issues, however when I try to run it with node build/index.js it crashes with this
build/index.js:2
Object.defineProperty(exports, "__esModule", { value: true });
^
ReferenceError: exports is not defined
It feels like I am missing something quite obvious, but I can't really seem to put my finger on it, so what am I missing?
You appear to have enabled Node's ES modules by setting "type": "module" in your package.json, but your tsconfig tells typescript to emit code compatible with CommonJS.
Either remove "type": "module", or configure tsconfig to emit code targeting ES modules.

need a correct eslintrc for async/await - using 7.6+ nodejs

With the latest version of nodejs 7.6+ I started using async/await.
I was using jshint but from what I read they currently do support this syntax and some suggested using eslint.
So ok I set eslint up but argh.. it flags async functions too.
Parsing error: Unexpected token init (Fatal)
I know there is nothing wrong as my code is running fine it's just the linter. Too if I comment out an async function it just flags the next one. IN fact eslint only flags the first async found with this error not all of them (what up with that?)
Here is the eslintrc file made using the init wizard. I was hoping just asking for node and es6 for env would be sufficient...apparently not.
module.exports = {
"env": {
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"rules": {
"indent": [
"error",
"tab"
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"never"
]
}
};
What is the fix?
I've tried several versions of .eslintrc and even saw there are a few issues related at the eslint repo but none are helping me to resolve this. I don't think it's a bug just missing something about getting eslint set up correctly for native nodejs using commonjs (no babel).
Who knows maybe babel plugin is required to make this work even though I am not using babel??? If that's true how do I set that up.
Since async/await is an ES2017 feature, you need to add that to your .eslintrc.js:
module.exports = {
// ...
"parserOptions": {
"ecmaVersion": 2017
},
// ...
}

Resources