S
S
shasoft2019-11-17 17:40:00
Node.js
shasoft, 2019-11-17 17:40:00

How to write a js file so that it can be picked up in both webpack and node?

I write like this

function fnTest(line) {
    return line.split('');
}
// Добавить правило в очередь правил
module.exports = function (line) {
   return [].concat(fnTest(line),fnTest(line+line));
};
I get the error "Uncaught TypeError: Cannot assign to read only property 'exports' of object" in the browser (building via Vue CLI)
If I remove the fnTest function, then everything is fine, there is no error. After a long search of options, I found out that the line.split ('') code gives an error, if you remove it, then everything is fine.
At the same time, in Node everything works this way and that. How to write so that it works there and there? Those. both when assembled and when used directly.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
shasoft, 2019-11-17
@shasoft

Apparently, the point here is not in export, but in the line.split function
As a solution, I replaced str.split with For full-scale projects, you can use https://www.npmjs.com/package/esm
node -r esm <js файл>

V
Vladimir, 2019-11-17
@HistoryART

First, never put ; after export.
Secondly, you need to export the function, and call it in the file
From where we export (let's say func.js)

function fnTest(line) {
    console.log('line', line.split(''));
}
// Добавить правило в очередь правил
module.exports = { fnTest }

Where do we export
const { fnTest } = require('func')

fnTest('aaa')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question