xpt
xpt

Reputation: 22984

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. 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. 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

I.e., to me module-b looks nothing different than module-a. but why it is OK to require('module-a') but not to require('module-b')?

Is it really because my module-b npm linked to module-a?

The whole npm link setup from module-b to module-a, and all the code, can be found at this repo.

UPDATE. I don't have a project's package.json for either module-a or module-b, but why module-a works? Moreover, having created module-c/package.json, the problem remains the same:

$ 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'

Upvotes: 4

Views: 8670

Answers (1)

guy mograbi
guy mograbi

Reputation: 28598

When nodejs requires a folder, it will try to find a main file.

By default the main file is index.js. Since you are using TypeScript, you do not have index.js, but instead you have index.ts.

To define a main file, you will need to define it in package.json. I can see you have done so in 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.

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". I assume that if you point it to build/index.js like you did in module-a it will work.

Upvotes: 10

Related Questions