简体   繁体   English

无命令安装节点扩展

[英]Install node extensions without commands

I am doing an internship in a company. 我正在公司实习。
I need to create a node server. 我需要创建一个节点服务器。
I installed node on the computer (Windows) and I should install some plugins like: 我在计算机上安装了节点(Windows),我应该安装一些插件,如:
- nodejs-webpack - nodejs-webpack
- colors - 颜色
- uglify - uglify

Normally I need to enter a command like : npm install "theModule" 通常我需要输入一个命令: npm install "theModule"
But the software can not access the internet (due to company restrictions) and support service can not authorize the software (or do not want). 但该软件无法访问互联网(由于公司限制),支持服务无法授权该软件(或不想要)。

Can I install modules in any other way ? 我可以用其他任何方式安装模块吗? (download from Google and slide archives in the correct folder for example). (例如,从Google下载并在正确的文件夹中下载幻灯片档案)。


If the answer is no, do you know how can i get around this security? 如果答案是否定的,你知道如何解决这个安全问题吗?

You need a private npm repository. 您需要一个私有的npm存储库。

Check out this answer: 看看这个答案:

can you host a private repository for your organization to use with npm? 你可以为你的组织托管私人存储库以便与npm一起使用吗?

I found it ! 我找到了 !

Just for exemple, we will install 'nodejs-websocket' : 仅举例来说,我们将安装'nodejs-websocket':
1) You just have to download it here . 1)你只需要在这里下载。
2) Put files into your Node's directory (for me it's "C:\\Program Files\\nodejs\\node_modules\\npm\\node_modules") 2)将文件放入Node的目录(对我而言,它是“C:\\ Program Files \\ nodejs \\ node_modules \\ npm \\ node_modules”)
3) in your .js file just add this line : var ws = require("C:/Program Files/nodejs/node_modules/npm/node_modules/nodejs-websocket/") 3)在你的.js文件中添加以下行: var ws = require("C:/Program Files/nodejs/node_modules/npm/node_modules/nodejs-websocket/")

Done ! 做完了! Thanks for all :D 谢谢大家:D

I added this as a comment on your own answer, but I figured I should add a real answer with a better explanation. 我添加了这个作为对你自己的答案的评论,但我想我应该添加一个更好的解释的真实答案。

Normally when you run npm install package-name npm installs the package to a node_modules directory in the directory you are in at that moment. 通常,当您运行npm install package-name npm会将程序包安装到您node_modules目录中的node_modules目录中。 So if your app was located at C:\\code\\my-app then you would cd into that directory and run npm install package-name . 所以,如果您的应用程序是位于C:\\code\\my-app ,那么你将cd到该目录并运行npm install package-name This would create a node_modules directory at C:\\code\\my-app\\node_modules if it didn't already exist. 这将在C:\\code\\my-app\\node_modules创建一个node_modules目录(如果它尚不存在)。 Then it would install package-name into that directory at C:\\code\\my-app\\node_modules\\package-name . 然后它会将package-name安装到C:\\code\\my-app\\node_modules\\package-name该目录中。

As long as the module is in the node_modules directory for your app, you can require the module in your code without entering a big long file path. 只要模块位于应用程序的node_modules目录中,就可以在代码中使用该模块,而无需输入大的长文件路径。

var ws = require('nodejs-websocket');

The place you manually installed your module is the global node_modules directory. 手动安装模块的位置是全局node_modules目录。 It's where npm would install a module if you did npm install -g package-name . 如果您使用npm install -g package-name那么npm将安装模块。 The global node_modules directory is added to your system path when you install npm. 安装npm时,全局node_modules目录将添加到系统路径中。 In other words, any modules you install in there would be accessible from the command line like any other command. 换句话说,您在其中安装的任何模块都可以从命令行访问,就像任何其他命令一样。 For example: 例如:

npm install -g bower

That would install the "bower" package to the global npm module directory. 这会将“bower”包安装到全局npm模块目录中。 Bower would then be accessible as a command line tool for you to use. 然后可以将Bower作为命令行工具进行访问。 For example: 例如:

