Question

I'm trying to develop an Abstract Syntax Tree program in JavaScript. I'm using Jet brains IDE for development. When I run the program, i get the error Cannot find module esprima. The nodejs settings are perfect and, I don't think there is any problem with it. Please find the code snippet below. In one article, I saw that esprima module is present in the nodejs. Please help. Thank You.

var esp = require('esprima');
Was it helpful?

Solution

Check your project's node_modules directory for esprima. If it is not there try to install it using the following command in terminal,

From your command prompt terminal, change your directory to your project's root directory.

Use WinKey + R to run command prompt. Then run cmd, and in cmd execute change directory command.For example, if your project is in C drive then,

C:

This will change to C: drive, then locate your project directory.

cd project_directory

Change project_directory with yours. Then install module using,

npm install esprima

Otherwise try to update it using,

npm update esprima

OTHER TIPS

If you're using WebStorm 7 (which is nearly complete as I write this), I'd suggest the following steps as it's simple.

In WebStorm 7+, you can quickly get to an embedded command line for your project by using the Tools menu option, Open Terminal.... From there, you can manipulate the installed node packages easily. If you aren't using a version of WebStorm that has the option, simply switch to the root directory of your Node.JS application and perform the same steps.

If you don't already have a package.json file defined for your node.js project. Add it.

You can either manually create the file or use

npm init

from the console and follow along with the prompts (press [Enter] once or twice to move along from field to field).

You can then add esprima as a dependency manually to dependencies as shown below:

{
  "name": "nodetemp",
  "version": "0.0.0",
  "description": "Best demo ever",
  "main": "index.js",
  "repository": "",
  "author": "",
  "license": "BSD",
  "dependencies": {
    "esprima": "*"
  }
}

In the example above, I've specified I wanted to use whatever the current version of esprima is located on npm.org. (Which is OK for development purposes more than likely, but less ideal for production unless carefully managed.)

Or from the command-line use npm again:

npm install esprima --save

This will download the current version of esprima and add it as a dependency in the package.json file. It will automatically associate the current semantic version of esprima with your package.

If you use the --save option, it currently would add the following to the package.json file:

  "dependencies": {
    "esprima": "~1.0.4"
  }

Once the dependency is listed in the package.json file, you can always use:

npm update

from the root directory of your application to update it (or download it fresh if it's not yet locally available).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top