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'
javascript node.js typescript npm
add a comment |
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'
javascript node.js typescript npm
Module-b needs to be in project's package.json. please share content of file.
– guy mograbi
Jan 1 at 22:32
do bothmodule-a
andmodule-b
have a fileindex.js
?
– guy mograbi
Jan 2 at 0:25
no..module-b
is not defined correctly. please look at "main" inpackage.json
formodule-b
vs.module-a
. module-a defines it correctlybuild/index.js
while module-b does not.
– guy mograbi
Jan 2 at 1:39
add a comment |
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'
javascript node.js typescript npm
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'
javascript node.js typescript npm
javascript node.js typescript npm
edited Jan 2 at 3:19
xpt
asked Jan 1 at 22:06
xptxpt
4,46583477
4,46583477
Module-b needs to be in project's package.json. please share content of file.
– guy mograbi
Jan 1 at 22:32
do bothmodule-a
andmodule-b
have a fileindex.js
?
– guy mograbi
Jan 2 at 0:25
no..module-b
is not defined correctly. please look at "main" inpackage.json
formodule-b
vs.module-a
. module-a defines it correctlybuild/index.js
while module-b does not.
– guy mograbi
Jan 2 at 1:39
add a comment |
Module-b needs to be in project's package.json. please share content of file.
– guy mograbi
Jan 1 at 22:32
do bothmodule-a
andmodule-b
have a fileindex.js
?
– guy mograbi
Jan 2 at 0:25
no..module-b
is not defined correctly. please look at "main" inpackage.json
formodule-b
vs.module-a
. module-a defines it correctlybuild/index.js
while module-b does not.
– guy mograbi
Jan 2 at 1:39
Module-b needs to be in project's package.json. please share content of file.
– guy mograbi
Jan 1 at 22:32
Module-b needs to be in project's package.json. please share content of file.
– guy mograbi
Jan 1 at 22:32
do both
module-a
and module-b
have a file index.js
?– guy mograbi
Jan 2 at 0:25
do both
module-a
and module-b
have a file index.js
?– guy mograbi
Jan 2 at 0:25
no..
module-b
is not defined correctly. please look at "main" in package.json
for module-b
vs. module-a
. module-a defines it correctly build/index.js
while module-b does not.– guy mograbi
Jan 2 at 1:39
no..
module-b
is not defined correctly. please look at "main" in package.json
for module-b
vs. module-a
. module-a defines it correctly build/index.js
while module-b does not.– guy mograbi
Jan 2 at 1:39
add a comment |
1 Answer
1
active
oldest
votes
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.
Oh thanks a lot Mr. Mograbi, I'm new to this andpackage.json
ofmodule-b
was forked from somebody else's place. I would never have been able to figure this one out on my own, thanks a lot for helping!!!
– xpt
Jan 2 at 3:17
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999308%2fcould-npm-link-cause-the-cannot-find-module-problem%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Oh thanks a lot Mr. Mograbi, I'm new to this andpackage.json
ofmodule-b
was forked from somebody else's place. I would never have been able to figure this one out on my own, thanks a lot for helping!!!
– xpt
Jan 2 at 3:17
add a comment |
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.
Oh thanks a lot Mr. Mograbi, I'm new to this andpackage.json
ofmodule-b
was forked from somebody else's place. I would never have been able to figure this one out on my own, thanks a lot for helping!!!
– xpt
Jan 2 at 3:17
add a comment |
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.
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.
answered Jan 2 at 1:42
guy mograbiguy mograbi
11.8k756100
11.8k756100
Oh thanks a lot Mr. Mograbi, I'm new to this andpackage.json
ofmodule-b
was forked from somebody else's place. I would never have been able to figure this one out on my own, thanks a lot for helping!!!
– xpt
Jan 2 at 3:17
add a comment |
Oh thanks a lot Mr. Mograbi, I'm new to this andpackage.json
ofmodule-b
was forked from somebody else's place. I would never have been able to figure this one out on my own, thanks a lot for helping!!!
– xpt
Jan 2 at 3:17
Oh thanks a lot Mr. Mograbi, I'm new to this and
package.json
of module-b
was forked from somebody else's place. I would never have been able to figure this one out on my own, thanks a lot for helping!!!– xpt
Jan 2 at 3:17
Oh thanks a lot Mr. Mograbi, I'm new to this and
package.json
of module-b
was forked from somebody else's place. I would never have been able to figure this one out on my own, thanks a lot for helping!!!– xpt
Jan 2 at 3:17
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999308%2fcould-npm-link-cause-the-cannot-find-module-problem%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Module-b needs to be in project's package.json. please share content of file.
– guy mograbi
Jan 1 at 22:32
do both
module-a
andmodule-b
have a fileindex.js
?– guy mograbi
Jan 2 at 0:25
no..
module-b
is not defined correctly. please look at "main" inpackage.json
formodule-b
vs.module-a
. module-a defines it correctlybuild/index.js
while module-b does not.– guy mograbi
Jan 2 at 1:39