Javascript 使用 npm 安装本地模块?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8088795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Installing a local module using npm?
提问by fancy
I have a downloaded module repo, I want to install it locally, not globally in another directory?
我有一个下载的模块存储库,我想在本地安装它,而不是在另一个目录中全局安装?
What is an easy way to do this?
有什么简单的方法可以做到这一点?
回答by Rich Apodaca
From the npm-link documentation:
In the local module directory:
在本地模块目录中:
$ cd ./package-dir
$ npm link
In the directory of the project to use the module:
在项目目录下使用模块:
$ cd ./project-dir
$ npm link package-name
Or in one go using relative paths:
或者一次性使用相对路径:
$ cd ./project-dir
$ npm link ../package-dir
This is equivalent to using two commands above under the hood.
这相当于在幕后使用上面的两个命令。
回答by fancy
you just provide one <folder>
argument to npm install
, argument should point toward the local folder instead of the package name:
您只需向 提供一个<folder>
参数npm install
,参数应指向本地文件夹而不是包名称:
npm install /path
回答by Luke W
Since asked and answered by the same person, I'll add a npm linkas an alternative.
由于同一个人的提问和回答,我将添加一个npm 链接作为替代。
from docs:
来自文档:
This is handy for installing your own stuff, so that you can work on it and test it iteratively without having to continually rebuild.
这对于安装您自己的东西非常方便,这样您就可以对其进行处理和迭代测试,而无需不断重建。
cd ~/projects/node-bloggy # go into the dir of your main project
npm link ../node-redis # link the dir of your dependency
[Edit] As of NPM 2.0, you can declare local dependencies in package.json
[编辑] 从 NPM 2.0 开始,您可以在 package.json 中声明本地依赖项
"dependencies": {
"bar": "file:../foo/bar"
}
回答by Akash
npm pack
+ package.json
npm pack
+ package.json
This is what worked for me:
这对我有用:
STEP 1: In module project
, execute npm pack
:
第 1 步:在 中module project
,执行npm pack
:
This will build a <package-name>-<version>.tar.gz
file.
这将构建一个<package-name>-<version>.tar.gz
文件。
STEP 2: Move the file to the consumer project
第 2 步:将文件移动到 consumer project
Ideally you can put all such files in a tmp
folder in your consumer-project
root:
理想情况下,您可以将所有此类文件放在根目录中的tmp
文件夹中consumer-project
:
STEP 3: Refer it in your package.json
:
第 3 步:在您的package.json
:
"dependencies": {
"my-package": "file:/./tmp/my-package-1.3.3.tar.gz"
}
STEP 4: Install
the packages:
第 4 步:Install
包装:
npm install
or npm i
or yarn
npm install
或npm i
或yarn
Your package should be available in your consumer-project's node_modules
folder.
您的包应该在您的consumer-project's node_modules
文件夹中可用。
Good Luck...
祝你好运...
回答by Paul Medynski
Neither of these approaches (npm link
or package.json
file dependency) work if the local module has peer dependencies that you only want to install in your project's scope.
如果本地模块具有您只想在项目范围内安装的对等依赖项,则这些方法(npm link
或package.json
文件依赖项)都不起作用。
For example:
例如:
/local/mymodule/package.json:
"name": "mymodule",
"peerDependencies":
{
"foo": "^2.5"
}
/dev/myproject/package.json:
"dependencies":
{
"mymodule": "file:/local/mymodule",
"foo": "^2.5"
}
In this scenario, npm sets up myproject
's node_modules/
like this:
在这种情况下, npm 设置myproject
'snode_modules/
是这样的:
/dev/myproject/node_modules/
foo/
mymodule -> /local/mymodule
When node loads mymodule
and it does require('foo')
, node resolves the mymodule
symlink, and then only looks in /local/mymodule/node_modules/
(and its ancestors) for foo
, which it doen't find. Instead, we want node to look in /local/myproject/node_modules/
, since that's where were running our project from, and where foo
is installed.
当 node 加载mymodule
并执行时require('foo')
, node 解析mymodule
符号链接,然后只查找/local/mymodule/node_modules/
(及其祖先) for foo
,它没有找到。相反,我们希望 node 查找/local/myproject/node_modules/
,因为这是从哪里运行我们的项目,以及foo
安装在哪里。
So, we either need a way to tell node to notresolve this symlink when looking for foo
, or we need a way to tell npm to install a copyof mymodule
when the file dependency syntax is used in package.json
. I haven't found a way to do either, unfortunately :(
所以,我们要么需要以某种方式告诉节点不解决这个符号链接寻找的时候foo
,或者我们需要一种方法来告诉NPM安装一个副本的mymodule
时候文件相关的语法中使用package.json
。不幸的是,我也没有找到办法:(
回答by Karl Morrison
Missing the main property?
缺少主属性?
As previous people have answered npm --save ../location-of-your-packages-root-directory
.
The ../location-of-your-packages-root-directory
however must have two things in order for it to work.
正如前面的人所回答的那样npm --save ../location-of-your-packages-root-directory
。该../location-of-your-packages-root-directory
但是必须有两件事情,以便为它工作。
1) package.json
in that directory pointed towards
1)package.json
在该目录中指向
2) main
property in the package.json
must be set and working i.g. "main": "src/index.js",
if the entry file for ../location-of-your-packages-root-directory
is ../location-of-your-packages-root-directory/src/index.js
2)main
在属性package.json
必须被设置和工作IG"main": "src/index.js",
是否为入口文件../location-of-your-packages-root-directory
就是../location-of-your-packages-root-directory/src/index.js