简体   繁体   English

在React Server中使用外部JS库

[英]Using external JS library in React server

I have to use a JS library called 'react-armor' here for my web project. 我的Web项目必须在这里使用一个名为“ react-armor”的JS库。 So, created a React powered server as instructed here . 因此,按照此处的指示创建了一个由React驱动的服务器。

The server is up and running. 服务器已启动并正在运行。 But, in order to use the react-armor library, the code specifies to import the library into the project JS: 但是,为了使用react-armor库,代码指定将库导入项目JS:

import { obfuscateClassNames } from 'react-armor';

*as in the readme.md tutorial of react-armor github *如react-armor github的readme.md教程中所述

But, how could I use the package react-armor (examples given in the readme.md) into my react server? 但是,如何在我的React Server中使用包react-armor (在readme.md中给出的示例)? I mean, how to integrate the github repo to my server? 我的意思是,如何将github存储库集成到我的服务器上? Just using import something from 'react-armor' , so where should react-armor exist? 仅仅使用import something from 'react-armor' ,那么react-armor应该在哪里存在?

Update 1: 更新1:

Added react-armor dependency in server package.json file. 在服务器package.json文件中添加了react-armor依赖性。

But, sudo npm install --save react-armor gives me errors. 但是, 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

Even after adding the dependency in package.json file, the import doesn't work. 即使在package.json文件中添加了依赖项之后,导入也无法进行。

This is my package.json in my react server folder: 这是我的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"
  }
}

Update 1: 更新1:

It seems like some dependency errors here. 这里似乎有一些依赖错误。 But, is it possible to run this on Tomcat instead of NodeJS? 但是,是不是可以运行这个在Tomcat代替的NodeJS? Why does everyone recommend using NodeJS for ReactApp instead of Tomcat, despite React being a client side rendered JS lib? 为什么尽管React是客户端渲染的JS库,但为什么每个人都建议对ReactApp使用NodeJS而不是Tomcat?

If it's an NPM package, you would run npm install --save react-armor ( --save if you want to add it to your down package's dependencies). 如果是NPM软件包,则应运行npm install --save react-armor (如果要将其添加到down软件包的依赖项中,则--save )。 It will download it and save it into a ./node_modules folder. 它将下载并保存到./node_modules文件夹中。 Node will automatically look in that path (in addition to other preset locations) to find that module. 节点将自动查找该路径(除其他预设位置外)以找到该模块。

If it's not hosted on NPM, you can pull it in from GitHub by adding this to your package.json file. 如果它不是托管在NPM上,则可以通过将其添加到package.json文件中来从GitHub中提取它。

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

Where the url is user/repo. 网址是用户/仓库。 You would still need to run npm install to actually download it. 您仍然需要运行npm install才能实际下载它。

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

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