PositiveGuy
PositiveGuy

Reputation: 47743

'require' keyword not being recognized by webstorm

I'm using WebStorm as the IDE.

Here's my folder structure and express.js insalled:

enter image description here

But my sample code is not aware of the require keyword:

var express = require('express');
var app = express();

app.listen(1337, function(){
    console.log("ready");
});

Update

Per Darin's answer Here's my package.json file which now sits in the root of the Website folder:

enter image description here

{
  "name": "MyTestSite.com",
  "version": "0.0.1",
  "description": "A Website",
  "main": "test.js",
  "directories": {
    "test": "tests"
  },
  "dependencies": {
    "express": "^4.11.2"
  },
  "devDependencies": {},
  "scripts": {
    "test": "n/a"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/n/a"
  },
  "keywords": [
    "express"
  ],
  "author": "My Name",
  "license": "n/a",
  "bugs": {
    "url": "https://github.com/n/a/issues"
  },
  "homepage": "https://github.com/n/a"
}

This was created with npm init. I don't think I need all that crap in there so now I just have:

{
  "name": "MyTestSite.com",
  "version": "0.0.1",
  "description": "A Website",
  "main": "test.js",
  "directories": {
    "test": "tests"
  },
  "dependencies": {
    "express": "^4.11.2"
  }
}

I must have something malformed here...WebStorm still doesn't recognize the require keyword.

require is a keyword for npm isn't it?

Upvotes: 31

Views: 20197

Answers (5)

valdeci
valdeci

Reputation: 15237

Update

On the new Webstorm versions, just going above error and clicking in More Actions... (or ALT+ENTER) and selecting Enable Node.js coding assistance will solve this.

enter image description here


To solve these problems on the new Webstorm versions, you need to enable the Coding assistance for Node.js.

To do this, go on the Settings > Languages & Frameworks > Node.js and NPM and click on the Coding assistance for Node.js option, and then click OK to save:

enter image description here

This will solve all Node.js unresolved variables and functions.

Upvotes: 1

Sk606
Sk606

Reputation: 2352

Under Preferences > Languages & Frameworks > Node.js and NPM, make sure "Node.js Core library is enabled" is enabled.

enter image description here

Upvotes: 18

Kjell Ivar
Kjell Ivar

Reputation: 1164

Under Settings > Languages & Frameworks > Node.js and NPM make sure to check Index internal node modules. After it's done indexing, it will recognize the require keyword.

enter image description here

Upvotes: 5

Matthew Bakaitis
Matthew Bakaitis

Reputation: 11980

In Webstorm, there are three places in your settings (ctrl-alt-s) where you can update settings for Node.js projects.

Settings: Javascript Libraries

First, in Javascript | Libraries you can specify that Node libraries should be loaded. Your set of libraries may look different than this, but it should be pretty close...or, if needed, you can add the libraries so that your flavor (node, io, whatever) shows up.

enter image description here

JSHint

Second, if you have JSHint enabled, you should also enable the Node.js environment so that JSHint acts appropriately.

enter image description here

Nodejs and NPM Settings

You can (should) also set the path to your node executable. Webstorm will also detect your globally installed modules, too, and show you if your versions are up-to-date.

enter image description here

The Official Docs

Finally, this reference link contains much more information about WebStorm and Node: JetBrains Webstorm -- Nodejs Docs

Upvotes: 48

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

Make sure that you have a package.json file in the root of your website.

Upvotes: 0

Related Questions