coreybutler / node-windows

Windows support for Node.JS scripts (daemons, eventlog, UAC, etc).
Other
2.83k stars 356 forks source link

ReferenceError: require is not defined #262

Closed mdodge-ecgrow closed 4 years ago

mdodge-ecgrow commented 4 years ago

I am trying to install a Node service in Windows. I know I did this before and I don't believe I ran into this issue. Here is my service-install.js:

var Service = require('node-windows').Service;

// Create a new service object
var svc = new Service({
  name:'Node-Testing.js',
  description: 'Nodejs.org test web server.',
  script: 'C:\Web\\Webs\\NodeTest\\server.js',
  nodeOptions: [
    '--harmony',
    '--max_old_space_size=4096'
  ]
  //, workingDirectory: '...'
});

// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
  svc.start();
});

svc.install();

And I've installed node-windows globally. So I open a command prompt, cd to the the NodeTest folder and run node service-install.js and I get back:

C:\Web\Webs\NodeTest>node service-install.js
(node:1080) ExperimentalWarning: The ESM module loader is experimental.
file:///C:/Web/Webs/NodeTest/service-install.js:1
var Service = require('node-windows').Service;
              ^

ReferenceError: require is not defined
    at file:///C:/Web/Webs/NodeTest/service-install.js:1:15

What am I doing wrong?

coreybutler commented 4 years ago

Installing globally won't work, it needs to be local to the project (i.e. standard npm install, not npm install -g.

It also looks like you may have "type": "module" in your package.json file. The error you're receiving is caused by the use of CommonJS syntax in an ES Modules environment.

You should be able to change the first line of your script to:

import { Service } from 'node-windows'

I haven't tested ES Modules with this library, but Node 14.x.x and above has been handling them pretty well (in general).

Also, you can delete the --harmony option in nodeOptions. That's an old flag, long gone in versions of Node which support ES Modules.

mdodge-ecgrow commented 4 years ago

Figured it out!!! Main issue was here: script: 'C:\Web\\Webs\\NodeTest\\server.js',. I was missing the double slash on the first seperator. I also had to remove type:module from the package.json. Also, you were right, it did not work globally.

I did try leaving that line type:module in there and using the import option instead of the require. I got this error: SyntaxError: The requested module 'node-windows' does not provide an export named 'Service'.

I could have sworn last time I did this, I believe it was in April, I had it installed globally and the type:module was in there. Because I just copied the files from my previous node service verbatim.

Oh well, it's working good. Thanks for your help and all your work on this project!!

Update: OK, I think I just figured out why I had the type:module in my old node project. I needed to remove it to create the service, but I need it in there to actually run the service because I am using import statements in my code.

coreybutler commented 4 years ago

@mdodge-ecgrow glad you got it working.

Right, there wouldn't be a named export, so the example I provided above would have been incorrect. It should be:

import whatever from 'node-windows'

var svc = new whatever.Service({...})