简体   繁体   English

在Windows 10上无法识别NodeJS安装的模块

[英]NodeJS installed modules are not recognized on Windows 10

I need some help to setup NodeJS on a Windows 10 machine. 我需要一些帮助来在Windows 10机器上设置NodeJS。

I installed the 10.15.3 LTS version from the official website which has done the following: 我在官方网站上安装了10.15.3 LTS版本,该版本执行了以下操作:

  • Node installed at C:\\Program Files\\nodejs\\ 节点安装在C:\\Program Files\\nodejs\\

  • Added to the Path in the user's variables: C:\\Users\\rsantos\\AppData\\Roaming\\npm 添加到用户变量中的Path: C:\\Users\\rsantos\\AppData\\Roaming\\npm

  • Added to the Path in the environment variables: C:\\Program Files\\nodejs\\ 添加到环境变量中的Path: C:\\Program Files\\nodejs\\

  • Created an empty npm folder at: C:\\Users\\rsantos\\AppData\\Roaming 在以下位置创建了一个空的npm文件夹: C:\\Users\\rsantos\\AppData\\Roaming

Then with npm init I initialized the package file which was created at C:\\Users\\rsantos . 然后使用npm init我初始化了在C:\\Users\\rsantos创建的包文件。

Followed by npm install supervisor which: 其次是npm install supervisor ,其中:

  • Created a npm-cache folder at C:\\Users\\rsantos\\AppData\\Roaming\\npm-cache C:\\Users\\rsantos\\AppData\\Roaming\\npm-cache创建了一个npm-cache文件夹

  • Created a node_modules folder (with the supervisor) at C:\\Users\\rsantos\\node_modules C:\\Users\\rsantos\\node_modules创建了一个node_modules文件夹(带有管理员)

  • Created a package-lock.json file at C:\\Users\\rsantos with: C:\\Users\\rsantos创建了一个package-lock.json文件, C:\\Users\\rsantos包含:

  {
    "name": "rsantos",
    "version": "1.0.0",
    "lockfileVersion": 1,
    "requires": true,
    "dependencies": {
      "supervisor": {
        "version": "0.12.0",
        "resolved": "https://registry.npmjs.org/supervisor/-/supervisor-0.12.0.tgz",
        "integrity": "sha1-3n5jNwFbKRhRwQ81OMSn8EkX7ME="
      }
    }
  }
  • Updated the package.json file to: package.json文件更新为:
  {
    "name": "rsantos",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
      "test": "echo \"Error: no test specified\" && exit 1"
    },
    "author": "",
    "license": "ISC",
    "dependencies": {
      "supervisor": "^0.12.0"
    }
  }

Then when I open the command line and type: 然后当我打开命令行并输入:

C:\\Users\\rsantos\\Desktop>supervisor --watch C:\\NodeJS_Projects\\Proj1\\ -e js C:\\NodeJS_Projects\\Proj1\\Test.js

I get: 我明白了:

'supervisor' is not recognized as an internal or external command, operable program or batch file.

After that I tried to install supervisor globally and it started to work. 之后我试图在全球安装主管,它开始工作。 The next error was 'require' not being recognized after installed locally. 在本地安装后,下一个错误是'require'无法识别。 I followed the same approach which made supervisor work and installed require globally. 我采用了相同的方法,使主管工作并在全球范围内安装了需求。 My code is requiring the module with require('request') . 我的代码要求模块带有require('request') But even installed globally, it fails with this error: 但即使全局安装,它也会因此错误而失败:

Error: Cannot find module 'request' at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)

Notes: 笔记:

  • I uninstalled everything and reinstalled, the result was always the same. 我卸载了所有东西并重新安装,结果总是一样的。

  • Also played with the environment variables but still no good. 也玩环境变量,但仍然没有好处。

  • npm list -g request outputs this: C:\\Users\\rsantos\\AppData\\Roaming\\npm -- request@2.88.0 npm list -g request输出: C:\\Users\\rsantos\\AppData\\Roaming\\npm -- request@2.88.0

  • At this point my C:\\Users\\rsantos\\node_modules folder has all the modules, including supervisor and request. 此时,我的C:\\Users\\rsantos\\node_modules文件夹包含所有模块,包括主管和请求。

  • The package.json has supervisor and request as dependencies. package.json具有supervisor和request作为依赖项。

  • The package-lock.json seems to have all the modules in the node_modules folder, including supervisor and request. package-lock.json似乎拥有node_modules文件夹中的所有模块,包括supervisor和request。

  • I still get the Cannot find module 'request' error. 我仍然得到Cannot find module 'request'错误。

Can someone help please? 有人可以帮忙吗?

when you want to use a NPM package as tool (eg >supervisor --watch PATH ), you can install the package --global . 当你想使用NPM包作为工具(例如>supervisor --watch PATH )时,你可以安装包--global When you want to load the package in Node with require() I would install it locally. 如果要使用require()在Node中加载包,我会在本地安装它。

Does the following work for you? 以下是否适合您?

Install the tooling globally: 全局安装工具:

npm install --global supervisor

Set up your project in local folder: 在本地文件夹中设置项目:

mkdir project
cd project
npm init -y
npm install --save request     # install to project/node_modules
node index.js                  # start your app

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

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