Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js require returns a string instead of module object

I'm trying to make an electron app. However, my work stuck on very beginning: the require keyword gives me a string, instead of module object.

electron = require('electron')
console.log(electron)

It prints:

/path/to/my/project/node_modules/[email protected]@electron/dist/electron

, that totally cannot be used as a module.

In comparison, if I import a built-in module, or another npm-installed module:

fs = require('fs')
console.log(fs)

mkdirp = require('mkdirp')
console.log(mkdirp)

It prints object hierarchy, including variables and functions.

The following are contents of election directory, I don't know if they are healthy:

$ ls node_modules/electron
appveyor.yml  cli.js  CONTRIBUTING.md  dist  electron.d.ts  index.js  install.js  issue_template.md  LICENSE  node_modules  package.json  path.txt  README.md  test
like image 820
jiandingzhe Avatar asked Jul 24 '17 07:07

jiandingzhe


1 Answers

This is the expected behavior. You need to run your app using electron (in node_modules/.bin), not plain node.

This is what the official tutorial uses inside package.json:

"scripts": {
  "start": "electron ."
}

This ends up calling the electron cli, which uses require to find the path to electron.exe (the string you discovered) and spawns that as a child process that runs the app.

like image 140
Andrew Myers Avatar answered Oct 06 '22 22:10

Andrew Myers