我想这样做,所以npm install也会安装这个包。Json的../somelocallib或者更重要的是它的依赖项。
"dependencies": {
"express": "*",
"../somelocallib": "*"
}
我想这样做,所以npm install也会安装这个包。Json的../somelocallib或者更重要的是它的依赖项。
"dependencies": {
"express": "*",
"../somelocallib": "*"
}
当前回答
实际上,从npm 2.0开始,现在已经支持本地路径了。
其他回答
这对我来说很有效:首先,确保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" : "*"
}
我想使用一组用TypeScript编写的本地依赖,这里的答案都不适合我。NPM安装会直接拒绝构建依赖项。
我不得不使用tsconfig。Json将包添加到我的项目中,而不将它们标记为依赖项。我的用例更加复杂,因为有些依赖项是相互依赖的,我希望所有依赖项都来自本地文件夹。
以下是我的解决方案:
// tsconfig.json
{
"compilerOptions": {
"baseUrl": "./",
"paths": {
"@tiptap/*": [
"tiptap/packages/*/src"
]
}
}
}
在上面的例子中,我有一个名为tiptap/的本地项目子文件夹,在tiptap/packages/*中有许多包。“路径”选项将重写所有@tiptap/foo导入到。/tiptap/packages/foo/src,包括我自己的文件和tiptap/中的文件。
这不是一个很好的解决方案,但这是唯一对我有效的方法。
有一个很棒的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" ]
在2021年,你需要这样使用它:
npm i my-pkg@file:./path-to-my-pkg.js
# To remove it later
npm un my-pkg
如果是完整的package.json,则在最后使用.js。
使用
const myPkg = require('my-pkg')
这很有魅力!
现在可以在包中指定本地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.