简体   繁体   English

找不到模块'esprima'

[英]Cannot find module 'esprima'

I'm trying to develop an Abstract Syntax Tree program in JavaScript. 我正在尝试用JavaScript开发一个抽象语法树程序。 I'm using Jet brains IDE for development. 我正在使用Jet brains IDE进行开发。 When I run the program, i get the error Cannot find module esprima . 当我运行程序时,我得到错误无法找到模块esprima The nodejs settings are perfect and, I don't think there is any problem with it. nodejs设置是完美的,我不认为它有任何问题。 Please find the code snippet below. 请在下面找到代码段。 In one article, I saw that esprima module is present in the nodejs. 在一篇文章中,我看到esprima模块存在于nodejs中。 Please help. 请帮忙。 Thank You. 谢谢。

var esp = require('esprima');

Check your project's node_modules directory for esprima . 检查项目的node_modules目录中的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. 使用WinKey + R运行命令提示符。 Then run cmd , and in cmd execute change directory command.For example, if your project is in C drive then, 然后运行cmd ,并在cmd中执行change directory命令。例如,如果你的项目在C盘中,那么,

C:

This will change to C: drive, then locate your project directory. 这将更改为C:驱动器,然后找到您的项目目录。

cd project_directory

Change project_directory with yours. 与您一起更改project_directory Then install module using, 然后使用安装模块,

npm install esprima

Otherwise try to update it using, 否则尝试使用更新它,

npm update esprima

If you're using WebStorm 7 (which is nearly complete as I write this), I'd suggest the following steps as it's simple. 如果你正在使用WebStorm 7(我写这篇文章几乎完成),我建议采用以下步骤,因为它很简单。

In WebStorm 7+, you can quickly get to an embedded command line for your project by using the Tools menu option, Open Terminal... . 在WebStorm 7+中,您可以使用“ Tools菜单选项“ 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. 如果您没有使用具有该选项的WebStorm版本,只需切换到Node.JS应用程序的根目录并执行相同的步骤。

If you don't already have a package.json file defined for your node.js project. 如果您还没有为node.js项目定义package.json文件。 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). 从控制台按照提示进行操作(按[Enter]一次或两次从字段移动到另一个字段)。

You can then add esprima as a dependency manually to dependencies as shown below: 然后,您可以手动将esprima作为依赖项添加到dependencies ,如下所示:

{
  "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. 在上面的例子中,我已经指定我想使用当前版本的esprima位于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

npm install esprima --save

This will download the current version of esprima and add it as a dependency in the package.json file. 这将下载当前版本的esprima并将其作为依赖项添加到package.json文件中。 It will automatically associate the current semantic version of esprima with your package. 它会自动将esprima的当前语义版本与您的包相关联。

If you use the --save option, it currently would add the following to the package.json file: 如果使用--save选项,它当前会将以下内容添加到package.json文件中:

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

Once the dependency is listed in the package.json file, you can always use: 一旦依赖项列在package.json文件中,您就可以始终使用:

npm update

from the root directory of your application to update it (or download it fresh if it's not yet locally available). 从应用程序的根目录更新它(如果它尚未在本地可用,则下载它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM