我想这样做,所以npm install也会安装这个包。Json的../somelocallib或者更重要的是它的依赖项。
"dependencies": {
"express": "*",
"../somelocallib": "*"
}
我想这样做,所以npm install也会安装这个包。Json的../somelocallib或者更重要的是它的依赖项。
"dependencies": {
"express": "*",
"../somelocallib": "*"
}
当前回答
NPM >= 2.0.0
该特性在npm 2.0.0版本中实现。本地路径可以使用npm install -S或npm install——save保存,使用以下任何一种形式:
../foo/bar
~/foo/bar
./foo/bar
/foo/bar
示例package.json:
{
"name": "baz",
"dependencies": {
"bar": "file:../foo/bar"
}
}
npm ls:
app@0.0.1 /private/tmp/app
└── somelocallib@0.0.1 -> /private/tmp/somelocallib
NPM < 2.0.0
在包中放入somelocallib作为依赖项。正常的Json:
"dependencies": {
"somelocallib": "0.0.x"
}
然后运行npm link ../somelocallib和NPM会将你正在使用的版本作为符号链接安装。
参考:链接(1)
其他回答
现在可以在包中指定本地Node模块安装路径。直接json。从文档中可以看出:
Local Paths As of version 2.0.0 you can provide a path to a local directory that contains a package. Local paths can be saved using npm install -S or npm install --save, using any of these forms: ../foo/bar ~/foo/bar ./foo/bar /foo/bar in which case they will be normalized to a relative path and added to your package.json. For example: { "name": "baz", "dependencies": { "bar": "file:../foo/bar" } } This feature is helpful for local offline development and creating tests that require npm installing where you don't want to hit an external server, but should not be used when publishing packages to the public registry.
有一个很棒的yalc可以帮助管理本地包。它帮助我使用后来部署的本地库。只需用.yalc目录打包项目(带或不带/node_modules)。所以只要这样做:
npm install -g yalc
in directory lib/$ yalc publish
在项目:
project/$ yalc add lib
project/$ npm install
就是这样。
当你想更新东西:
lib/$ yalc push //this will updated all projects that use your "lib"
project/$ npm install
使用Docker打包和部署
tar -czvf <compresedFile> <directories and files...>
tar -czvf app.tar .yalc/ build/ src/ package.json package-lock.json
注意:记得添加.yalc目录。
inDocker:
FROM node:lts-alpine3.9
ADD app.tar /app
WORKDIR /app
RUN npm install
CMD [ "node", "src/index.js" ]
完整的纱线用户本地开发指南:
首先在主项目中添加依赖项:
cd main-project
yarn add file:../path/to/your-library
接下来,如果你想避免在每次改变它的源代码时重新构建这个依赖:
cd your-library
yarn link
这将注册一个到你的库的链接。接下来,使用刚刚在主项目中创建的链接。
cd main-project
yarn link your-library
现在,每次更改库中的代码时,您都不需要重新构建它,它将自动包含在主项目中。Yarn link的工作原理是在你的node_modules文件夹中创建符号链接,在这里阅读更多信息:https://classic.yarnpkg.com/lang/en/docs/cli/link/
我知道npm install ../ somelocallib作品。
然而,我不知道你在问题中显示的语法是否可以从package.json中工作…
不幸的是,doc似乎只提到URL作为依赖项。
尝试file:///…/…tar.gz,指向压缩的本地库…告诉我们它是否有效。
这对我来说很有效:首先,确保npm目录有正确的用户
sudo chown -R myuser ~/.npm
sudo chown -R myuser /usr/local/lib/node_modules
那你就在你的包裹里。Json链接目录
"scripts": {
"preinstall": "npm ln mylib ../../path/to/mylib"
},
"dependencies": {
"mylib" : "*"
}