Using NPM Packages in other Node Scripts


#1

So I’m attempting to make a parcel tracking site. But I need to use a node script linked with my html files so they can access its functions.

{
  "//1": "describes your app and its dependencies",
  "//2": "https://docs.npmjs.com/files/package.json",
  "//3": "updating this file will download and update your packages",
  "name": "hello-express",
  "version": "0.0.1",
  "description": "A simple Node app built on Express, instantly up and running.",
  "main": "server.js",
  "scripts": {
    "server": "node server.js",
    "tracking": "node public/tracking.js",
    "start": "npm run server | npm run tracking"
  },
  "dependencies": {
    "express": "^4.16.4",
    "shipengine": "^0.3.1"
  },
  "engines": {
    "node": "8.x"
  },
  "repository": {
    "url": "https://glitch.com/edit/#!/hello-express"
  },
  "license": "MIT",
  "keywords": [
    "node",
    "glitch",
    "express"
  ]
}

And heres that tracking.js file:

console.log('hi');
const ShipEngine = require('shipengine');
var engine = new ShipEngine.ShipEngine('k0XAWrKUK8Lwf59KYHEqp0Og08AxI89oPOj8msXwH0Q'); 
 
function track(carrier, number) {
  engine.trackPackage(carrier, number).then((data)=>{
    if(data.status_code == 'UN') {
      console.log("Invalid");
    } else {
      console.log(data);
    }
    console.log(data.carrier_status_code);
  }).catch((err) => {
      console.log('error', err); 
  });
}

Now this works with glitches console but in Chromes developer console(control + shift + i)
View Link: https://glitch.com/edit/#!/flawless-basilisk


#2

Hey @Ash13-Dev, I’m a little confused by what you mean when you refer to Chrome’s dev console in this context. Chrome’s console won’t have any knowledge of tracking.js on the server. What exactly are you doing and what’s your expected result?


#3

Thanks for reading,

by console I’m referring to the output tab/console tab which outputs errors, logs, etc. It keeps informing me that ‘require’ is not defined in tracking.js
I am trying to get tracking.js to use a certain NPM package to execute functions. I then link this script to all my html files so I can access those functions from other scripts.


#4

Hey @Ash13-Dev I think the current problem is in how you’re leveraging requirejs. As I understand things, the client-side require you’re calling in tracking.js won’t work the same as require() will work in node. Specifically in this case I don’t think you can require a github repo directly - I think you’ll need to require a javascript file specifically.


#5

I will probably just stop using glitch as it works fine, but it doesn’t do what I need it to. It cannot run php and node in one project, it fails to give good info, and you cannot upload most file types directly to the project without it just going to assets. If you could at all help me I would appreciate it.


#6

I’m happy to help however I can, and we definitely don’t want to see you go if we can avoid it.

You’re right that if you’re trying to use both php and node in a single project to serve external requests that won’t work, since each process needs to listen to a different port, but we only open a single port in the project’s container. That’s not likely to change in the near future. You might be able to proxy those requests somehow, and you could certainly run your php and node processes in different projects and route to each of them from a proxy project as appropriate, but that’s getting a little complicated.

I’m curious what info you’re looking for that we’re not supplying; depending on what it is that’s something I might be able to take back to the rest of the team.

Similarly with upload - what file types are you trying to upload that are going to assets that you’d rather didn’t? A couple of general notes about this:

  1. asset management will be changing at some point in the not-too-distant future
  2. file allocations are based on file type, so if something unexpected is occurring there we might be able to take a look at it (keeping in mind point #1).
  3. you can always use wget in the console to download a file into the filesystem and put it wherever you want it.

#7

Thanks for trying to help,
I will stick with glitch due to the simplicity of hosting however, I am not able to remember what file types but there are a few I’d like to upload that I just couldn’t. Also I won’t be needing PHP but I still need help with the original question. Which is: How can I use server.js to send back info.
So basically I want the client script to send some sort of request and tell the server script to give it info for this package. The server will then return that info to the client.


#8

Hi @Ash13-Dev sorry this sort of dropped by the wayside.

It seems like you’ve made some progress and are headed the right direction of wrapping your server-side processes in an API that you can use from your client. I note that sending tracking info to pages/trackAPI.html results is detailed tracking data being logged to the console.

Are you all set then, or do you still have open questions?