"use strict" is not working as expected

Hi All,

I’m trying to learn as per topic ES6: Explore Differences Between the var and let Keywords > “use strict” concept.

I’ve below code which doesn’t throw any error with VS Code version 1.31.1 on Mac

 "use strict";
 secondVariable = "new-test";

 firstVariable = "test";
 firstVariable = "test2";

 console.log(secondVariable);

Please clarify

What are you running the code with? If I put that in a file and run it with node, I do get an error.

~$ cat > foo.js
 "use strict";
 secondVariable = "new-test";

 firstVariable = "test";
 firstVariable = "test2";

 console.log(secondVariable);

~$ node foo.js
/Users/chuck/foo.js:2
 secondVariable = "new-test";
                ^

ReferenceError: secondVariable is not defined
    at Object.<anonymous> (/Users/chuck/foo.js:2:17)
    at Module._compile (internal/modules/cjs/loader.js:722:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:733:10)
    at Module.load (internal/modules/cjs/loader.js:620:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
    at Function.Module._load (internal/modules/cjs/loader.js:552:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:775:12)
    at startup (internal/bootstrap/node.js:300:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:826:3)
~$

I see what you mean: VSCode doesn’t hilight the error. That’s because you need to install eslint, install the eslint plugin, then configure eslint for your project. Which as I’ve just discovered, requires it to be a node project (having a package.json file that is).

Alternatively, use WebStorm, which just works out of the box.

Thanks for reply, I’ve started learning JavaScript recently and have below follow up doubts per your explanations

  • Do learn basics of JS do I need to create node project ?
  • WebStorm doesn’t have community edition yet like IntelliJ, do you still recommend it over VS Code ?
  • My current workspace looks like screenshot attached ? How can I use eslint with it ?

Thanks in advance.

[UPDATES]

To use ESLint please install eslint by running npm install eslint in the workspace folder Javascript
or globally using 'npm install -g eslint'. You need to reopen the workspace after installing eslint.

Did global install per above info & re-opened the workspace

  • Got new error in console as
File will not be validated. Consider running 'eslint --init' in the workspace folder Javascript
Alternatively you can disable ESLint by executing the 'Disable ESLint' command.

Did installation per above info and got new error

eslint --init

? **How would you like to use ESLint?** To check syntax, find problems, and enforce code style

? **What type of modules does your project use?** JavaScript modules (import/export)

? **Which framework does your project use?** None of these

? **Where does your code run?** Browser, Node

? **How would you like to define a style for your project?** Use a popular style guide

? **Which style guide do you want to follow?** Airbnb (https://github.com/airbnb/javascript)

? **What format do you want your config file to be in?** JavaScript

Checking peerDependencies of eslint-config-airbnb-base@latest

Local ESLint installation not found.

The config that you've selected requires the following dependencies:

eslint-config-airbnb-base@latest eslint@^4.19.1 || ^5.3.0 eslint-plugin-import@^2.14.0

? **Would you like to install them now with npm?** Yes

Error: Cannot find module ‘eslint-config-airbnb-base’

I’m struck now and not sure if ESLint is configured properly with VS Code atm.

Please guide.

Thanks in advance.

Creating a node project is as simple as npm init, which will create a package.json file in the current directory. That’s enough to be considered a “node.js project” (even if it doesn’t actually run on node.js)

WebStorm is free, so it doesn’t need a community edition. You can also use IntelliJ CE, install some plugins, it should be the same as WebStorm. It does look like you’re close to getting VSCode to do what you want though, so if you’re more comfortable with VSCode, just keep using it.

The problem with eslint-config-airbnb-base is a bug in eslint when it’s installed globally. Run this command in your project:
npm install --save-dev eslint-config-airbnb-base
Then run eslint --init again.

TAZ for detailed clarifications.

Regards,
Vikram