Skip to content
Advertisement

Error when import a node_module on svelte

i have to create a module to use on my application, but when I import that in my major project i have this error

Error: Cannot find module '.../node_modules/Table/dist/index.js'. Please verify that the package.json has a valid "main" entry

I have not the folder dist/index.js, in the guide I followed I did not see that it was necessary to make the package, also in another project i did not have these problems. My version of node is v12.16.2


I update my rollup to build with npm run dev

Here there is my new rollup

import svelte from 'rollup-plugin-svelte';
import resolve from 'rollup-plugin-node-resolve';
    
const pkg = require('./package.json');
    
export default {
        input: 'src/Table.svelte',
        output: {
            file: `dist/bundle.js`,
            format: "iife"
        },
        plugins: [
            svelte(),
            resolve({ preferBuiltins: true, mainFields: ['browser'] })
        ],
        external: ['uuid', 'object-exporter']
};

But i have again this error

internal/modules/cjs/loader.js:327
      throw err;
      ^

Error: Cannot find module '/../../node_modules/Table/dist/index.js'. Please verify that the package.json has a valid "main" entry

Advertisement

Answer

Your rollup config build your package in dist/bundle.js:

output: {
    file: `dist/bundle.js`,
    format: "iife"
},

But you probably defined in your package.json:

  "main": "dist/index.js",

But dist/index.js doesn’t exits. Just replace it with "main": "dist/bundle.js".

Advertisement