简体   繁体   English

node_modules 中安装的依赖项缺少 node_modules

[英]Installed dependency in node_modules is missing node_modules

I have 2 TS packages.我有 2 个 TS 包。 project-1 is installed as a dependency in project-2 . project-1作为依赖项安装在project-2中。 I am able to import and access all type definitions of project-1 .我能够导入和访问project-1的所有类型定义。 But the dependencies (node_modules directory) in project-1 are not found.但是找不到project-1中的依赖项(node_modules目录)。 This is because the node_modules directory of project-1 is not present.这是因为project-1node_modules目录不存在。 Do I really need to manually npm install inside that node_modules/project-1 directory?我真的需要在node_modules/project-1目录中手动npm install npm 吗? Or should the node_modules directory be published to npm as well?或者 node_modules 目录也应该发布到 npm 吗? If so, should I remove it from tsconfig.json's exclude property?如果是这样,我应该将其从tsconfig.json's exclude属性中删除吗? I think node_modules should be in .gitignore right?我认为node_modules应该在.gitignore中,对吗?

Just to be clear:只是要清楚:

./project-2/node_modules                         // this exists
./project-2/node_modules/project-1               // this exists
./project-2/node_modules/project-1/node_modules  // this does not

/* only after manually running npm install in 
 * ./project-2/node_modules/project-1 the node_modules folder appears.
 */

project-1 ** package.json**项目-1 ** package.json**

  "main": "./dist/index.js",
  "types": "./dist/index.d.ts",
  "type": "commonjs",

tsconfig.json tsconfig.json

{
  "compilerOptions": {
    "declaration": true,
    "esModuleInterop": true,
    "module": "CommonJS",
    "outDir": "dist",
    "baseUrl": ".",
    "rootDir": "src",
    "strict": true,
    "target": "ES5",
    "noImplicitAny": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "src/**/*.test.ts"]
}

.gitignore .gitignore

node_modules
dist
logs
tsconfig.tsbuildinfo
.env

.npmignore this file is empty. .npmignore这个文件是空的。

How do I ensure that the node_modules directory is present when installing an npm package?安装 npm package 时如何确保 node_modules 目录存在?

The node_modules directory should not be published to npm registry. node_modules 目录不应发布到 npm 注册表。 It should only contain dependencies installed during the development of your project.它应该只包含在项目开发期间安装的依赖项。 To ensure that the node_modules directory is present when installing an npm package, you need to run npm install or yarn install in your project root directory after you have installed your dependencies.为确保在安装 npm package 时存在 node_modules 目录,您需要在安装依赖项后在项目根目录中运行 npm install 或 yarn install。 The node_modules directory should also be listed in your.gitignore file to avoid committing it to your Git repository. node_modules 目录也应该列在您的 .gitignore 文件中,以避免将其提交到您的 Git 存储库。

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

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