rollup/rollup-plugin-node-resolve

Could not resolve a require path (JSON)

Mottie opened this issue · 4 comments

I'm seeing the following error:

$ rollup -c

index.js → bundle.js...
[!] Error: Could not resolve './data/all' from node_modules\known-css-properties\index.js
Error: Could not resolve './data/all' from node_modules\known-css-properties\index.js
    at error (C:\Users\Me\AppData\Roaming\npm\node_modules\rollup\dist\rollup.js:181:12)
    at C:\Users\Me\AppData\Roaming\npm\node_modules\rollup\dist\rollup.js:10121:7
    at <anonymous>

The node module has an index.js that loads in a JSON file from a folder:

module.exports.all = require('./data/all').properties;

If I modify the above code to export the JSON data directly, it works as expected.

module.exports.all = [ /* ... */ ];

Here are the rollup config and package json files:

rollup.config.js

import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';

export default {
  entry: 'index.js',
  dest: 'bundle.js',
  moduleName: 'test',
  format: 'iife',
  plugins: [
    resolve({
      jsnext: true,
      main: true,
      browser: true,
      preferBuiltins: false
    }),
    commonjs()
  ]
};

package.json

{
  "name": "rollup-test",
  "version": "0.1.0",
  "description": "Rollup test of requiring a node_module json file",
  "main": "bundle.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "rollup -c"
  },
  "dependencies": {
    "known-css-properties": "^0.3.0"
  },
  "devDependencies": {
    "rollup-plugin-commonjs": "^8.1.0",
    "rollup-plugin-node-resolve": "^3.0.0"
  }
}

You will need an additional plugin to handle JSON. This one only takes care of path resolution.

The plugin is https://github.com/rollup/rollup-plugin-json

Hmm, I included the JSON plugin and I'm still seeing the same error.

import resolve from 'rollup-plugin-node-resolve'; // v3.0.0
import commonjs from 'rollup-plugin-commonjs'; // v8.2.1
import json from 'rollup-plugin-json'; // v2.3.0

export default {
  entry: 'index.js',
  dest: 'stylelint-bundle.js',
  moduleName: 'stylelint',
  format: 'iife',
  plugins: [
    json({
      preferConst: true,
      include: 'node_modules/**'
    }),
    resolve({
      jsnext: true,
      main: true,
      browser: true,
      preferBuiltins: false
    }),
    commonjs()
  ]
};

Does the plugin order matter?

Doesn't seem to matter according to https://github.com/rollup/rollup-plugin-json/blob/585fe3d167d22a7152f165f1dbe3d87197c6043b/src/index.js#L10.

Is there a way to import "../package" without the explicit .json extension?

Answering myself (Rollup newb here) - This seems to do the trick:

import json from 'rollup-plugin-json'
import resolve from 'rollup-plugin-node-resolve'

export default {
  input: './src/index.js',
  output: {
    file: './lib/index.js',
    format: 'umd',
  },
  plugins: [
    json(),
    resolve({
      extensions: ['.js', '.json'],
    }),
  ],
}

src/index.js:

import json from '../package'

console.log(json)

Tested with json plugin both before and after resolve plugin.