【问题标题】:Using external JS library in React server在 React 服务器中使用外部 JS 库
【发布时间】:2016-06-09 14:11:30
【问题描述】:

我的网络项目必须使用一个名为“react-armor”here 的 JS 库。所以,按照here 的指示创建了一个 React 驱动的服务器。

服务器已启动并正在运行。但是,为了使用 react-armor 库,代码指定将库导入到项目 JS 中:

import { obfuscateClassNames } from 'react-armor';

*如react-armor github的readme.md教程中的那样

但是,我如何在我的反应服务器中使用包 react-armor(readme.md 中给出的示例)?我的意思是,如何将 github 存储库集成到我的服务器?只使用import something from 'react-armor',那么react-armor应该存在于哪里?

更新 1:

在服务器 package.json 文件中添加了 react-armor 依赖项。

但是,sudo npm install --save react-armor 给了我错误。

npm http GET https://registry.npmjs.org/react-armor
npm http 404 https://registry.npmjs.org/react-armor
npm ERR! TypeError: Cannot read property 'latest' of undefined
npm ERR!     at next (/usr/share/npm/lib/cache.js:687:35)
npm ERR!     at /usr/share/npm/lib/cache.js:675:5
npm ERR!     at saved (/usr/share/npm/node_modules/npm-registry-client/lib/get.js:142:7)
npm ERR!     at /usr/lib/nodejs/graceful-fs/polyfills.js:133:7
npm ERR!     at Object.oncomplete (fs.js:107:15)
npm ERR! If you need help, you may report this log at:
npm ERR!     <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR!     <npm-@googlegroups.com>

npm ERR! System Linux 3.13.0-87-generic
npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install" "react-armor"
npm ERR! cwd /home/local/unixroot
npm ERR! node -v v0.10.25
npm ERR! npm -v 1.3.10
npm ERR! type non_object_property_load
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /home/local/unixroot/npm-debug.log
npm ERR! not ok code 0

即使在 package.json 文件中添加了依赖项,导入也不起作用。

这是我的 react 服务器文件夹中的 package.json:

   {
      "name": "react-tutorial",
      "version": "0.0.0",
      "private": true,
      "license": "see LICENSE file",
      "description": "Code from the React tutorial.",
      "main": "server.js",
      "dependencies": {
        "react-armor": "elierotenberg/react-armor",
        "body-parser": "^1.4.3",
         "express": "^4.4.5"
      },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/reactjs/react-tutorial.git"
  },
  "keywords": [
    "react",
    "tutorial",
    "comment",
    "example"
  ],
  "author": "petehunt",
  "bugs": {
    "url": "https://github.com/reactjs/react-tutorial/issues"
  },
  "homepage": "https://github.com/reactjs/react-tutorial",
  "engines" : {
    "node" : "0.12.x"
  }
}

更新 1:

这里似乎有些依赖错误。但是,是否可以在 Tomcat 而不是 NodeJS 上运行 this?尽管 React 是客户端渲染的 JS 库,但为什么每个人都建议将 NodeJS 用于 ReactApp 而不是 Tomcat?

【问题讨论】:

    标签: javascript node.js tomcat reactjs server


    【解决方案1】:

    如果它是一个 NPM 包,你可以运行 npm install --save react-armor(如果你想将它添加到你的 down 包的依赖项中,--save)。它将下载并保存到./node_modules 文件夹中。 Node 将自动在该路径中查找(除了其他预设位置)以找到该模块。

    如果它不是托管在 NPM 上,你可以通过将它添加到你的 package.json 文件中来从 GitHub 中提取它。

    "dependencies": {
      "react-armor": "elierotenberg/react-armor"
    }
    

    url 是用户/repo。您仍然需要运行 npm install 才能实际下载它。

    【讨论】:

    • 我猜他们没有把它放在 NPM 上。我只是快速搜索并找不到它。奇怪的。尝试通过 GitHub 链接添加它(我的帖子的第二部分)。
    • 是的,我在 package.json 中添加了相同的内容,但导入仍然没有发生。会被下载到服务器库中吗?
    • 您可以发布您的 package.json 的副本吗?谢谢!
    • 是的,当然。更新。我正在尝试将 react-armor 库导入到 here 中提到的默认反应教程应用程序中。谢谢。
    • 我认为这可能是您的 npm 版本。您可以通过运行 npm install -g npm 进行升级,或者尝试将 URL 更改为 "git+https://github.com/username/repository.git"
    猜你喜欢
    • 2020-09-01
    • 2017-11-23
    • 1970-01-01
    • 2012-01-20
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    相关资源
    最近更新 更多