bower install angularjs

The global directory is more for tools like that and not really for modules that you intend to use in your code. 全局目录更适用于此类工具,而不适用于您打算在代码中使用的模块。 Technically you can require a module from anywhere by including the full path in the require call like you did, but the standard practice would be to place it in the node_modules directory in the root of your application, and then require it with just its name and not a full path. 从技术上讲,您可以通过在require调用中包含完整路径来从任何地方请求模块,但标准做法是将它放在应用程序根目录的node_modules目录中,然后只需要它的名称和不是一条完整的道路。


Edit: Here's another tip that you might like to take advantage of as well. 编辑:这是您可能希望利用的另一个提示。

When you normally install a module with npm install package-name , your application usually has a package.json file at the root of it. 当您通常使用npm install package-name安装模块时,您的应用程序通常在其根目录下有一个package.json文件。 If it does, you can do npm install package-name --save and npm will add the package-name module to a list in your app's package.json file. 如果是,您可以执行npm install package-name --save ,npm会将package-name模块添加到应用程序的package.json文件中的列表中。 Once a module is listed in your app's package.json file it's called a "dependency" of your app because it basically says your app depends on package-name . 一旦模块在你的应用程序的package.json文件中列出,它就被称为你应用程序的“依赖项”,因为它基本上说你的应用程序依赖package-name

Normally, when you have dependencies listed in package.json , you can completely delete your app's node_modules directory and then simply run npm install from within your app's root directory and npm will automatically install all dependencies it finds listed in your app's package.json file. 通常,当您在package.json列出依赖项时,您可以完全删除应用程序的node_modules目录,然后只需从应用程序的根目录中运行npm install ,npm将自动安装它在应用程序的package.json文件中列出的所有依赖项。 Since your corporate firewall won't allow this automatic downloading of modules, you won't get that benefit. 由于您的企业防火墙不允许自动下载模块,因此您无法获得该优势。 However it is still good practice to follow the same conventions. 但是遵循相同的惯例仍然是一种好习惯。

A trick you can do to create your package.json file is to manually install your dependencies into your app's node_modules directory. 您可以创建package.json文件的技巧是手动将依赖项安装到应用程序的node_modules目录中。 Once you have the modules your app needs, you can instruct npm to create a package.json file for your app by simply running npm init . 获得应用程序所需的模块后,您可以通过运行npm init指示npm为您的应用程序创建package.json文件。 It will walk you through a few little prompts to fill out your package.json file with details about your app. 它将引导您完成一些小提示,以填写您的package.json文件,其中包含有关您的应用程序的详细信息。 It will then peek inside your node_modules directory to see if you've already installed any modules before having a package.json file. 然后,它会node_modules您的node_modules目录,看看在安装package.json文件之前是否已经安装了任何模块。 If it finds any, it will automatically add them to the dependencies field in the package.json file it creates :D 如果找到任何内容,它会自动将它们添加到它创建的package.json文件中的依赖项字段中:D

With a proper package.json in place you'll always have a nice tidy list of what dependencies your application needs, even if your node_modules directory gets deleted. 有了适当的package.json ,即使你的node_modules目录被删除,你也总会有一个很好的整洁列表,列出你的应用程序需要什么依赖项。 Normally people add their app's node_modules directory to their .gitignore file, only checking in the package.json file. 通常人们将应用程序的node_modules目录添加到他们的.gitignore文件中,只检查package.json文件。 This way they don't store their dependencies in source control but they can still be easily installed on new machines that clone it by simply running npm install from inside the app's directory. 这样他们就不会将他们的依赖项存储在源代码控制中,但它们仍然可以通过简单地从应用程序目录中运行npm install来轻松地安装到克隆它的新机器上。 In your case though you may want to just add node_modules to your source control since you can't let npm install dependencies automatically. 在您的情况下,您可能只想将node_modules添加到源控件,因为您不能让npm自动安装依赖项。

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

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