简体   繁体   English

我可以从模块的 node_modules 导入吗?

[英]Can I import from node_modules of a module?

I'm not that great at JS/TS but I'm helping run a build system.我不太擅长 JS/TS,但我正在帮助运行构建系统。 I have a bit of code that was submitted, and I'm not sure if I should alter the build system or kick it back to the developer.我提交了一些代码,我不确定是否应该更改构建系统或将其踢回给开发人员。

The code looks like this...代码看起来像这样......

import * as googleAuth from "googleapis/node_modules/google-auth-library"

When I run yarn install the various modules are installed properly in ./node_modules/ including googleapis and google-auth-library .当我运行yarn install时,各种模块都正确安装在./node_modules/中,包括googleapisgoogle-auth-library When I run yarn build I get something like this:当我运行yarn build我得到这样的东西:

yarn run v1.19.1
$ rm -rf lib; ./node_modules/.bin/tsc -d -p tsconfig.npm.json
Error: src/foo/bar/common/oauth_helper.ts(3,29): error TS[23]: Cannot find module 'googleapis/node_modules/google-auth-library' or its corresponding type declarations.

When I test this out locally, I can make the error go away by doing cd./node_modules/googleapis && yarn install but that installs a second set of all the modules needed there.当我在本地对此进行测试时,我可以通过执行cd./node_modules/googleapis && yarn install来消除错误 go,但这会安装第二组所需的所有模块。

Is there something that I'm missing or should I tell the developer that he needs to import directly from goggle-auth-library .有没有我遗漏的东西,或者我应该告诉开发人员他需要直接从goggle-auth-library导入。 Furthermore if the developer is referencing this module explicitly I feel like he should list it as a dependency in package.json , not get it implicitly.此外,如果开发人员显式引用此模块,我觉得他应该将其列为package.json中的依赖项,而不是隐式获取它。

I would suggest to directly install google-auth-library instead of going through the node_modules of another package.我建议直接安装google-auth-library而不是通过另一个 package 的 node_modules。

This might be risky because lets says in future googleapis package updates and for some reason removes dependency on google-auth-library this will break your app.这可能有风险,因为让我们说在未来的googleapis package 更新中,并且出于某种原因删除对google-auth-library依赖,这会破坏你的应用程序。

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

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