简体   繁体   English

Node.js可以传递node_modules依赖项吗?

[英]Can Node.js pass down node_modules dependencies?

So I have a ec2 instance running on a free tier on amazon's web services. 所以我有一个ec2实例在亚马逊网络服务的免费层上运行。 I'm running a Ubuntu server that has been configured to run at its public address. 我正在运行已配置为在其公共地址上运行的Ubuntu服务器。 Here is the server.js code 这是server.js代码

const express = require('express')
const app = express()

/*
app.get('/', (req, res) => {
    res.send("CONTENT COMING")
})
*/

app.use(express.static('public'))
app.listen(3000, () => {console.log("Server Running on port 3000")})

as you can see I use express to serve a static file server just so I can develop locally and easily deploy online to the server when the time comes. 如您所见,我使用express为静态文件服务器提供服务,以便可以在本地开发并在需要时轻松地在线部署到服务器。 Inside the public folder I have an index.html and a test.js file to serve. 里面的公用文件夹我有一个index.htmltest.js文件服务。 My question is will I have to create another instance of a node package manager to install packages to use in my program? 我的问题是我是否必须创建节点程序包管理器的另一个实例来安装要在程序中使用的程序包?

For example, if I want to require node.js File-System by using 例如,如果我想通过使用来要求node.js文件系统

const fs = require('fs')

would I have to create another npm init to then install my dependencies or could I somehow use my dependencies from the directory above? 我是否必须创建另一个npm init来安装我的依赖项,还是可以某种方式使用上面目录中的依赖项?

The answer is no. 答案是不。 You only need one. 您只需要一个。

NPM modules can be incorporated into your code several folders down in your project. NPM模块可以合并到项目中几个文件夹中的代码中。

Take for example a project folder: 以一个项目文件夹为例:

/root
   app.js
   package.json
   /public
      index.html
   /node_modules
      ...

All modules in the node-module folder can be used in any project sub-folder. node-module文件夹中的所有模块都可以在任何项目子文件夹中使用。

Say, you have a file named anotherApp.js in a folder down the line, it will 'inherit' all node modules installed in any parent folder. 假设您在该行下的文件夹中有一个名为anotherApp.js的文件,它将“继承”任何父文件夹中安装的所有节点模块。 Therefore, it will also be able to access every module that is in the project root node_module folder. 因此,它也将能够访问项目根节点node_module文件夹中的每个模块。

/root
   /somefoldername
      /anotherfolder
         /application
            anotherApp.js
   /node_modules
      ...

I should add, that NPM (Node package manager) is there to handle locally installed modules. 我应该补充说,那儿有NPM(节点程序包管理器)来处理本地安装的模块。 The fs (File System) modules does come with node and is installed as a global, therefore you do not need to install it via NPM. fs (文件系统)模块确实随节点一起提供,并且作为全局模块安装,因此您不需要通过NPM安装它。 Check out https://www.npmjs.com/ for a full list of available downloadable modules. 请访问https://www.npmjs.com/获取可用的可下载模块的完整列表。

I would always suggest using git to synchronize your local files and your files on the EC2 server. 我总是建议使用git同步本地文件和EC2服务器上的文件。

To install the same dependency on both your local and cloud, you can save it in your package.json eg npm install --save express 要在本地和云上安装相同的依赖项,可以将其保存在package.json例如npm install --save express

And on your server, npm install would install all the dependencies listed in package.json 在您的服务器上, npm install将安装package.json列出的所有依赖项

You just need to insert the dependencies in your current project in your package.json and make sure that you create a deployment flow that installs it. 您只需要在您的package.json当前项目中插入依赖项,并确保您创建了一个安装它的部署流程。

If you are having trouble to deploy to your EC2 Instance, I would recommend you to start one instance using Amazon EB and use CodeShip to deploy it. 如果您在部署到EC2实例时遇到问题,建议您使用Amazon EB启动一个实例,然后使用CodeShip进行部署。

Another option would be to run it using Heroku and integrating with git. 另一个选择是使用Heroku并与git集成来运行它。

Both will take care of installing the dependencies before of running it. 两者都将在运行依赖项之前先进行安装。

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

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