Squarerootofpi
Squarerootofpi

Reputation: 75

How to use node_modules in Deno as typescript imports?

Project: REST API for serving information stored in a neo4j graph database. Backend: Deno

I am farely new to deno, but I'm not new to typescript, having used it in Angular frequently.

Problem: I want to use a driver to connect my neo4j database to my backend, but there is no neo4j driver made for Deno. I have scoured the internet and documentation for solutions, and have been trying to import the javascript library using the node modules import tool that has been suggested from similar answers and is supported by the deno team.

Essentially, I do npm install neo4j-driver, and then add the following code to my deno project.

Failed Solution: the javascript node modules wrapper

I implement call this function as a test for my deno server in a server.ts file.

The command I use for deno is: deno run --allow-all --unstable server.ts

neo4j_conn.ts file: (called by server.ts)

import { createRequire } from "https://deno.land/std/node/module.ts";
const require = createRequire(import.meta.url);

export async function testconnection(uri: string, user: string, password: string) {

  //This is the line that fails. 
  var neo4j = require('neo4j-driver').v1;  //this fails whether or not I include the .v1 or not. 

  var driver = neo4j.driver(uri, neo4j.auth.basic(user, password))
  const session = driver.session()
  const personName = 'Alice'

  try {
    const result = await session.run(
      'CREATE (a:Person {name: $name}) RETURN a',
      { name: personName }
    )

    const singleRecord = result.records[0]
    const node = singleRecord.get(0)

    console.log(node.properties.name)
  } finally {
    await session.close()
  }

  await driver.close()

}

This returns the following error:

error: Uncaught (in promise) Error: Cannot find module 'net'
Require stack:
- /mnt/c/Users/xxxxx/source/private_logic/deno-try/node_modules/neo4j-driver-bolt-connection/lib/channel/node/node-channel.js
- /mnt/c/Users/xxxxx/source/private_logic/deno-try/node_modules/neo4j-driver-bolt-connection/lib/channel/node/index.js       
- /mnt/c/Users/xxxxx/source/private_logic/deno-try/node_modules/neo4j-driver-bolt-connection/lib/channel/index.js
- /mnt/c/Users/xxxxx/source/private_logic/deno-try/node_modules/neo4j-driver-bolt-connection/lib/bolt/handshake.js
- /mnt/c/Users/xxxxx/source/private_logic/deno-try/node_modules/neo4j-driver-bolt-connection/lib/bolt/index.js
- /mnt/c/Users/xxxxx/source/private_logic/deno-try/node_modules/neo4j-driver-bolt-connection/lib/index.js
- /mnt/c/Users/xxxxx/source/private_logic/deno-try/node_modules/neo4j-driver/lib/index.js
- /mnt/c/Users/xxxxx/source/private_logic/deno-try/neo4jconn.ts
    at Function._resolveFilename (https://deno.land/[email protected]/node/module.ts:273:19)
    at Function._load (https://deno.land/[email protected]/node/module.ts:380:29)
    at Module.require (https://deno.land/[email protected]/node/module.ts:133:21)
    at require (https://deno.land/[email protected]/node/module.ts:1158:16)
    at Object.<anonymous> (file:///mnt/c/Users/xxxxx/source/private_logic/deno-try/node_modules/neo4j-driver-bolt-connection/lib/channel/node/node-channel.js:24:29)    
    at Module._compile (https://deno.land/[email protected]/node/module.ts:168:36)
    at Object.Module._extensions..js (https://deno.land/[email protected]/node/module.ts:1109:10)
    at Module.load (https://deno.land/[email protected]/node/module.ts:147:34)
    at Function._load (https://deno.land/[email protected]/node/module.ts:413:14)
    at Module.require (https://deno.land/[email protected]/node/module.ts:133:21)

As far as I could tell, I had done everything right, but I am a little in over my head when it comes to the typescript/js module translation.

My file structure is as follows:

package.json
package-lock.json
server.ts
neo4j_conn.ts
node_modules -|
              |
              :

Neo4j developer js docs: https://neo4j.com/developer/javascript/

Deno node modules "require": https://doc.deno.land/https/deno.land/[email protected]/node/module.ts

Upvotes: 3

Views: 616

Answers (1)

Steven Guerrero
Steven Guerrero

Reputation: 1056

If you look at the Node compatibility layer README in std you will realize that right now there is no compatibility module for the net library. The compatibility will improve day by day, but take into account that Deno is not a drop in replacement for Node, but a whole new thing that won't work with Node libraries by default

https://deno.land/[email protected]/node

Upvotes: 3

Related Questions