1

I am using nodejs 10.1.0

ubuntu@ip-10-0-0-196:~/code/makeIt/chrome-render$ node -v
v10.1.0

I am using typescript2.8.3

ubuntu@ip-10-0-0-196:~/code/makeIt/chrome-render$ tsc -v
Version 2.8.3

I've set up a directory for nodejs development like so:

ubuntu@ip-10-0-0-196:~/code/makeIt/chrome-render$ find
.
./tsconfig.json
./node_modules
. lotsof files in node_modules
. lotsof files in node_modules
. lotsof files in node_modules
./yarn.lock
./src
./src/makeIt.ts
./src/server.ts
./dist
./dist/src
./dist/src/server.js
./dist/src/makeIt.js
./dist/src/server.js.map
./package.json
./dist/src/makeIt.js.map
ubuntu@ip-10-0-0-196:~/code/makeIt/chrome-render$

I compile the code OK with tsc - no errors.

When I run my code I get an error.

ubuntu@ip-10-0-0-196:~/code/makeIt/chrome-render$ node dist/src/server.js
internal/modules/cjs/loader.js:573
    throw err;
    ^

Error: Cannot find module './package.json'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:571:15)
    at Function.Module._load (internal/modules/cjs/loader.js:497:25)
    at Module.require (internal/modules/cjs/loader.js:626:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (/home/ubuntu/code/makeIt/chrome-render/dist/src/server.js:39:20)
    at Module._compile (internal/modules/cjs/loader.js:678:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:689:10)
    at Module.load (internal/modules/cjs/loader.js:589:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:528:12)
    at Function.Module._load (internal/modules/cjs/loader.js:520:3)
ubuntu@ip-10-0-0-196:~/code/makeIt/chrome-render$

There is no problem when move package.json into dist/src/

node dist/src/server.js

ubuntu@ip-10-0-0-196:~/code/makeIt/chrome-render$ find
.
./tsconfig.json
./node_modules
. lotsof files in node_modules
. lotsof files in node_modules
. lotsof files in node_modules
./yarn.lock
./src
./src/makeIt.ts
./src/server.ts
./dist
./dist/src
./dist/src/server.js
./dist/src/makeIt.js
./dist/src/server.js.map
./dist/src/package.json
./dist/src/makeIt.js.map
ubuntu@ip-10-0-0-196:~/code/makeIt/chrome-render$

Moving package.json does not seem like the right solution - I think I need it to be in my project root.

Can anyone suggest what should I be doing to avoid this error?

Am I configuring my nodejs/typescript project wrong?

2
  • 1
    Probably not related to your issue, dist should not contain src but only its files. Put the content of tsconfig.json into the question. Commented May 22, 2018 at 6:04
  • pls post code in this file > /home/ubuntu/code/makeIt/chrome-render/dist/src/server.js:39:20 Commented May 22, 2018 at 6:06

1 Answer 1

2

Credit to @hashtabe_0

My server.js file contained

const package_json = require('./package.json');

Removing it fixed the problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.