0

I published an npm package. The directory structure is something like the following:

my-package
└── js/script.js
└── index.js

The js/script.js file contains an object that is the name of my library, lets say elephant. Something like this:

var elephant = {
    function_1: function() {
        ...
    },
    function_2: function() {
        ...
    }
}

In my index.js file, I am exporting this like so:

import { elephant } from "./js/script.js";

module.exports = elephant;

Once I published and installed my package, I tried to pull it in my project using the following line of code:

const elephant = require('my-package');
elephant.function_1();
elephant.function_2();

However, unfortunately when I run my server (in a Vue project), I get the following error:

Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>'

I assume this is referring to the third line in the index.js file. How can I fix this issue? How do I export my object variable and use it in my node project? Thanks for any help!

2
  • Have you tried swapping var elephant = to export elephant = in your script.js? I'm expecting the code-splitting is failing here... if not, have you tried console.log() all the way up? Commented Jul 12, 2020 at 1:28
  • @MatthewSpence Yes, that fails and I get Module parse failed Commented Jul 12, 2020 at 1:33

1 Answer 1

2

You can't mix import and module.exports

use export default elephant instead of module.exports = elephant;

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

1 Comment

The import works but if I call a function in node project, ie, elephant.function_1(), I get this error: "TypeError: elephant.function_1 is not a function".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.