简体   繁体   English

npm链接会导致找不到模块问题吗?

[英]Could npm link cause the cannot find module problem?

I'm trying to learn the concept of how to use TypeScript modules from plain JavaScript projects, and it seems to me that I can only use a npm linked module, but not a module that npm link to others. 我正在尝试从普通的JavaScript项目中学习如何使用TypeScript模块的概念, 在我看来,我只能使用npm链接的模块,而不能使用npm链接到其他模块的模块。 Let me explain with an example: 让我用一个例子来解释:

$ cat index1.js 
const { add, multiply, divide } = require('module-a')

const newfunc = (a, b) =>
       divide(multiply(add(a, b), 6), 2);

const result = newfunc(1, 2)
console.log(result);

$ node index1.js
9

The module-a is a TypeScript module that I npm linked to from my JavaScript project. module-a -a是我从我的JavaScript项目链接到的TypeScript模块。 And it works fine. 而且效果很好。 Now: 现在:

$ diff -wU 1 index1.js index2.js
--- index1.js   2019-01-01 16:25:50.000000000 -0500
+++ index2.js   2019-01-01 16:37:33.000000000 -0500
@@ -1,2 +1,3 @@
 const { add, multiply, divide } = require('module-a')
+const { myfunc } = require('module-b')

@@ -7 +8,3 @@
 console.log(result);
+
+console.log(myfunc(1, 2));

$ node index2.js
internal/modules/cjs/loader.js:605
    throw err;
    ^

Error: Cannot find module 'module-b'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:603:15)
    at Function.Module._load (internal/modules/cjs/loader.js:529:25)
    at Module.require (internal/modules/cjs/loader.js:657:17)
    at require (internal/modules/cjs/helpers.js:22:18)
...

$ ls -l node_modules/
total 0
lrwxrwxrwx 1 tong tong 30 2019-01-01 16:17 module-a -> /usr/lib/node_modules/module-a
lrwxrwxrwx 1 tong tong 30 2019-01-01 16:28 module-b -> /usr/lib/node_modules/module-b

$ ls -l /usr/lib/node_modules/module-b
lrwxrwxrwx 1 root root 83 2019-01-01 16:32 /usr/lib/node_modules/module-b -> /paths/to/ts-modules-test/module-b

Ie, to me module-b looks nothing different than module-a . 也就是说,对我来说, module-b看起来与module-a module-b没什么不同。 but why it is OK to require('module-a') but not to require('module-b') ? 但是为什么可以require('module-a')而不require('module-b')

Is it really because my module-b npm linked to module-a ? 真的是因为我的module-b npm链接到module-a吗?

The whole npm link setup from module-b to module-a , and all the code, can be found at this repo . module-bmodule-a的整个npm链接设置以及所有代码都可以在此repo中找到。

UPDATE. 更新。 I don't have a project's package.json for either module-a or module-b , but why module-a works? 我没有针对module-amodule-b的项目的package.json,但是为什么module-a可以工作? Moreover, having created module-c/package.json , the problem remains the same: 此外,创建了module-c/package.json ,问题仍然存在:

$ find . 
.
./node_modules
./node_modules/module-a
./node_modules/module-b
./index1.js
./index2.js

$ npm init --force --yes
Wrote to /paths/to/ts-modules-test/module-c/package.json:

{
  "name": "module-c",
  "version": "1.0.0",
  "description": "",
  "main": "index1.js",
  "dependencies": {
    "module-a": "^1.0.0",
    "module-b": "^1.0.0"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

$ node index2.js
internal/modules/cjs/loader.js:605
    throw err;
    ^

Error: Cannot find module 'module-b'

When nodejs requires a folder, it will try to find a main file. 当nodejs需要一个文件夹时,它将尝试查找主文件。

By default the main file is index.js . 默认情况下,主文件是index.js Since you are using TypeScript, you do not have index.js , but instead you have index.ts . 由于使用的是TypeScript,因此没有index.js ,而是具有index.ts

To define a main file, you will need to define it in package.json . 要定义主文件,您需要在package.json定义它。 I can see you have done so in module-a . 我可以在module-a看到您已经这样做module-a

{
   "main": "build/index.js"
}

This means that at some point during installation you have compiled the ts to js and the output from the compiler was placed in folder build . 这意味着在安装过程中的某个时候,您已将ts编译为js并且编译器的输出已放置在build文件夹中。

Looking in module-b , your compiler for this module is also pointing to build folder, but the package.json "main" property has value "index.js". module-b ,该模块的编译器也指向build文件夹,但是package.json “ main”属性的值为“ index.js”。 I assume that if you point it to build/index.js like you did in module-a it will work. 我假设,如果您像在module-a一样将它指向build/index.js ,它将起作用。

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

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